Java uses the introspection mechanism to copy Object Information

Source: Internet
Author: User

In business development, we often encounter the following situation: to construct A new bean-B Based on the persistent bean information A, B needs most of the attribute information in, however, if you copy A's reference to B directly based on the business needs, the old data will be lost. In this case, you can clone it, But clone needs to modify the bean class, in addition, occasional business requirements lead to class redefinition, which can be regarded as code pollution. In this case, the java internal saving mechanism can be used to replicate attributes between objects. Introspection is a default Processing Method for Bean class attributes in Java. For example, there is a class Person with an age attribute. You can use the setAge () and getAge () methods to obtain/set the age value. You can use setAge ()/getAge () to obtain/set the age value as the default processing rule in Java. Java provides an API to access setAge ()/getAge () of an attribute (). In general usage, the BeanInfo information of an object is obtained through the class Introspector, and the property descriptor (PropertyDescriptor) is obtained through BeanInfo ), the property descriptor can be used to obtain the getter/setter methods corresponding to a property, and then call these methods through the reflection mechanism.
Public static void main (String [] args) throws Exception {
PersonInfo personInfoA = new PersonInfo (); personInfoA. setName ("tiger"); personInfoA. setAge (28); PersonInfo personInfoB = new PersonInfo (); copyProperties (personInfoB, personInfoA); personInfoB. setName (personInfoB. getName () + "temp"); personInfoB. setAge (personInfoB. getAge () + 1); System. err. println (personInfoA. toString (); System. err. println (personInfoB. toString ());
}
Public static void copyProperties (Object personInfoB, Object personInfoA) throws Exception {BeanInfo beanInfo = Introspector. getBeanInfo (personInfoA. getClass (); PropertyDescriptor [] props = beanInfo. getPropertyDescriptors (); for (int I = 0; I <props. length; I ++) {PropertyDescriptor srcProp = props [I]; // obtain the corresponding Property Information PropertyDescriptor destProp = findProperty (personInfoB, srcProp. getName (); if ((DestProp! = Null) & (destProp. getWriteMethod ()! = Null) & (srcProp. getReadMethod ()! = Null) try {// find the source Object property value Object srcVal = srcProp. getReadMethod (). invoke (personInfoA, new Object [0]); // call the attribute value assignment method of the target Object to copy the value of destProp. getWriteMethod (). invoke (personInfoB, new Object [] {srcVal});} catch (Exception ex) {}} public static class PersonInfo {private String name; private int age; public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age ;}} private static PropertyDescriptor findProperty (Object object Object, String name) throws IntrospectionException {// get the property information through the internal tool class BeanInfo = Introspector. getBeanInfo (object. getClass (); // get the property description information PropertyDescriptor [] properties = info. getPropertyDescriptors (); for (int I = 0; I <properties. length; I ++) {PropertyDescriptor property = properties [I]; if (property. getName (). equals (name) {return property;} return null ;}

Related Article

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.