Dark Horse programmer-introspector Introspection

Source: Internet
Author: User

--------------------- Android training, Java training, and hope to communicate with you! ----------------------
The so-called introspection is to use the reflection method to obtain or set the attributes of an object. This object must have the get and set methods. This class containing the get and set methods is also known as JavaBean. There are many introspection methods. The following example shows: String propertyname = "name ";
Student = new student ("pengyuding", 22); how to get the attributes in the "name" of the student object? (It is known that the student class contains the string name and INT age attributes and has the corresponding set and get methods)
1. Use the attribute name to piece together the get method, and then use reflection to get and call this method (this method is omitted here, which focuses on the following methods)
2. Use the property description class propertydescriptor to obtain the get method and then call it.
Propertydescriptor Pd = new propertydescriptor (propertyname, student. getclass (); // obtain the propertydescriptor of the "name" attribute in student.
Method method = Pd. getreadmethod (); // use the attribute description to obtain the get method. Similarly, you can obtain the set method.
Object retval2 = method. Invoke (student); // call this method
System. Out. println ("result obtained by the second method:" + retval2 );


3. Use the static method getbeaninfo of the introspector class to obtain information about the JavaBean, get the attribute description of the class, and use the method 2. Beaninfo = introspector. getbeaninfo (student. getclass (); // you can get the beaninfo object of the student class. beaninfo contains information that uses this class as the JavaBean.
Propertydescriptor [] PPS = beaninfo. getpropertydescriptors (); // use the beaninfo object to get a set of property descriptions (propertydescriptor) objects (only all property descriptions can be obtained here)
Object retval2 = NULL;
For (propertydescriptor PD: PDS)
{
If (PD. getname (). Equals (propertyname) // traverses this set to obtain the property named "name" and obtain the method and call it according to method 2.
{
Method method = Pd. getreadmethod ();
Retval2 = method. Invoke (student );
Break;
}
}

System. Out. println ("result obtained by the third method:" + retval2 );


4. Use the Toolkit beanutilssystem. Out. println (beanutils. getproperty (student, "name "));
Beanutils. setproperty (student, "Age", "9"); // here 9 uses the string system. Out. println (student. getage ());
Propertyutils. setproperty (student, "Age", 4); // here 4 uses the original type
System. Out. println (student. getage ());
Before using this tool, you must import the jar package of this tool. It provides two important static tool classes: beanutils and propertyutils. They all have the setproperty method. The difference is that beanutils uses the string type regardless of the received parameter or the returned parameter, while propertyutils processes the attribute original type. The same is true for getproperty.

-------------------- Android training, Java training, and hope to communicate with you! ---------------------- For details, please refer to: http://edu.csdn.net/heima

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.