Reputation: 35
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).
Upvotes: 0
Views: 72
Reputation: 10972
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'
}
}
Upvotes: 0
Reputation: 1305
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;
}
}
Upvotes: 0
Reputation: 2724
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);
Upvotes: 0
Reputation: 843
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/
Upvotes: 1