-
Notifications
You must be signed in to change notification settings - Fork 1
/
Merge2ObjectOfSameType.java
54 lines (42 loc) · 1.54 KB
/
Merge2ObjectOfSameType.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package org.kur.practice.reflection;
import org.kur.practice.base.Employee;
import java.lang.reflect.Method;
/**
* Created by Keyur on 26/7/17.
* This class demonstrates the merge of 2 objects of same type.
*/
public class Merge2ObjectOfSameType {
public static void main(String[] args) {
Employee e1 = new Employee();
Employee e2 = new Employee();
e1.setId(1);
e1.setName("name");
e2.setDesignation("designation");
e2.setSalary(100.0);
merge(e1, e2);
System.out.println("E1:" + e1.toString());
System.out.println("E2:" + e2.toString());
}
private static void merge(Object obj, Object update){
if(!obj.getClass().isAssignableFrom(update.getClass())){
return;
}
Method[] methods = obj.getClass().getMethods();
for(Method fromMethod: methods){
if(fromMethod.getDeclaringClass().equals(obj.getClass())
&& fromMethod.getName().startsWith("get")){
String fromName = fromMethod.getName();
String toName = fromName.replace("get", "set");
try {
Method toMetod = obj.getClass().getMethod(toName, fromMethod.getReturnType());
Object value = fromMethod.invoke(update, (Object[])null);
if(value != null){
toMetod.invoke(obj, value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}