Java introspection mechanism

Source: Internet
Author: User

First, introspection

  Introspection (introspector) is a default processing method of the Java language for JavaBean class properties and events. JavaBean is a special class that is used primarily to pass data information, in which methods are used primarily to access private fields, and the method name conforms to some naming convention. If you pass information between two modules, you can encapsulate the information into JavaBean, which is called a "value object", or "VO", with fewer methods, stored in a private variable of the class, obtained through set (), get (), as follows:

1  Public classStudent {2      PublicStudent () {3SYSTEM.OUT.PRINTLN ("Call construct Student method");4     }5      PublicStudent (intage,string name) {6          This. age=Age ;7          This. Name =name;8     }    9     Private intAge ;Ten     PrivateString name; One      Public intGetage () { A         returnAge ; -     } -      Public voidSetage (intAge ) { the          This. Age =Age ; -     } -      PublicString GetName () { -         returnname; +     } -      Public voidsetName (String name) { +          This. Name =name; A     } at}
View Code

There is a property name in the class person, so we can get its value by Getname,setname or set a new value by Getname/setname to access the Username property, which is the default rule. The Java JDK provides a set of Getter/setter methods that the API uses to access a property, which is introspection.

JDK Introspection Class Library: PropertyDescriptor class :

  The PropertyDescriptor class indicates that the JavaBean class exports a property through memory. Main methods:
1. Getpropertytype (), gets the property of the class object;
2. Getreadmethod (), obtain the method used to read the property value, Getwritemethod (), get the method to write the property value;
3. Hashcode (), gets the hash value of the object;
4. Setreadmethod (method Readmethod), set the way to read the value of the property;
5. Setwritemethod (method Writemethod), set methods for writing property values.

The specific examples are as follows:

1Student st =NewStudent (23, "Tri Ying");2String propertyname = "Age";3 //1. Create a PropertyDescriptor object from the constructor4PropertyDescriptor PD =NewPropertyDescriptor ("name", Student.class);5PropertyDescriptor PD2 =NewPropertyDescriptor ("Age", Student.class);6  //2. Use this object to obtain a write method7Method method1=Pd.getreadmethod ();8Method2=Pd2.getreadmethod ();9 //3. Execute the Write methodTenObject object=Method1.invoke (ST); OneObject Object2 =Method2.invoke (ST); A  //4. Output the value of the Object field - System.out.println (object); - System.out.println (object2); the   /*//5. Obtaining a Read method from an object - method = Pd.getreadmethod (); - //6. Execute the Read method and define the variable to accept its return value and force the shape - String name = (string) method.invoke (ST, null); + //7. Value after the output is shaped - System.out.println (name);*/ +}
View Code

In the example above, the property description class gets the Read property and the method to write the property, which allows the JavaBean property to be read. 

Another approach is to get beaninfo information for an object through the Getbeaninfo () method of the class Introspector, and then get the descriptor of the property by BeanInfo (PropertyDescriptor). With this property descriptor you can get the Getter/setter method for a property, and then we can invoke these methods through the reflection mechanism. We also often refer to JavaBean instance objects as value objects, because there are usually only some information fields and storage methods in these beans, there is no functional method, JavaBean is actually a specification, when a class satisfies this specification, This class can be called by other specific classes. When a class is used as a JavaBean, the JavaBean attribute is inferred from the method name, and it does not see the member variables inside the Java class, by removing the set method prefix, and then taking the remainder, if the second letter of the remainder is lowercase, The first letter of the remaining part is changed to lowercase.

1  Public Static voidTest2 ()throwsexception{2Student Student =NewStudent ();3            //1. Introspector to get the beaninfo of the Bean object4BeanInfo bif = Introspector.getbeaninfo (Student.class);5            //2. Obtaining a property descriptor by BeanInfo (PropertyDescriptor)6PropertyDescriptor pds[] =bif.getpropertydescriptors ();7            //3. Using the attribute descriptor to obtain the corresponding Get/set method8             for(PropertyDescriptor Pd:pds) {9                   //4. Get and output the name of the fieldTen System.out.println (Pd.getname ()); One                   //5. Type of the obtained and output field A System.out.println (Pd.getpropertytype ()); -                   if(Pd.getname (). Equals ("name")){ -                          //6. Get the Write method of the PropertyDescriptor object theMethod MD =Pd.getwritemethod (); -                          //7. Execute the Write method -Md.invoke (student, "Sunfei"); -                   } +            } -            //8. Output the value of the Assigned field + System.out.println (Student.getname ()); A}
View Code

Java introspection mechanism

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.