📄 beanutil.java
字号:
} catch (Exception e) { throw new IllegalArgumentException("Error executing index read method for " + ((descriptor != null)?descriptor.getName():"<null>") + " on " + ((curObject != null)?curObject.getClass().getName():"<null>")); } } } } /** * Helper method to retrieve a property editor out of cache. */ public static PropertyEditor getPropertyEditor(Class clazz) { // Try to retrieve editor from cache Object editor = propertyEditors.get(clazz); // If editor is null we must try to find it using the PropertyEditorManager if (editor == null) { editor = PropertyEditorManager.findEditor(clazz); if (editor == null) { // Flag that editor is null // Clone map and add the editor to the map // This way we can use the unsynchronized HashMap rather than Hashtable Map newEditorMap = (Map) ((HashMap) propertyEditors).clone(); newEditorMap.put(clazz, new Byte("0")); propertyEditors = newEditorMap; log.debug("PropertyEditorManager.findEditor returned null for class: " + clazz.getName()); } else if (editor instanceof FastPropertyEditor) { // Clone map and add the editor to the map // This way we can use the unsynchronized HashMap rather than Hashtable Map newEditorMap = (Map) ((HashMap) propertyEditors).clone(); newEditorMap.put(clazz, editor); propertyEditors = newEditorMap; } } else if (editor instanceof Byte) { return null; } return (PropertyEditor) editor; } /** * Helper method that gets the value using a PropertyEditor * If the editor is a FastPropertyEditor then getAsValue is called * otherwise normal setAsText, getValue calls are made */ public static Object getAsValue(PropertyEditor pe, String txt) { if (pe instanceof FastPropertyEditor) { return ((FastPropertyEditor)pe).getAsValue(txt); } else { pe.setAsText(txt); return pe.getValue(); } } /** * Call toString on object. If object has an associated ValidationEditorSupport * property editor then its getAsText will be called. This allows you to associate * a common toString algorithm to call types. * Objects of class String are however only returned */ public static String toStringValue(Object obj) { String result = ""; if (obj != null) { // If the object is a String we just return it to get best performance if (obj instanceof String) { return (String) obj; } PropertyEditor pe = getPropertyEditor(obj.getClass()); if (pe != null) { // FastPropertyEditors are cached so we MUST use the stateless // getAsText method to get the value if (pe instanceof FastPropertyEditor) { result = ((FastPropertyEditor)pe).getAsText(obj); } else if (pe instanceof ValidationEditorSupport) { pe.setValue(obj); result = pe.getAsText(); } else { result = obj.toString(); } } else { result = obj.toString(); } } return result; } /** * Get the value of the Index for indexed Properties. * * @param curSegment QuerySegment of type METHOD that we need the index from * @exception IllegalArgumentException */ private static Integer getIndexedPropertyIndex(QuerySegment curSegment) { Integer index; try { List values = curSegment.getValues(); if (values.size() != 1) { throw new IllegalArgumentException("Only Indexed properties allowed!"); } Object value = values.get(0); if(value instanceof Query) { index = new Integer(((Query)value).getSegments()[0].getValues().get(0).toString()); } else { index = new Integer(values.get(0).toString()); } } catch (Exception e) { throw new IllegalArgumentException("Only Indexed properties allowed! - Parameter must be an integer "); } return index; } /** * Get the actual number of QuerySegments in the array * * @param segments QuerySegment array for the propertyName that we are setting */ private static int findTheActualNumberOfSegments(QuerySegment[] segments) { for (int i = 0; i < segments.length; ++i) { if (segments[i] == null) { return i; } } return segments.length; //should never be here } /** * Set a non-indexed field. Will delegate the actuall setting of the field to * one of three methods. Sections were taken from BeanUtil.setProprty (rev.1.4) * * @param obj the object to set the property on. * @param descriptor the PropertyDescriptor representing the property to set * @param val the value to set * * @exception IllegalArgumentException */ static protected boolean setValue(Object obj, PropertyDescriptor descriptor, Object val) throws IllegalArgumentException { if (descriptor == null || descriptor.getWriteMethod() == null) { log.debug("No descriptor found for. Unable to set value. "); return false; } //no value, so don't set it. if (val == null) return false; Method m = descriptor.getWriteMethod(); Class valueClass = val.getClass(); // The getParameterTypes method is inefficient so we only call it once and save the result Class[] parameterClasses = m.getParameterTypes(); Class parameterClass = parameterClasses[0]; if (!valueClass.equals(String.class) && !valueClass.equals(String[].class) && !parameterClass.equals(String.class)) { return setObjectDirectly(parameterClass, valueClass, val, m, obj); } String[] value = convertObjectToStringArray(val); if (value != null) { if (descriptor.getPropertyEditorClass() == null) // No property editor given -> try default conversions { if (setStringValueDirectly(obj, descriptor, value)) { return true; } } // Set the value using a property editor. Pass in the parameterClass setStringValueWithPropertyEditor(obj, descriptor, value, parameterClass); return true; } return false; } /** * Will convert an object to a String array * * @param val the object to convert to an array */ private static String[] convertObjectToStringArray(Object val) { Class valueClass = val.getClass(); if (valueClass.equals(String.class)) { return new String[]{(String) val}; } if (valueClass.equals(String[].class)) { return (String[]) val; } return new String[]{val.toString()}; } /** * Set an indexed field. Will delegate the actual setting of the field to * one of three methods. Sections were taken from BeanUtil.setProprty (rev.1.4) * * @param obj the object to set the property on. * @param descriptor the PropertyDescriptor representing the property to set * @param val the value to set * @param index the index to set it at * * @exception IllegalArgumentException */ static protected boolean setIndexedValue(Object obj, IndexedPropertyDescriptor descriptor, Object val, Integer index) { if (descriptor == null) { log.info("no descriptor found"); return false; } Method m = descriptor.getIndexedWriteMethod(); Class valueClass = val.getClass(); Class parameterClass = m.getParameterTypes()[1]; if (!valueClass.equals(String.class) && !valueClass.equals(String[].class) && !parameterClass.equals(String.class)) { return setIndexedObjectDirectly(parameterClass, valueClass, val, m, obj, index); } String[] value = convertObjectToStringArray(val); if (value != null) { if (descriptor.getPropertyEditorClass() == null) // No property editor given -> try default conversions { if (setIndexedStringValueDirectly(obj, descriptor, value, index)) return true; } setIndexedStringValueWithPropertyEditor(obj, descriptor, value, index); return true; } else { return false; } } /** * Set a non-indexed field with an Object directly. This was taken from BeanUtil.setProprty (rev.1.4) * * @param parameterClass the class for the method * @param valueClass the class for what we are actually trying to set it to. * @param val the value we want to set * @param m the method we are using to set the value * @param obj the object we are trying to set it on * @param index the index to set it at * * @exception IllegalArgumentException */ private static boolean setIndexedObjectDirectly(Class parameterClass, Class valueClass, Object val, Method m, Object obj, Integer index) { // Try setting it directly if (!parameterClass.isAssignableFrom(valueClass)) { // Convert numbers //LogFactory.getLog(this.getClass()).debug("Assignable:"+Number.class.isAssignableFrom(valueClass)+" "+Number.class.isAssignableFrom(parameterClass)+" "+parameterClass); if (parameterClass.equals(int.class)) { parameterClass = Integer.class; } else if (parameterClass.equals(long.class)) { parameterClass = Long.class; } if (Number.class.isAssignableFrom(valueClass) && Number.class.isAssignableFrom(parameterClass)) { if (Long.class.isAssignableFrom(parameterClass)) { val = new Long(((Number) val).longValue()); } else if (Integer.class.isAssignableFrom(parameterClass)) { val = new Integer(((Number) val).intValue()); } } // throw new IllegalArgumentException("Parameter type of "+descriptor.getName()+"("+aMethod.getParameterTypes()[0].getName()+") does not match property type "+aValue.getClass().getName()); } try { Object[] writeParameter = new Object[]{index, val}; m.invoke(obj, writeParameter); return true; } catch (Exception e) { // Ignore and continue return false; } } /** * Set a non-indexed field with an Object directly. This was taken from BeanUtil.setProprty (rev.1.4) * * @param parameterClass the class for the method * @param valueClass the class for what we are actually trying to set it to. * @param val the value we want to set * @param m the method we are using to set the value * @param obj the object we are trying to set it on * * @exception IllegalArgumentException */ private static boolean setObjectDirectly(Class parameterClass, Class valueClass, Object val, Method m, Object obj) { // Try setting it directly if (!parameterClass.isAssignableFrom(valueClass)) { // Convert numbers //Category.getInstance(this.getClass()).debug("Assignable:"+Number.class.isAssignableFrom(valueClass)+" "+Number.class.isAssignableFrom(parameterClass)+" "+parameterClass); if (parameterClass.equals(int.class)) { parameterClass = Integer.class; } else if (parameterClass.equals(long.class)) { parameterClass = Long.class; } if (Number.class.isAssignableFrom(valueClass) && Number.class.isAssignableFrom(parameterClass)) { if (Long.class.isAssignableFrom(parameterClass)) { val = new Long(((Number) val).longValue()); } else if (Integer.class.isAssignableFrom(parameterClass)) { val = new Integer(((Number) val).intValue()); } } // throw new IllegalArgumentException("Parameter type of "+descriptor.getName()+"("+aMethod.getParameterTypes()[0].getName()+") does not match property type "+aValue.getClass().getName()); } try { Object[] writeParameter = new Object[]{val}; m.invoke(obj, writeParameter); return true; } catch (Exception e) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -