Copying data to the same classes in different packages

I call an external service that returns the following class:

package abc;

public class FirstClass {
   private String name;
   private String age;
   private String number;
}

In my model, I defined a class with the same structure:

package xyz;

public class FirstClass {
   private String name;
   private String age;
   private String number;
}

I want to copy data from an object abc.FirstClassto an object xyz.FirstClass. I do not want to display the data field by field. I think this can be done by dozer - are there any easier ways to do this?

+4
source share
4 answers

You can use PropertyUtils # copyProperties :

"" bean " " bean , ( <20 > ).

:

abc.FirstClass src = new abc.FirstClass();
xyz.FirstClass dest = new xyz.FirstClass();
PropertyUtils.copyProperties(src, dest);
+1

Java Reflection - .

Java โ„ข

- , . java.lang.reflect.Field , โ€‹โ€‹ , , . ,

- :

StackOverflow.abc.firstClass abc = new StackOverflow.abc.firstClass();
StackOverflow.xyz.firstClass xyz = new StackOverflow.xyz.firstClass();

Class<? extends StackOverflow.xyz.firstClass> xyzClass = xyz.getClass();
Field[] fields = abc.getClass().getDeclaredFields();
for (Field abcField : fields) {
    abcField.setAccessible(true); //To access private fields
    try {
        Field xyzField = xyzClass.getDeclaredField(abcField.getName());
        xyzField.setAccessible(true);
        xyzField.set(xyz, abcField.get(abc));
    } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException ex) {
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
    }
}

tutorial Oracle . , , .

+1

The reason you cannot copy the entire class object is because each class object has a unique hash code. Even if your class has the same attribute.

I know this is pain, but field mapping is the only way. You can try using spring beans to display the fields.

+1
source

You can use reflection

this does the job and shows it:

public class FirstClass {
       private String name;
       private String age;
       private String number;

       public FirstClass(String _name, String _age, String _number)
       {
           name=_name; age=_age; number=_number;
       }

    }


public class CloneClass {
    private String name;
    private String age;
    private String number;

       public CloneClass(String _name, String _age, String _number)
       {
           name=_name; age=_age; number=_number;
       }

       public void show()
       {
          System.out.println("NAME="+name+" AGE="+age+" NUMBER="+number);
       }

    }


    FirstClass A=new FirstClass("Jules","44","123A4535");

    CloneClass B=new CloneClass("","","");

    Class class1=A.getClass();
    Class class2=B.getClass();

    // all fields from A

    Field[] fields_A = class1.getDeclaredFields();
    Field[] fields_B = class2.getDeclaredFields();

    for (int k=0;k<fields_A.length;k++)
    {
        Field one_field=fields_A[k];

        // Name of field in source
        String name_of_field=one_field.getName();

        if (name_of_field.equals("this$0")) continue; // Not this !

        // Search if it exists in destination
        for (int z=0;z<fields_B.length;z++)
            {
            Field field_destination=fields_B[k];
            String name_of_field2=field_destination.getName();

            if (name_of_field.equals(name_of_field2))
            // TODO
            // You should also verify the type !
                {

                try
                {
                // To read private var
                one_field.setAccessible(true);
                field_destination.setAccessible(true);

                Object value=one_field.get( A);
                field_destination.set(B, value);
                }
                catch (Exception ex) {System.err.println(ex);}
                }
                }
            } // for (int k=0;k<fields_A.length;k++)

    B.show();
0
source

Source: https://habr.com/ru/post/1618492/


All Articles