public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class IntrospectorTest {
<span style="white-space:pre"> </span>@SuppressWarnings("rawtypes")
<span style="white-space:pre"> </span>public static void main(String[] args) throws IntrospectionException {
<span style="white-space:pre"> </span>BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);//Introspector 内省
<span style="white-space:pre"> </span>System.out.println("BeanDescriptor================================");
<span style="white-space:pre"> </span>BeanDescriptor beanDesc = beanInfo.getBeanDescriptor(); //bean描述 descriptor描述符号
<span style="white-space:pre"> </span>Class cls = beanDesc.getBeanClass();
<span style="white-space:pre"> </span>System.out.println(cls.getName());
//<span style="white-space:pre"> </span>com.spring.learning.Person
<span style="white-space:pre"> </span>System.out.println("MethodDescriptor===============================");
<span style="white-space:pre"> </span>MethodDescriptor[] methodDescs = beanInfo.getMethodDescriptors();//method描述
<span style="white-space:pre"> </span>for (int i = 0; i < methodDescs.length; i++) {
<span style="white-space:pre"> </span>Method method = methodDescs[i].getMethod();
<span style="white-space:pre"> </span>System.out.println(method.getName());
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>/*
<span style="white-space:pre"> </span>hashCode
<span style="white-space:pre"> </span>wait
<span style="white-space:pre"> </span>notifyAll
<span style="white-space:pre"> </span>equals
<span style="white-space:pre"> </span>wait
<span style="white-space:pre"> </span>toString
<span style="white-space:pre"> </span>notify
<span style="white-space:pre"> </span>wait
<span style="white-space:pre"> </span>getClass
<span style="white-space:pre"> </span>setAge
<span style="white-space:pre"> </span>getAge
<span style="white-space:pre"> </span>setName
<span style="white-space:pre"> </span>getName
<span style="white-space:pre"> </span>*/
<span style="white-space:pre"> </span>System.out.println("PropertyDescriptor==============================");
<span style="white-space:pre"> </span>PropertyDescriptor[] propDescs = beanInfo.getPropertyDescriptors();
<span style="white-space:pre"> </span>for (int i = 0; i < propDescs.length; i++) {
<span style="white-space:pre"> </span>Method methodR = propDescs[i].getReadMethod();
<span style="white-space:pre"> </span>if (methodR != null) {
<span style="white-space:pre"> </span>System.out.println(methodR.getName());
<span style="white-space:pre"> </span>}
//<span style="white-space:pre"> </span>getAge
//<span style="white-space:pre"> </span>getClass
//<span style="white-space:pre"> </span>getName
<span style="white-space:pre"> </span>Method methodW = propDescs[i].getWriteMethod();
<span style="white-space:pre"> </span>if (methodW != null) {
<span style="white-space:pre"> </span>System.out.println(methodW.getName());
<span style="white-space:pre"> </span>}
//<span style="white-space:pre"> </span>setAge
//<span style="white-space:pre"> </span>setName
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>/*
<span style="white-space:pre"> </span>getAge
<span style="white-space:pre"> </span>setAge
<span style="white-space:pre"> </span>getClass
<span style="white-space:pre"> </span>getName
<span style="white-space:pre"> </span>setName
<span style="white-space:pre"> </span> */
<span style="white-space:pre"> </span>}
}