Inrospector learning Summary

Source: Internet
Author: User


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 ));}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.