java使用Introspector枚举对象的属性,introspector枚举,package cn.o
分享于 点击 9109 次 点评:111
java使用Introspector枚举对象的属性,introspector枚举,package cn.o
package cn.outofmemory.snippets.core;import java.beans.BeanInfo;import java.beans.Introspector;import java.beans.PropertyDescriptor;public class ListBeanPropertyNames { public static void main(String[] args) throws Exception { BeanInfo beanInfo = Introspector.getBeanInfo(Bean.class); PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); for (int i=0; i<descriptors.length; i++) { String propName = descriptors[i].getName(); Class<?> propType = descriptors[i].getPropertyType(); System.out.println("Property with Name: " + propName + " and Type: " + propType); } } public static class Bean { // Property property1 private String property1; // Property property2 private int property2; public String getProperty1() { return property1; } public void setProperty1(String property1) { this.property1 = property1; } public int getProperty2() { return property2; } public void setProperty2(int property2) { this.property2 = property2; } }}
输出:
Property with Name: class and Type: class java.lang.ClassProperty with Name: property1 and Type: class java.lang.StringProperty with Name: property2 and Type: int
用户点评