First, about JavaBean
First of all, we need to understand what is JavaBean. In my previous thoughts, as long as the. Java end of the file is JavaBean, this understanding is wrong.
The following conditions must be met:
1, the property is private
2. Provide standard Gettter () and setter () methods
3. There must be no parameter constructor
Second, the introspection about the JavaBean operation
Introspection definition: Access to properties in JavaBean by reflection.
Classes used in JDK: PropertyDescriptor
1, the simple introspection operation to the JavaBean:
First, let's start by defining a simple javabean:
Package Com.reflect;public class Reflectpoint {private int x;private int y;public reflectpoint (int x, int y) {super (); . x = X;this.y = y;} public int GetX () {return x;} public void SetX (int x) {this.x = x;} public int GetY () {return y;} public void sety (int y) {this.y = y;}}
Next, we need to write a test
Package Com.reflect;import Java.beans.introspectionexception;import Java.beans.propertydescriptor;import Java.lang.reflect.invocationtargetexception;import Java.lang.reflect.method;public class IntroSpectorTest{public static void Main (string[] args) throws Exception {Reflectpoint pt1 = new Reflectpoint (3,5); String propertyname = "X"; Defines the property name in JavaBean object retVal = GetProperty (PT1, PropertyName); System.out.println (RetVal); 3Object value = 7;setproperties (PT1, PropertyName, value); System.out.println (Pt1.getx ());//7}private static void SetProperties (Object pt1, String propertyname,object value) Throws Introspectionexception,illegalaccessexception, InvocationTargetException {//Create property descriptor PropertyDescriptor PD = New PropertyDescriptor (PropertyName, Pt1.getclass ());//Get Write Method Methodsetx = Pd.getwritemethod (); Methodsetx.invoke (pt1, value);} Private static Object GetProperty (Object pt1, String PropertyName) throws Introspectionexception, illegalaccessexception,invocationtargetexception {PropeRtydescriptor PD = new PropertyDescriptor (PropertyName, Pt1.getclass ()); Method methodgetx = Pd.getreadmethod (); The Read method of the X attribute is obtained object retVal = Methodgetx.invoke (PT1); return retVal;}}
2, the complex introspection operation to JavaBean:
Through the class Introspector to get the BeanInfo information of an object, and then through BeanInfo to get the property descriptor (PropertyDescriptor), through this property descriptor can get a property corresponding to the Getter/setter method, and then we can invoke these methods through the reflection mechanism.
Or create a JavaBean first, ditto:
Package Com.reflect;public class Reflectpoint {private int x;private int y;public reflectpoint (int x, int y) {super (); . x = X;this.y = y;} public int GetX () {return x;} public void SetX (int x) {this.x = x;} public int GetY () {return y;} public void sety (int y) {this.y = y;}}
Then, write a test class:
Package Com.reflect;import Java.beans.beaninfo;import Java.beans.introspectionexception;import Java.beans.introspector;import Java.beans.propertydescriptor;import java.lang.reflect.InvocationTargetException; Import Java.lang.reflect.method;public class Introspectortest{public static void Main (string[] args) throws Exception { Reflectpoint pt1 = new Reflectpoint (3,5); String propertyname = "X"; Defines the property name in JavaBean object retVal = GetProperty (PT1, PropertyName); System.out.println (RetVal); Object value = 7;setproperties (PT1, PropertyName, value); System.out.println (Pt1.getx ());} private static void SetProperties (Object pt1, String propertyname,object value) throws Introspectionexception, Illegalaccessexception, invocationtargetexception {BeanInfo BeanInfo = Introspector.getbeaninfo (Pt1.getclass ()); propertydescriptor[] pds = Beaninfo.getpropertydescriptors (); for (PropertyDescriptor Pd:pds) {if (Pd.getname (). equals (PropertyName)) {Method methodsetx = Pd.getwritemethod (); Methodsetx.invoke (pt1, value); bReak;}}} Private static Object GetProperty (Object pt1, String PropertyName) throws Introspectionexception, illegalaccessexception,invocationtargetexception {BeanInfo BeanInfo = Introspector.getbeaninfo (Pt1.getclass ()); propertydescriptor[] pds = Beaninfo.getpropertydescriptors (), Object retVal = null;for (PropertyDescriptor pd:pds) {if ( Pd.getname (). Equals (PropertyName)) {Method methodgetx = Pd.getreadmethod (); retVal = Methodgetx.invoke (PT1); break;}} return retVal;}}
The result of the output is the same as the simple introspection,
Java Foundation Consolidation Series (eight): introspective operation on JavaBean