Java examples for Reflection:Java Bean
get Java Bean Property Descriptor
//package com.java2s; import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; public class Main { public static void main(String[] argv) throws Exception { Class beanClass = String.class; String propertyName = "java2s.com"; System.out.println(getPropertyDescriptor(beanClass, propertyName)); }//from w ww. ja v a 2 s .co m public static PropertyDescriptor getPropertyDescriptor(Class beanClass, String propertyName) { for (PropertyDescriptor pd : getPropertyDescriptors(beanClass)) { if (pd.getName().equals(propertyName)) { return pd; } } return null; } public static PropertyDescriptor[] getPropertyDescriptors( Class beanClass) { BeanInfo beanInfo = null; try { beanInfo = Introspector.getBeanInfo(beanClass); } catch (IntrospectionException e) { return (new PropertyDescriptor[0]); } PropertyDescriptor[] descriptors = beanInfo .getPropertyDescriptors(); if (descriptors == null) { descriptors = new PropertyDescriptor[0]; } return descriptors; } public static PropertyDescriptor getPropertyDescriptors( Class beanClass, String name) { for (PropertyDescriptor pd : getPropertyDescriptors(beanClass)) { if (pd.getName().equals(name)) { return pd; } } return null; } }