I have one class with name OldClass and one class with name NewClass. Both classes have exact the same fields. Can I copy the values from OldClass to NewClass without calling getters and setters manually for about 100 fields per class (200 such class pairs).
Asked
Active
Viewed 61 times
0
-
2Hint: having classes with 100 fields is a real problem by its own. – GhostCat May 14 '19 at 12:28
-
1Hint 2: if they have the exact same fileds, they should probably be the same class... or one should inherit the other. – TheChubbyPanda May 14 '19 at 12:28
-
Those are entity classes for ORM. I want to copy a file "database" to a real relational database. – Philipp May 14 '19 at 13:02
4 Answers
1
Yes you can. I suggest you take a look at MapStruct. It should be a perfect fit for your requirements.
Just define a mapper interface for your class and let MapStruct create the implementation.
Take a look at http://mapstruct.org/

Jochen Reinhardt
- 833
- 5
- 14
0
Just serialize the object A and deserialize to ObjectB.
ObjectMapper mapper = new ObjectMapper();
String jsonResult = mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(objectA);
TypeReference<ObjectB> typeRef
= new TypeReference<ObjectB>() {};
ObjectB objectB = mapper.readValue(jsonInput, typeRef);

Karthikeyan
- 2,634
- 5
- 30
- 51
0
I'd suggest to use Commons BeanUtils for this task:
BeanUtils.copyProperties(destObj, srcObj);
For this to work both objects must be Java Beans, which boils down to having getters and setters that follow the bean naming convention and a no-args constructor.
Basic example:
import org.apache.commons.beanutils.BeanUtils;
public class BeanUtilsTest {
public static final class Bean1 {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static final class Bean2 {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static void main(String[] args) throws Exception {
final Bean1 bean1 = new Bean1();
final Bean2 bean2 = new Bean2();
bean1.setName("");
BeanUtils.copyProperties(bean2, bean1);
System.out.println("'" + bean2.getName() + "'"); // prints ''
bean1.setName(null);
BeanUtils.copyProperties(bean2, bean1);
System.out.println("'" + bean2.getName() + "'"); // prints 'null'
}
}

dpr
- 10,591
- 3
- 41
- 71
-
@Philipp did this comment belong to my answer? I cannot reproduce the described problem... – dpr May 15 '19 at 08:16
-
@Philipp I still cannot reproduce. I added an example which sets `""` and `null` as expected. – dpr May 15 '19 at 10:25
0
Pure Java SDK Solution:
You can use Java Reflection.
Compiling Example (assumes that field names and types are equal).
import java.lang.reflect.Field;
public class Test {
public static void main(String[] args) throws Exception {
final OldClass oldClass = new OldClass();
oldClass.setA("foo");
oldClass.setB("bar");
final NewClass newClass = new NewClass();
copyAllFields(oldClass, newClass);
System.out.println(newClass); // Prints: NewClass [a=foo, b=bar]
}
private static void copyAllFields(OldClass pOldClass, NewClass pNewClass) throws Exception {
final Field[] fields = pOldClass.getClass().getDeclaredFields();
for (Field fieldOldClass : fields) {
fieldOldClass.setAccessible(true);
final Object fieldValue = fieldOldClass.get(pOldClass);
final Field fieldNewClass = pNewClass.getClass().getDeclaredField(fieldOldClass.getName());
fieldNewClass.setAccessible(true);
fieldNewClass.set(pNewClass, fieldValue);
}
}
}
NewClass.java
public class NewClass {
private String a;
private String b;
@Override
public String toString() {
return "NewClass [a=" + a + ", b=" + b + "]";
}
}
OldClass.java
public class OldClass {
private String a;
private String b;
@Override
public String toString() {
return "OldClass [a=" + a + ", b=" + b + "]";
}
public void setA(String pA) {
a = pA;
}
public void setB(String pB) {
b = pB;
}
}

Marteng
- 1,179
- 13
- 31
-
Doesn't work as setAccessible is ignored. Can't Access private member. – Philipp May 15 '19 at 07:55
-
@Philipp you're right. I updated and tested my example. In `#copyAllFields` the call `fieldOldClass.setAccessible(true);` was missing (before reading the value from the field). – Marteng May 15 '19 at 08:03