一、前言
关于jawr源码包net.jawr.web.util.PropertyUtils属性工具类,通过java.beans.PropertyDescriptor获取getProperty指定类对象属性值、变更指定类对象赋值setProperty、调用invokeMethod指定对象方法等操作。
二、源码说明
package net.jawr.web.util;@b@@b@import java.beans.BeanInfo;@b@import java.beans.IntrospectionException;@b@import java.beans.Introspector;@b@import java.beans.PropertyDescriptor;@b@import java.lang.reflect.InvocationTargetException;@b@import java.lang.reflect.Method;@b@import org.apache.log4j.Logger;@b@@b@public class PropertyUtils@b@{@b@ private static final Logger LOGGER = Logger.getLogger(PropertyUtils.class);@b@@b@ public static String getProperty(Object bean, String name)@b@ throws NoSuchMethodException, IllegalAccessException, InvocationTargetException@b@ {@b@ String value = null;@b@@b@ PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);@b@@b@ if (descriptor == null) {@b@ throw new NoSuchMethodException("Unknown property '" + name + "'");@b@ }@b@@b@ Method readMethod = descriptor.getReadMethod();@b@ if (readMethod == null) {@b@ throw new NoSuchMethodException("Property '" + name + "' has no getter method");@b@ }@b@@b@ Object result = invokeMethod(readMethod, bean, new Object[0]);@b@ if (result != null) {@b@ value = result.toString();@b@ }@b@@b@ return value;@b@ }@b@@b@ private static PropertyDescriptor getPropertyDescriptor(Object bean, String name)@b@ {@b@ int i;@b@ PropertyDescriptor descriptor = null;@b@ PropertyDescriptor[] descriptors = getPropertyDescriptors(bean.getClass());@b@ if (descriptors != null)@b@ {@b@ for (i = 0; i < descriptors.length; ++i)@b@ if (name.equals(descriptors[i].getName()))@b@ descriptor = descriptors[i];@b@ }@b@@b@ return descriptor;@b@ }@b@@b@ public static PropertyDescriptor[] getPropertyDescriptors(Class beanClass)@b@ {@b@ if (beanClass == null) {@b@ throw new IllegalArgumentException("No bean class specified");@b@ }@b@@b@ PropertyDescriptor[] descriptors = null;@b@@b@ BeanInfo beanInfo = null;@b@ try {@b@ beanInfo = Introspector.getBeanInfo(beanClass);@b@ } catch (IntrospectionException e) {@b@ return new PropertyDescriptor[0];@b@ }@b@ descriptors = beanInfo.getPropertyDescriptors();@b@ if (descriptors == null) {@b@ descriptors = new PropertyDescriptor[0];@b@ }@b@@b@ return descriptors;@b@ }@b@@b@ public static void setProperty(Object bean, String name, Object value)@b@ throws IllegalAccessException, InvocationTargetException, NoSuchMethodException@b@ {@b@ if (bean == null)@b@ throw new IllegalArgumentException("No bean specified");@b@@b@ if (name == null) {@b@ throw new IllegalArgumentException("No name specified");@b@ }@b@@b@ PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);@b@@b@ if (descriptor == null) {@b@ throw new NoSuchMethodException("Unknown property '" + name + "'");@b@ }@b@@b@ Method writeMethod = descriptor.getWriteMethod();@b@ if (writeMethod == null) {@b@ throw new NoSuchMethodException("Property '" + name + "' has no setter method");@b@ }@b@@b@ Object[] values = new Object[1];@b@ values[0] = value;@b@@b@ invokeMethod(writeMethod, bean, values);@b@ }@b@@b@ private static Object invokeMethod(Method method, Object bean, Object[] values)@b@ throws IllegalAccessException, InvocationTargetException@b@ {@b@ try@b@ {@b@ return method.invoke(bean, values);@b@ }@b@ catch (IllegalArgumentException e)@b@ {@b@ LOGGER.error("Method invocation failed.", e);@b@ throw new IllegalArgumentException("Cannot invoke " + method.getDeclaringClass().getName() + "." + method.getName() + " - " + e.getMessage());@b@ }@b@ }@b@}