2010-02-16 73 views

回答

3

您可以使用java.beans.Introspector类获取有关给定bean的信息。您无法查询BeanInfo特定属性,但可以遍历他们:

private Class<?> getPropertyType(Class<?> clazz, String property) { 
    BeanInfo beanInfo = Introspector.getBeanInfo(clazz); 
    PropertyDescriptor[] propDescriptors = beanInfo.getPropertyDescriptors(); 
    for (PropertyDescriptor propDescriptor : propDescriptors) { 
     // String name of a property 
     if (property.equals(propDescriptor.getName())) { 
      // Class the getter corresponds to. 
      return propDescriptor.getPropertyType(); 
     } 
    } 
    ... 
} 
+0

你知道BeanUtil是否可以做到这一点? – 2010-02-16 11:31:39

+1

我想是这样,但在提出问题之前,您可能应该声明您使用的是Apache Commons! – 2010-02-16 11:36:25

0

发现... org.apache.commons.beanutils.PropertyUtils.getPropertyType(对象豆,字符串名称)

相关问题