Java examples for Reflection:Java Bean
get Java Bean Simple Property Read Method
//package com.java2s; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; public class Main { public static Method getSimplePropertyReadMethod(Object bean, String name) {// w ww. j a v a 2 s . com PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean, name); if (propertyDescriptor == null) throw new IllegalArgumentException("No property:" + name); return propertyDescriptor.getReadMethod(); } public static PropertyDescriptor getPropertyDescriptor(Object bean, String name) { PropertyDescriptor[] descriptors = getPropertyDescriptors(bean); for (int i = 0; i < descriptors.length; i++) { if (name.equals(descriptors[i].getName())) return descriptors[i]; } return null; } public static PropertyDescriptor[] getPropertyDescriptors(Object bean) { BeanInfo beanInfo = null; try { beanInfo = Introspector.getBeanInfo(bean.getClass()); } catch (IntrospectionException e) { return (new PropertyDescriptor[0]); } PropertyDescriptor[] descriptors = beanInfo .getPropertyDescriptors(); return descriptors; } }