Introduction to Java introspection Mechanism

Source: Internet
Author: User
1. Introduction to the Java internal saving mechanism introspection is a way for Java to handle Bean class attributes and events (that is to say, a JavaBean object is given, we can get/call all its get/set methods ). For example, if Class A has a property name, we can use getname and setname to get its value or set a new value. Use getname/setname to access the name attribute. This is the default rule. Java provides a set of APIs used to access the getter/setter methods of a certain attribute. Through these APIs, you do not need to understand this rule. These APIs
Stored in the Java. Beans package. Generally, the beaninfo information of an object is obtained through the introspector class, and the property descriptor (propertydescriptor) is obtained through beaninfo ), the property descriptor can be used to obtain the getter/setter method corresponding to a property, and then we can call these methods through the reflection mechanism. Let's take an example. This example prints out all the attribute names and values of an object: // defines a javabeanpublic class personbean {public string name; Public String [] childname; public String [] getchildname () {return childname;} public void setchildname (string [] childname) {This. childname = childname;} Public String getname () {return name;} public void setname (string name) {This. name = Name ;}// define a test class to call the set and get methods. Import Java. beans. beani Nfo; import Java. beans. introspectionexception; import Java. beans. introspector; import Java. beans. propertydescriptor; import Java. lang. reflect. invocationtargetexception; public class testintrospector {public static void main (string [] ARGs) throws exceptions, exceptions, securityexception, nosuchmethodexception, invocationtargetexception, introspectionexception {// demonstrate call of the get Method // Initialize a JavaBean object personbean Pb = new personbean (); Pb. setname ("kangjian"); string [] Childs = new string [] {"KK", "JJ", "Nn"}; Pb. setchildname (Childs); // Add the attributes of the JavaBean to beaninfo. The second parameter is the deadline parameter, indicating that this class is not included before the deadline. If this parameter is not set, the info of the class and all its parent classes will be obtained. Beaninfo Bi = introspector. getbeaninfo (Pb. getclass (), object. class); // encapsulate the information of each attribute into a propertydescriptor to form an array, including the attribute name, read/write method, and attribute type. propertydescriptor [] Pd = Bi. getpropertydescriptors (); // demonstrate how to get for (INT I = 0; I <PD. length; I ++) {If (Pd [I]. getpropertytype (). isarray () // getpropertytype to obtain the attribute type. {// Getreadmethod () gets the get method of this attribute ---- method object, and then calls this method using invoke string [] result = (string []) Pd [I]. getreadmethod (). invoke (Pb, null); system. out. println (Pd [I]. getname () + ":"); // get the property name for (Int J = 0; j <result. length; j ++) {system. out. println (result [J]) ;}} else {system. out. println (Pd [I]. getname () + ":" + Pd [I]. getreadmethod (). invoke (Pb, null) ;}// demonstrate the call to the Set Method // initialize a set an personbean Pb that has not been set 0 = new personbean (); // simulate a data (the data name is the same as the JavaBean attribute name) string name = "Luonan "; string [] childname = new string [] {"Luo", "Nan"}; beaninfo bi0 = introspector. getbeaninfo (pb0.getclass (), object. class); propertydescriptor [] pd0 = bi0.getpropertydescriptors (); For (INT I = 0; I <pd0.length; I ++) {If (pd0 [I]. getpropertytype (). isarray () {If (pd0 [I]. getname (). equals ("childname"); {If (pd0 [I]. getpropertytype (). getcom Ponenttype (). equals (string. class) {// getcomponenttype () can get the element type of the array type // getwritemethod () Get the Set Method of this attribute --- method object, then use invoke to call this method pd0 [I]. getwritemethod (). invoke (pb0, new object [] {childname}) ;}} else {If (pd0 [I]. getname (). equals ("name"); {pd0 [I]. getwritemethod (). invoke (pb0, name) ;}} system. out. println (pb0.getname (); string [] array = pb0.getchildname (); For (INT I = 0; I <array. length; I ++) {s Ystem. out. println (array [I]) ;}} for more difficult applications, please refer to http://hi.baidu.com/shunxinyangkun/blog/item/dd2c4ff46ae61aeb7609d7ba.html 2. Thinking about introspection struts2 action (and struts1 formbean) is so implemented. The form tag at the front end has some attributes (the configuration file knows that the form is submitted to the action, and this action has the attributes corresponding to this form and their get/set). After submission, the struts servlet blocks and forwards the request to a specific action. before forwarding to the action, Struts sets the value in form to the action by saving the province. In fact, as long as there is a set ** or get **, the province will understand that there is such a ** attribute, this makes it easy for us to define bean classes through an interface without worrying about the specific implementation, rather than concerning the storage of bean data. For example, all the getter/setter methods can be defined in the interface, but the real data access is implemented in a specific class, which can improve the scalability of the system. Iii. Summarize and apply Java reflection and introspection to program design to greatly provide intelligent and scalable applications. Many projects adopt these two technologies to implement their core functions. For example, we mentioned struts and the digester project used to process XML files, in fact, almost all projects adopt these two technologies more or less. In practical application, the two must be combined to achieve real intelligence and high scalability. Original article link: http://polaris.blog.51cto.com/1146394/273614

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.