Java Reflection-2 (object replication, parent class domain)

Source: Internet
Author: User

Why would you want to copy an object? Suppose you have a class car that contains Name,color2 properties, then copy the Car1 object to the Car2 object, just Car2.setname (car1.getname) and Car2.setcolor (Car1.getcolor) two.

There are many classes in the actual project have more than dozens of, even hundreds of fields, at this time, if using the above approach, there will be a lot of code workload and repetitive work. The workaround is to use the reflection mechanism.

First, there are 2 classes as follows

 1  /**   2   * Created by yesiming on 16-11-19.  3  */ 4  public  class   Car { 5   Private   String name;  6  private   String color;  7  //  omit set/ Get  8  }  9 } 
1 /** 2 * Created by yesiming on 16-11-20. 3  */ 4  Public class extends car{5     Private String model; 6     // Omit Set/get 7 }

Let's start with the field of the onboarding Kia.

1 /**2 * Object replication through Reflection (vulnerability: cannot replicate domains in parent class)3      * @throwsException4      */5 @Test6      Public voidCopyObject ()throwsexception{7User src =NewUser ();//Source Object8Src.setid (1);9Src.setname ("Yesiming");TenUser target =NewUser ();//target Object OneClass clazz = User.class; Afield[] fields = Clazz.getdeclaredfields ();//The domain where the class declaration is obtained (the domain that does not contain the parent class) -          for(Field field:fields) { -             if(!field.isaccessible ()) theField.setaccessible (true);//Set accessible -Field.set (target, Field.get (SRC));//from the source object get value set to the target object -         } -         //Output Display +          for(Field field:fields) { - System.out.println (Field.get (target)); +         } A}

Because the Getdeclaredfields method cannot get the domain in the superclass, the above operation is flawed, not even meaningful

Here's how to get the domain in the superclass, using recursion to

1 /**2 * outputs all domains, including domains in the parent class (implemented recursively)3      */4      Public voidShowallfield (Class clazz) {5field[] Fields =clazz.getdeclaredfields ();6SYSTEM.OUT.PRINTLN ("------" + clazz.tostring () + "------");7         if(Fields! =NULL&& fields.length > 0) {8              for(Field field:fields) {9 System.out.println (field);Ten             } One         } AClass Superclazz =Clazz.getsuperclass (); -         if(Superclazz! = Object.)class) {//End Recursion -Showallfield (Superclazz);//Recursive the         } -     } -  - @Test +      Public voidshowallfieldtest () { -Class clazz = Kia.class; + Showallfield (clazz); A}

The results of the operation are as follows:

------Class O1.o1_a.Kia------Private java.lang.String O1.o1_a.Kia.model------class O1.o1_a.Car------Private Java.lang.String o1.o1_a.Car.nameprivate java.lang.String O1.o1_a.Car.color

In combination with the above 2 operations, we can write a class copy method with practical value.

Java Reflection-2 (object replication, parent class domain)

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.