Utility class for merging POJOs behind a single interface.
Useful in cases where you want to define beans with 'default' values.
For example, given the following bean classes...
public interface IA {
String getX();
void setX(String x);
}
public class A implements IA {
private String x;
public A(String x) {
this.x = x;
}
public String getX() {
return x;
}
public void setX(String x) {
this.x = x;
}
}
The getters will be called in order until the first non-null value is returned...
PojoMerge m;
m = PojoMerge.merge(IA.class, new A("1"), new A("2"));
assertEquals("1", m.getX());
m = PojoMerge.merge(IA.class, new A(null), new A("2"));
assertEquals("2", m.getX());
m = PojoMerge.merge(IA.class, new A(null), new A(null));
assertEquals(null, m.getX());
Notes:
-
Null POJOs are ignored.
-
Non-getter methods are either invoked on the first POJO or all POJOs depending on the
callAllNonGetters
flag
passed into the constructor.
-
For purposes of this interface, a getter is any method with zero arguments and a non-
void
return type.