Using Reflection mechanisms to copy attributes between forms

Source: Internet
Author: User

  • If you do something in struts, you will often encounter the copy-on of formBean. Simply put, if two different formbeans have the same attributes, assign the values of the same formBean attributes to the same attributes of the other formBean. (Of course, it would be better if the two formbeans are the same)

  • If you do not understand this, org. apache. commons. beanutils. copyProperties (java. lang. object dest, java. lang. object orig) You must have used it. The worst thing about this method is that the first formBean completely overwrites the value of the second formbean attribute. For example, if one attribute of bean1 is null, if this method is used, naturally this null will overwrite the bean2 attribute value. Sometimes we cannot accept this result. What we need is that bean1 will overwrite the bean2 attribute to be empty, that is, when bean2 has a value, bean1 does not overwrite the bean2 attribute. What are the advantages? I am not quite clear about it myself. People who want to use hibenate will have a deeper understanding.


  • I wrote a similar class, but due to limited time and capabilities, this class can only copy formBean
    That is to say, all the attributes of this bean must be of the String type, and the String [] cannot be used. Although the function is limited, I think this idea is good, you may wish to write a better copyProperties () method than apache on the basis of me. I haven't been here for a long time and have been working overtime recently. Therefore, I may not be able to maintain this post much time. I hope you will forgive me.

  • The following is the source code, there is still a test formBean, but this will make this post too long, my comments are very detailed, so it is not long-winded, however, let's simply talk about the implementation idea: First traverse the formBean attributes, put these attributes into a container, and then traverse the attributes to get the get and set methods, then, the attribute name and the two methods are used as an example. that is to say, if you use the getMethodsMap method, you can get the ing set of the property and the get and set methods (except for the property of the get or set method does not exist here ), then, we traverse the projection set of 2nd formbeans and compare and assign values to the projection set of the first formBean. If you want to expand it, you also need to determine the attribute type. I have some basic ideas, however, I am very busy and have no time. I hope you can help me fulfill this wish.

    Package com. xjw. utils;
    Import java. lang. reflect .*;
    Import java. util .*;

    /**
    This class is used to copy attributes between formbeans. The main method is copyFormBeanPropertys.
    */
    Public class BeanUtil {
    /**
    Get the fields container
    @ Param Class objClass the Class Object of the current object
    @ Return ArrayList: container for loading Object Attributes
    */
    Public static ArrayList getFildsArray (Class objClass ){
    ArrayList al = null;
    Try {
    // Obtain all attributes
    Field [] fields = objClass. getDeclaredFields ();
    Al = new ArrayList ();
    For (int I = 0; I <fields. length; I ++ ){
    Al. add (fields [I]. getName ());
    }
    } Catch (Exception e ){
    Al = null;
    System. out. println (e );
    }
    Return al;
    }


    /**
    Obtain the ing between bean attributes and methods.
    @ Param Class objClass the Class Object of the current object
    @ Return HashMap: container that loads the ing between attributes and Methods
    */
    Public static HashMap getMethodsMap (Class objClass ){
    HashMap hm = null;
    Try {
    // Obtain all methods of this object
    Method [] methods = objClass. getDeclaredMethods ();
    Hm = new HashMap (); // container that loads the ing between attributes and Methods
    String fieldName = ""; // The first letter of the attribute name is uppercase.
    String methodName = ""; // method name
    ArrayList al = getFildsArray (objClass); // obtain all attributes of this class
    Boolean isEndWithGet = false; // whether it starts with get
    Boolean isFind = false; // determines whether the method name contains this attribute name.
    Boolean isEndWithSet = false; // whether it is starting with set
    If (al! = Null) {// attributes cannot exist
    Int alSize = al. size (); // number of attributes
    For (int I = 0; I <alSize; I ++ ){
    // Obtain the attribute name in upper case.
    FieldName = upFirstChar (String) al. get (I ));
    // Get and set methods corresponding to attribute names
    Method [] myMothodArrag = new Method [2];
    // Traverse all methods to find the set and get methods with the same attribute name
    For (int j = 0; j <methods. length; j ++ ){
    MethodName = (methods [j]. getName ());
    IsEndWithGet = methodName. startsWith ("get ");
    IsFind = methodName. endsWith (fieldName );
    IsEndWithSet = methodName. startsWith ("set ");
    If (isFind & isEndWithGet ){
    MyMothodArrag [0] = methods [j]; // put the get method first in the Custom container
    } Else if (isFind & isEndWithSet ){
    MyMothodArrag [1] = methods [j]; // put the set Method to the 2nd-bit custom container
    }
    }
    // Only attributes are displayed after traversal. If methods are missing, they are not placed in the shadow container.
    If (myMothodArrag [0]! = Null & myMothodArrag [0]! = Null ){
    Hm. put (fieldName, myMothodArrag );
    }

    }
    }

    } Catch (Exception e ){
    System. out. prin

  • 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.