I. Introduction to introspector
Introspection is mainly used to analyze JavaBean;
JavaBean:
(1) A public class;
(2) It is mainly used to store data;
(3) getter & setter method;
Classes such as inrospector, beaninfo, and propertydescriptor need to be involved;
2. introspector Development
Package Org. xiazdong. introspector; import Java. beans. beaninfo; import Java. beans. introspector; import Java. beans. propertydescriptor; import Java. lang. reflect. method; import Org. JUnit. test; import Org. xiazdong. person; public class introspectordemo01 {// obtain all bean attributes of the person class @ testpublic void test1 () throws exception {beaninfo info = introspector. getbeaninfo (person. class); propertydescriptor [] PDS = info. getpropertydescriptors (); For (propertydescriptor PD: PPS) {system. out. println (PD. getpropertytype () + "" + PD. getname () ;}// obtain all bean attributes of person (excluding object classes) @ testpublic void Test2 () throws exception {beaninfo info = introspector. getbeaninfo (person. class, object. class); propertydescriptor [] PDS = info. getpropertydescriptors (); For (propertydescriptor PD: PPS) {system. out. println (PD. getpropertytype () + "" + PD. getname () ;}}// set the age attribute of person @ testpublic void test3 () throws exception {person P = new person (); propertydescriptor Pd = new propertydescriptor ("Age", person. class); Method method = PD. getwritemethod (); method. invoke (p, 20); system. out. println (P. getage ();} // call the getage () @ testpublic void test4 () throws exception {person P = new person ("xiazdong", 30) of person ); propertydescriptor Pd = new propertydescriptor ("Age", person. class); Method method = PD. getreadmethod (); system. out. println (method. invoke (p, null ));}}