📄 beanutil.java
字号:
if (e instanceof InvocationTargetException) { Throwable t = ((InvocationTargetException) e).getTargetException(); if (t instanceof IllegalArgumentException) { throw (IllegalArgumentException) t; } } // Ignore and continue return false; } } /** * Set a non-indexed field with a String value directly. * * @param obj the object we are trying to set it on * @param descriptor PropertyDescriptor representing the firled we want to set * @param values the String array we are setting * * @exception IllegalArgumentException */ static protected boolean setStringValueDirectly(Object obj, PropertyDescriptor descriptor, String[] values) throws IllegalArgumentException { try { Method m = descriptor.getWriteMethod(); if (descriptor.getPropertyType().equals(String.class) || descriptor.getPropertyType().equals(Object.class)) { m.invoke(obj, new Object[]{values[0]}); return true; } else if (descriptor.getPropertyType().equals(String[].class)) { m.invoke(obj, new Object[]{values}); return true; } } catch (Exception e) { if (e instanceof InvocationTargetException) { Throwable t = ((InvocationTargetException) e).getTargetException(); if (t instanceof IllegalArgumentException) { throw (IllegalArgumentException) t; } } throw new IllegalArgumentException(e.toString()); } return false; } /** * Set a non-indexed field with a String value directly using the Property Editor. * * @param obj the object we are trying to set it on * @param descriptor PropertyDescriptor representing the firled we want to set * @param values the String array we are setting * @param paramClass The parameter class used in the write method. Passed in for efficiency. * * @exception IllegalArgumentException */ static protected void setStringValueWithPropertyEditor(Object obj, PropertyDescriptor descriptor, String[] values, Class paramClass) throws IllegalArgumentException { try { PropertyEditor pe = getPropertyEditor(descriptor); Method m = descriptor.getWriteMethod(); if (pe == null) { throw new IllegalArgumentException("No property editor registered for this type"); // No editor for this parameter type } try { try { // The paramClass was passed to this method, now get the component type Class compType = paramClass.getComponentType(); if (compType != null) { Object a = Array.newInstance(compType, values.length); for (int i = 0; i < values.length; i++) { Array.set(a, i, getAsValue(pe, values[i])); } m.invoke(obj, new Object[]{a}); } else { Object realValue = getAsValue(pe, values[0]); m.invoke(obj, new Object[]{realValue}); } } catch (NumberFormatException nfe) { //need this for Sun's Editor's throw new PropertyEditorException(PropertyMessage.BAD_VALUE, values[0]); } } catch (IllegalArgumentException ex) { if (obj instanceof IllegalArgumentAware) { ((IllegalArgumentAware) obj).addIllegalArgumentException(descriptor.getName(), ex); } else { throw ex; } } } catch (IllegalArgumentException iae) { throw iae; } catch (Exception e) { if (e instanceof InvocationTargetException) { Throwable t = ((InvocationTargetException) e).getTargetException(); if (t instanceof IllegalArgumentException) { throw (IllegalArgumentException) t; } } throw new IllegalArgumentException(e.toString()); } } /** * Set a indexed field with a String value directly using the Property Editor. * * @param obj the object we are trying to set it on * @param descriptor PropertyDescriptor representing the firled we want to set * @param values the String array we are setting * @param index the index that we want to set * * @exception IllegalArgumentException */ static protected void setIndexedStringValueWithPropertyEditor(Object obj, IndexedPropertyDescriptor descriptor, String[] values, Integer index) throws IllegalArgumentException { try { PropertyEditor pe = getPropertyEditor(descriptor); Method m = descriptor.getIndexedWriteMethod(); if (pe == null) { throw new IllegalArgumentException("No property editor registered for this type"); // No editor for this parameter type } Object realValue = getAsValue(pe, values[0]); m.invoke(obj, new Object[]{index, realValue}); } catch (IllegalArgumentException iae) { throw iae; } catch (Exception e) { if (e instanceof InvocationTargetException) { Throwable t = ((InvocationTargetException) e).getTargetException(); if (t instanceof IllegalArgumentException) { throw (IllegalArgumentException) t; } } throw new IllegalArgumentException(e.toString()); } } /** * Set a indexed field with a String value directly. * * @param obj the object we are trying to set it on * @param descriptor PropertyDescriptor representing the firled we want to set * @param values the String array we are setting * @param index the index that we want to set * * @exception IllegalArgumentException */ static protected boolean setIndexedStringValueDirectly(Object obj, IndexedPropertyDescriptor descriptor, String[] values, Integer index) throws IllegalArgumentException { try { Method m = descriptor.getIndexedWriteMethod(); if ((descriptor.getIndexedPropertyType().equals(String.class)) || (descriptor.getIndexedPropertyType().equals(Object.class))) { m.invoke(obj, new Object[]{index, values[0]}); return true; } else if (descriptor.getIndexedPropertyType().equals(String[].class)) { m.invoke(obj, new Object[]{index, values}); return true; } } catch (Exception e) { if (e instanceof InvocationTargetException) { Throwable t = ((InvocationTargetException) e).getTargetException(); if (t instanceof IllegalArgumentException) { throw (IllegalArgumentException) t; } else { //this really will say nothing helpful, so let's log it //more clutter, but at least debugging becomes possible log.error("Error invoking indexed property method " + descriptor.getIndexedWriteMethod().getName() + " index=" + index + " values=" + Arrays.asList(values), t); throw new IllegalArgumentException(t.toString()); } } throw new IllegalArgumentException(e.toString()); } return false; } /** * Get a property descriptor for a given field on an object * * @param property the property that we are interested in * @param obj the object we want the property descriptor for */ static protected PropertyDescriptor getPropertyDescriptor(String property, Object obj) { Map fieldMap = getFieldMapForClass(obj.getClass()); return (PropertyDescriptor) fieldMap.get(property); } /** * Get the field map for a class. The map is used by getPropertyDescriptor. * If it does not exist for the class it will call buildFieldMap * * @param objClass the object we want to build the field map for */ static protected Map getFieldMapForClass(Class objClass) { Map fieldMap = (Map) objectMap.get(objClass); if (fieldMap == null) { fieldMap = buildFieldMap(objClass); if (fieldMap != null) { objectMap.put(objClass, fieldMap); } } return fieldMap; } /** * Build field map for a class. * * @param objClass the object we want to build the field map for * @return a Map with the keys being property names and values being PropertyDescriptors */ static protected Map buildFieldMap(Class objClass) { Map fieldMap = new WeakHashMap(); PropertyDescriptor[] descriptors = getPropertyDescriptors(objClass); if (descriptors != null) { for (int i = 0; i < descriptors.length; ++i) { fieldMap.put(descriptors[i].getName(), descriptors[i]); } return fieldMap; } return null; } /** * get The property Editor for the propertyDescriptor. Handles both Indexed and non_indexed properties * * @param descriptor PropertyDescriptor * @return the PropertyEditor */ static protected PropertyEditor getPropertyEditor(PropertyDescriptor descriptor) { Class peClass = descriptor.getPropertyEditorClass(); try { if (peClass == null) { if (descriptor instanceof IndexedPropertyDescriptor) { return getPropertyEditor(((IndexedPropertyDescriptor) descriptor).getIndexedPropertyType()); } else { return getPropertyEditor(descriptor.getPropertyType()); } } else { return (PropertyEditor) peClass.newInstance(); } } catch (Exception e) { throw new IllegalArgumentException(e.toString()); } } /** * Get property descriptors for a class. * Only return descriptors for properties that are not a property of java.lang.Object * based on webwork (0.95) Dispatcher Code * * @param objClass * @return array of property descriptors */ protected synchronized static PropertyDescriptor[] getPropertyDescriptors(Class objClass) { PropertyDescriptor[] descriptors = (PropertyDescriptor[]) propertyDescriptors.get(objClass); // Check if available if (descriptors == null) { BeanInfo beanInfo; try { beanInfo = Introspector.getBeanInfo(objClass, Object.class); } catch (IntrospectionException e) { // We're in trouble // Let it try again on next request, but return "nothing" for now return new PropertyDescriptor[0]; } // Get list of descriptors that are ok descriptors = beanInfo.getPropertyDescriptors(); List list = new ArrayList(descriptors.length); for (int i = 0; i < descriptors.length; i++) { PropertyDescriptor descriptor = descriptors[i]; if (descriptor instanceof IndexedPropertyDescriptor) { Method readMethod = ((IndexedPropertyDescriptor) descriptor).getIndexedReadMethod(); Method writeMethod = ((IndexedPropertyDescriptor) descriptor).getIndexedWriteMethod(); //make sure it is not from Object if (verifyNotObjectProperty(readMethod, writeMethod)) { list.add(descriptor); } } else { Method readMethod = descriptor.getReadMethod(); Method writeMethod = descriptor.getWriteMethod(); if (verifyNotObjectProperty(readMethod, writeMethod)) { list.add(descriptor); } } } descriptors = new PropertyDescriptor[list.size()]; list.toArray(descriptors); // Put list into cache propertyDescriptors.put(objClass, descriptors); } return descriptors; } /** * Verify That the property is not from Object * * @param readMethod The ReadMethod From the PropertyDescriptor * @param writeMethod The WriteMethod From the PropertyDescriptor * @return true if not Object property */ private static boolean verifyNotObjectProperty(Method readMethod, Method writeMethod) { return (readMethod != null && !readMethod.getDeclaringClass().equals(Object.class)) || (writeMethod != null && !writeMethod.getDeclaringClass().equals(Object.class)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -