New characteristics of 20--JDK 4-framework development within the province

Source: Internet
Author: User
Tags reflection

1. The role of introspection :

When developing a framework, it is often necessary to use the properties of Java objects to encapsulate the data of the program, each time using reflection technology to complete such operations is too cumbersome, so sun has developed a set of APIs, specifically for manipulating Java object properties.

The process of introspection is the process of accessing JavaBean. Sometimes you don't know the code in JavaBean, for example, when you do framework development, you need introspection.

L introspective access to JavaBean properties in two ways:

• Manipulating bean properties through the PropertyDescriptor class

• Get the BeanInfo of the Bean object through the Introspector class, and then BeanInfo to get the descriptor (PropertyDescriptor) of the property, through which the property descriptor can get a corresponding getter/setter method, and then call these methods through the reflection mechanism.

2. Understanding Introspection

Introspection is a default processing method of the Java language for Bean class properties . For example, Class A has property name, which can be getname,setname to get its value or set a new value. The Name property is accessed by Getname/setname, which is the default rule. Java provides a set of APIs to access the Getter/setter method of a property. The general practice is to get the BeanInfo information of an object through the class Introspector , and then BeanInfo to get the descriptor (PropertyDescriptor ) of the property. This property descriptor allows you to get a Getter/setter method for a property and then invoke these methods through the reflection mechanism

3. Case study

packageCom.java.Bean;

importJava.beans.BeanInfo;

importjava.beans.IntrospectionException;

importjava.beans.Introspector;

importjava.beans.PropertyDescriptor;

importjava.lang.reflect.InvocationTargetException;

importJava.lang.reflect.Method;

Publicclass BeanDemo1 {

/**

* @param args

* @throws introspectionexception

* @throws invocationtargetexception

* @throws illegalargumentexception

* @throws illegalaccessexception

*/

publicstaticvoid Main (string[] args)throws Introspectionexception,illegalaccessexception, IllegalArgumentException, InvocationTargetException {

Test4 ();

}

Get all the properties of the person bean by introspection

publicstaticvoid test1 ()throws introspectionexception {

BeanInfo bi = introspector.getbeaninfo (person. Class, Object. class); The *introspector class provides a standard way to learn about the properties, events, and methods supported by the target Java bean through tools.

The Beaninfor class implements this BeanInfo interface and provides explicit information about its bean's methods, properties, events, and so on;

The parameter after the method Object.class is to terminate the class up, or the Getbeaninfo method will always index up, without this parameter, the output will call up the getclass*/in the parent class object.

Propertydescriptor[] PD =bi.getpropertydescriptors ();

for (PropertyDescriptor PDS:PD) {

System.out.println (Pds.getname ());

}

}

Assign value by introspection to person's Name property: Qwe setname ("abc")

publicstaticvoid test2 ()throws introspectionexception, Illegalaccessexception, IllegalArgumentException, InvocationTargetException {

Person p = new person ();

BeanInfo bi = introspector.getbeaninfo (person. Class);

Propertydescriptor[] PD =bi.getpropertydescriptors ();

for (PropertyDescriptor PDS:PD) {

String name = Pds.getname ();

if (Name.equals ("name")) {//Here are two name different, one is declared in the For loop, one is in the JavaBean, the latter is the Equals () method must enclose in quotation marks

Method M =pds.getwritemethod (); Returns a reflect (reflection) class method

M.invoke (P, "qwe"); Invokes the underlying methods represented by this method object for the specified object with the specified parameter.

}

}

System.out.println (P.getname ());

}

Assigning values to the property name property of a bean through the PropertyDescriptor class: ABC SetName ("abc")

publicstaticvoid test3 ()throws Introspectionexception,illegalaccessexception, IllegalArgumentException, InvocationTargetException {

Person p = new person ();

PropertyDescriptor PD = new propertydescriptor ("name", P.getclass ());//propertydescriptor Description Java A property that the bean exports through a pair of storage methods.

Method M = Pd.getwritemethod (); Gets the method that should be used to read the property value. Equivalent to the Get method in JavaBean

M.invoke (P, "abc");

System.out.println (P.getname ());

}

publicstaticvoid test4 ()throws Introspectionexception,illegalaccessexception, IllegalArgumentException, InvocationTargetException {

Person p = new person ();

P.setname ("Hans");

PropertyDescriptor PD = new propertydescriptor ("name", P.getclass ());

Method M = Pd.getreadmethod (); Gets the method that should be used to read the property value. Equivalent to set method

String str = (string) M.invoke (p,null);

System.out.println (str);

}

}

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.