-
Notifications
You must be signed in to change notification settings - Fork 1
/
CopyOneAobjectToAnother.java
37 lines (29 loc) · 1.33 KB
/
CopyOneAobjectToAnother.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
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import java.lang.reflect.Field;
import java.util.Collection;
public class MyBeansUtil<T> {
public T copyNonNullProperties(T target, T source) {
if (source == null || target == null || target.getClass() != source.getClass()) return null;
final BeanWrapper src = new BeanWrapperImpl(source);
final BeanWrapper trg = new BeanWrapperImpl(target);
for (final Field property : target.getClass().getDeclaredFields()) {
Object srcObject = src.getPropertyValue(property.getName());
if(srcObject instanceof Number) {
Number srcNumber = (Number)srcObject;
Number trgNumber = (Number)trg.getPropertyValue(property.getName());
if(srcNumber.doubleValue() != 0.0 && trgNumber.doubleValue() == 0.0) {
trg.setPropertyValue(
property.getName(),
srcObject);
}
}
else if (srcObject != null && !(srcObject instanceof Number) && !(srcObject instanceof Collection<?>)) {
trg.setPropertyValue(
property.getName(),
srcObject);
}
}
return target;
}
}