一、前言
关于spring-beans包中org.springframework.beans.PropertyDescriptorUtils属性工具类,对javabean(属性特有getter/setter方法)通过java.beans.PropertyDescriptor映射读取属性值方法getReadMethod()、写入属性值的方法getWriteMethod()进行原属性PropertyDescriptor间复制copy、查找属性类型findPropertyType及其等值判断equals等操作。
二、源码说明
package org.springframework.beans;@b@@b@import java.beans.IntrospectionException;@b@import java.beans.PropertyDescriptor;@b@import java.lang.reflect.Method;@b@import java.util.Enumeration;@b@import org.springframework.util.ObjectUtils;@b@@b@class PropertyDescriptorUtils@b@{@b@ public static void copyNonMethodProperties(PropertyDescriptor source, PropertyDescriptor target)@b@ throws IntrospectionException@b@ {@b@ target.setExpert(source.isExpert());@b@ target.setHidden(source.isHidden());@b@ target.setPreferred(source.isPreferred());@b@ target.setName(source.getName());@b@ target.setShortDescription(source.getShortDescription());@b@ target.setDisplayName(source.getDisplayName());@b@@b@ Enumeration keys = source.attributeNames();@b@ while (keys.hasMoreElements()) {@b@ String key = (String)keys.nextElement();@b@ target.setValue(key, source.getValue(key));@b@ }@b@@b@ target.setPropertyEditorClass(source.getPropertyEditorClass());@b@ target.setBound(source.isBound());@b@ target.setConstrained(source.isConstrained());@b@ }@b@@b@ public static Class<?> findPropertyType(Method readMethod, Method writeMethod)@b@ throws IntrospectionException@b@ {@b@ Class[] params;@b@ Class propertyType = null;@b@@b@ if (readMethod != null) {@b@ params = readMethod.getParameterTypes();@b@ if (params.length != 0)@b@ throw new IntrospectionException("Bad read method arg count: " + readMethod);@b@@b@ propertyType = readMethod.getReturnType();@b@ if (propertyType == Void.TYPE)@b@ throw new IntrospectionException("Read method returns void: " + readMethod);@b@@b@ }@b@@b@ if (writeMethod != null) {@b@ params = writeMethod.getParameterTypes();@b@ if (params.length != 1)@b@ throw new IntrospectionException("Bad write method arg count: " + writeMethod);@b@@b@ if (propertyType != null) {@b@ if (propertyType.isAssignableFrom(params[0]))@b@ {@b@ propertyType = params[0]; break label198:@b@ }@b@ if (params[0].isAssignableFrom(propertyType)) { break label198:@b@ }@b@@b@ throw new IntrospectionException("Type mismatch between read and write methods: " + readMethod + " - " + writeMethod);@b@ }@b@@b@ propertyType = params[0];@b@ }@b@@b@ label198: return propertyType;@b@ }@b@@b@ public static Class<?> findIndexedPropertyType(String name, Class<?> propertyType, Method indexedReadMethod, Method indexedWriteMethod)@b@ throws IntrospectionException@b@ {@b@ Class[] params;@b@ Class indexedPropertyType = null;@b@@b@ if (indexedReadMethod != null) {@b@ params = indexedReadMethod.getParameterTypes();@b@ if (params.length != 1)@b@ throw new IntrospectionException("Bad indexed read method arg count: " + indexedReadMethod);@b@@b@ if (params[0] != Integer.TYPE)@b@ throw new IntrospectionException("Non int index to indexed read method: " + indexedReadMethod);@b@@b@ indexedPropertyType = indexedReadMethod.getReturnType();@b@ if (indexedPropertyType == Void.TYPE)@b@ throw new IntrospectionException("Indexed read method returns void: " + indexedReadMethod);@b@@b@ }@b@@b@ if (indexedWriteMethod != null) {@b@ params = indexedWriteMethod.getParameterTypes();@b@ if (params.length != 2)@b@ throw new IntrospectionException("Bad indexed write method arg count: " + indexedWriteMethod);@b@@b@ if (params[0] != Integer.TYPE)@b@ throw new IntrospectionException("Non int index to indexed write method: " + indexedWriteMethod);@b@@b@ if (indexedPropertyType != null) {@b@ if (indexedPropertyType.isAssignableFrom(params[1]))@b@ {@b@ indexedPropertyType = params[1]; break label289:@b@ }@b@ if (params[1].isAssignableFrom(indexedPropertyType)) { break label289:@b@ }@b@@b@ throw new IntrospectionException("Type mismatch between indexed read and write methods: " + indexedReadMethod + " - " + indexedWriteMethod);@b@ }@b@@b@ indexedPropertyType = params[1];@b@ }@b@@b@ if ((propertyType != null) && (((!(propertyType.isArray())) || @b@ (propertyType@b@ .getComponentType() != indexedPropertyType)))) {@b@ label289: throw new IntrospectionException("Type mismatch between indexed and non-indexed methods: " + indexedReadMethod + " - " + indexedWriteMethod);@b@ }@b@@b@ return indexedPropertyType;@b@ }@b@@b@ public static boolean equals(PropertyDescriptor pd, PropertyDescriptor otherPd)@b@ {@b@ return ((ObjectUtils.nullSafeEquals(pd.getReadMethod(), otherPd.getReadMethod())) && @b@ (ObjectUtils.nullSafeEquals(pd@b@ .getWriteMethod(), otherPd.getWriteMethod())) && @b@ (ObjectUtils.nullSafeEquals(pd@b@ .getPropertyType(), otherPd.getPropertyType())) && @b@ (ObjectUtils.nullSafeEquals(pd@b@ .getPropertyEditorClass(), otherPd.getPropertyEditorClass())) && @b@ (pd@b@ .isBound() == otherPd.isBound()) && (pd.isConstrained() == otherPd.isConstrained()));@b@ }@b@}