1. Simple introspective operation on the JavaBean
public classintrospectortest {public static void main (string[] args) throwsexception{reflectpoint pt1 = new Reflectpoint (3, 5); String propertyname = "x" ; "X"--"X"--"GetX"-methodgetx--> Object retVal = getProperty (pt1, PropertyName); System.out.println (RetVal); Object value = 7 ; setproperties (PT1, PropertyName, value); System.out.println (Pt1.getx ()); } private static Object GetProperty (Object pt1, String PropertyName) throws exception{propertydescriptor pd = New PropertyDescriptor (PropertyName, Pt1.getclass ()); Method methodgetx = Pd.getreadmethod (); Object retVal = methodgetx.invoke (pt1); return RetVal;} private static void SetProperties (object pt1, String PropertyName, Object value) throws exception{propertydescriptor pd = new PropertyDescriptor (PropertyName, Pt1.getclass ()); Method methodsetx = pd.getwritemethod (); Methodsetx.invoke (pt1, value);}
Adopt a complex approach
Private static Object GetProperty (Object pt1, String PropertyName) throws exception{// propertydescriptor pd = NE W PropertyDescriptor (PropertyName, Pt1.getclass ());// Method methodgetx = Pd.getreadmethod ();// Object RetVal = Methodgetx.invoke (pt1); Take a complex approach //treat a common class as JavaBean to see BeanInfo BeanInfo = introspector.getbeaninfo (Pt1.getclass ()); propertydescriptor[] PropertyDescriptors = beaninfo.getpropertydescriptors (); Object retVal = null; For(PropertyDescriptor pd:propertydescriptors) {if(Pd.getname (). Equals (PropertyName)) {Method Readmethod = Pd.getreadmethod (); retVal = Readmethod.invoke (PT1); break;}} return retVal;}
2. Use the Beanutils Toolkit to operate the JavaBean
/** * Use beanutils * To set properties and get values using a string, 1. Conversion of auto-complete type */ System.out.println (Beanutils.getproperty ( PT1, "x"). GetClass (). GetName ()); Beanutils.setproperty (PT1, "X", "9"); System.out.println (Pt1.getx ()); 2. Supports cascading operations for attributes Beanutils.setproperty (pt1, "Birthday.time", "111"); System.out.println (Beanutils.getproperty (pt1, "birthday.time"));
View Help documents:/apache-commons/commons-beanutils-1.8.0-bin/commons-beanutils-1.8.0/apidocs/index.html
You can copy the properties of an object to another object.
static void Copyproperties (Object dest, Object orig) Copy property values from the Origin bean to the Desti Nation Bean for all cases where the property names is the same.
You can convert the properties of JavaBean to a map
Static Map describe (Object Bean) Return The entire set of properties for which the specified Bean provides a r Ead method.
Fill the map into the JavaBean.
static void populate (Object Bean, Map properties) populate the JavaBeans properties of the specified Bean, bas Ed on the specified name/value pairs
Beanutils is the operation of JavaBean in the form of a string,
Propertyutils is the type of bean itself that operates on JavaBean
Use Propertyutils propertyutils.setproperty (pt1, "X", 9); System.out.println (Propertyutils.getproperty (PT1, "X"). GetClass () . GetName ());
Java high-tech-operation JavaBean