Introspection (Introspector) is a default processing method of the Java language for JavaBean class properties and events.
JavaBean is a special class that is primarily used to pass data information, and methods in this kind are primarily used to access private fields, and the method name conforms to a certain naming convention. If you pass information between two modules, you can encapsulate the information into JavaBean, which is called a value object, or VO. method is less than. This information is stored in the private variables of the class, and the
is obtained through set (), get ().
 JAVA JDK provides a set of Getter/setter methods that the API uses to access a property, which is introspective
Package Com.liang.instopector;import Java.io.serializable;import Java.util.date;public class Student implements Serializable {private string Name= "Liang";p rivate int age=21;private Date birthday;public string GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public Date Getbirthday () {return birthday;} public void Setbirthday (Date birthday) {this.birthday = birthday;}}
package Com.liang.instopector;import Java.beans.beaninfo;import Java.beans.introspector;import Java.beans.propertydescriptor;import Java.lang.reflect.method;import Java.util.date;import Org.junit.Test;public Class Demo {@Testpublic void Test1 () throws exception{//gets the specified BeanInfo object beanInfo beanInfo = Introspector.getbeaninfo ( Student.class);//Get Property Description Array propertydescriptor[] PDS = Beaninfo.getpropertydescriptors ();//Traversal: Because the class inherits the object class, There is a getclass () method in the object class, so the number is one System.out.println (pds.length) more than you write; for (PropertyDescriptor Pd:pds) { System.out.print (Pd.getname () + "");} System.out.println ();//Get the specified propertydescriptorpropertydescriptor pd=new propertydescriptor ("name", Student.class) ;//Get Get Method M=pd.getreadmethod (); Student s=new Student (); String str= (String) M.invoke (s, null); System.out.println (str);//Get Set Method M1=pd.getwritemethod (); M1.invoke (S, "AAA"); System.out.println (S.getname ());}}
Java Introspection (Introspector)