Is there a nice way to iterate over object fields using reflection?
The main problem is in object can be another object therefore it's needed to iterate over another object's properties too.
For example I have AllInclusiveDetails object
public class AllInclusiveDetails {
#JsonProperty("renters_business")
private String rentersBusiness;
#Valid
#NotNull
#JsonProperty("main_property_owner_details")
private ShortCustomer mainPropertyOwnerDetails;
#Valid
#NotNull
#JsonProperty("main_tenant_details")
private ShortCustomer mainTenantDetails;
}
And ShortCustomer is
public class ShortCustomer {
#NotNull
#Positive
private Long id;
#NotEmpty
#JsonProperty("full_name")
private String fullName;
#JsonProperty("organization_number")
private String organizationNumber;
#PastOrPresent
private LocalDate birthdate;
}
I want to iterate over AllInclusiveDetails object fields using reflection and if there is another object in it , I want to iterate over that object fields too.
The main purpose is to track if value of the same field in two different object are equal or not and if not to save old value and the new one.
Does this satisfy your requirement:
for(Field field : AllInclusiveDetails.class.getDeclaredFields()) {
if(field.getType()== ShortCustomer.class) {
//Do your logic here
}
}
Here's a way to get all the fields of a class and a method to recursively use reflection to compare fields. Play around with main to test it, it will not work correctly for Objects that are logically equivalent but not the same in memory.
// Gathers all fields of this class, including those in superclasses, regardless of visibility
public static List<Field> getAllFields(Class<?> klass) {
List<Field> fields = new ArrayList<>();
for (Class<?> k = klass; k != null; k = k.getSuperclass()) {
fields.addAll(Arrays.asList(k.getDeclaredFields()));
}
return fields;
}
// Uses reflection and recursion to deep compare two objects.
// If the sub-fields and sub-arrays are not deeply equal this will return false.
// This will cause problems with data structures that may be logically equivalent
// but not have the same structure in memory, HashMaps and Sets come to mind
//
// Also might perform illegal reflective access which gets a warning from the JVM
// WARNING: Illegal reflective access ... to field java.util.LinkedList.size
// WARNING: Please consider reporting this to the maintainers ...
// WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
// WARNING: All illegal access operations will be denied in a future release
public static <T> boolean reflexiveEquals(T o1, T o2) {
return reflexiveEquals(o1, o2, new HashSet<>(), new HashSet<>());
}
private static <T> boolean reflexiveEquals(T o1, T o2, Set<Object> o1Refs, Set<Object> o2Refs) {
if (o1 == o2) {
// exact same object or both are null
return true;
}
if (o1 == null || o2 == null) {
// one is null but the other is not
System.err.println(o1 + " != " + o2);
return false;
}
Class<?> type = o1.getClass();
if (type != o2.getClass()) {
// not the exact same class therefore not equal
// you could treat this differently if you want
System.err.println(type + " != " + o2.getClass());
return false;
}
if (PRIMITIVE_WRAPPERS.contains(type)) {
// if it's a primitive wrapper then compare plainly
boolean result = Objects.equals(o1, o2);
if (!result) {
System.err.println("Objects.equals: " + o1 + " : " + o2);
}
return result;
}
// before descending, make sure there wont be an infinite loop
// if this object appeared in the reference chain before
// then it is currently being compared lower in the stack,
// return true to let it finish it's comparison
if (o1Refs.contains(o1) || o2Refs.contains(o2)) {
return true;
}
try {
// keep track of the objects that have been descended into
o1Refs.add(o1);
o2Refs.add(o2);
if (type.isArray()) {
// if its an array, compare all elements
try {
Object[] a1 = (Object[]) o1;
Object[] a2 = (Object[]) o2;
// only comparable field besides elements
if (a1.length != a2.length) {
System.err.println("Array length diff");
return false;
}
for (int i = 0; i < a1.length; i++) {
if (!reflexiveEquals(a1[i], a2[i], o1Refs, o2Refs)) {
return false;
}
}
return true;
} catch (Exception e) {
return false;
}
}
// otherwise its some other object so compare all fields
// moving up the super-classes as well
for (Class<?> k = type; k != null; k = k.getSuperclass()) {
for (Field f : k.getDeclaredFields()) {
try {
f.setAccessible(true);
if (!reflexiveEquals(f.get(o1), f.get(o2), o1Refs, o2Refs)) {
return false;
}
} catch (IllegalArgumentException | IllegalAccessException e) {
return false;
}
}
}
return true;
} finally {
// remove the references since their compare is complete
o1Refs.remove(o1);
o2Refs.remove(o2);
}
}
private static final Set<Class<?>> PRIMITIVE_WRAPPERS = getPrimitiveWrapperClasses();
private static final Set<Class<?>> getPrimitiveWrapperClasses() {
Set<Class<?>> set = new HashSet<>();
set.add(Boolean.class);
set.add(Character.class);
set.add(Byte.class);
set.add(Short.class);
set.add(Integer.class);
set.add(Long.class);
set.add(Float.class);
set.add(Double.class);
set.add(Void.class);
return set;
}
public static class AllInclusiveDetails {
private String rentersBusiness;
private ShortCustomer mainPropertyOwnerDetails;
private ShortCustomer mainTenantDetails;
private ShortCustomer[] arr;
private List<ShortCustomer> list;
}
public static class ShortCustomer {
private Long id;
private String fullName;
private String organizationNumber;
private LocalDate birthdate;
}
public static void main(String[] args) {
AllInclusiveDetails aids1 = new AllInclusiveDetails();
aids1.rentersBusiness = "Business";
aids1.mainTenantDetails = new ShortCustomer();
aids1.mainTenantDetails.id = 1L;
aids1.mainTenantDetails.fullName = "John Doe";
aids1.arr = new ShortCustomer[] {
aids1.mainTenantDetails,
aids1.mainPropertyOwnerDetails };
aids1.list = new LinkedList<>(Arrays.asList(aids1.arr));
AllInclusiveDetails aids2 = new AllInclusiveDetails();
aids2.rentersBusiness = "Business";
aids2.mainTenantDetails = new ShortCustomer();
aids2.mainTenantDetails.id = 1L;
aids2.mainTenantDetails.fullName = "John Doe";
aids2.arr = new ShortCustomer[] {
aids2.mainTenantDetails,
aids2.mainPropertyOwnerDetails };
aids2.list = new LinkedList<>(Arrays.asList(aids2.arr));
System.out.println(reflexiveEquals(aids1, aids2));
}
Related
I have a method from which I need to return two values.I m confused as to how can I return two values.
public List<Class1> getCode(Long Code)
{
String Query1="Some Query";
List<Object[]> value = repos.getQuery(Query1);
List<Class1> counts = new ArrayList<>();
if (null != value)
{
Iterator<Object[]> rowItr = value.iterator();
while (rowItr.hasNext())
{
Class1 count = new Class1();
Object[] obj = rowItr.next();
if (null != obj)
{
if (null != obj[0])
{
count.setValuess1(obj[0].toString());
}
if (null != obj[1])
{
count.setValuess2(obj[1].toString());
}
}
counts.add(count);
return (List<Class1>) counts;
}
String Query2="SomeQuery" ;
List<Object[]> value2 = repos.getQuery(Query2);
List<Class2> count1s = new ArrayList<>();
if (null != value2)
{
Iterator<Object[]> rowItr1 = value2.iterator();
while (rowItr.hasNext())
{
Class2 countt = new Class2();
Object[] obj1 = rowItr1.next();
if (null != obj1)
{
if (null != obj1[0])
{
countt.setValuess1(obj1[0].toString());
}
if (null != obj1[1])
{
countt.setValuess2(Long.valueOf(obj1[1].toString()));
}
}
count1s.add(countt);
}
}
return (List<Class2>)count1s;
}
}
This is my Class1
public class1
{
private String valuess1;
private String valuess2;
private List<Class2>class2;
}
This is My Class2
public class Class2
{
private String valuess1;
private Long valuess2;
}
How can I return count1s and counts together .I have tried returning the value by the use of casting but it does not accept it.I have seen quiet a few solutions but none of them has worked for me.Any help would be appreciated.
You can return a Pair.
Pair<List<Class1>,List<Class2>> res = new Pair(counts, count1s);
return res;
Or you can create a class that represents the return values and return it.
public class Res {
public List<Class1> l1;
public List<Class2> l2;
public Res(List<Class1> l1, List<Class2> l2){
this.l1 = l1;
this.l2 = l2;
}
}
Whenever you want to return more than one values, you better return an array holding values you want, so when you call you can initialize the returned array and the loop through it. Hope that helps
I want to have an Enum like this:
public enum Type {
STRING, INTEGER, BOOLEAN, LIST(Type);
Type t;
Type() { this.t = this; )
Type(Type t) { this.t = t; }
}
Such that I can enter various Types for LIST, like being able to call Type.LIST(STRING). Is this possible in Java?
enums are limited, you can't have an unknown amount of entries. So you can't have LIST(LIST(LIST(LIST(...))) as a separate Type enum. You'll need a class, but that doesn't mean you have to instantiate lots of objects necessarily:
It may be premature optimization, but you can use a flyweight pattern to ensure that you can't get more than one instance of a Type:
package com.example;
public final class Type {
public enum LeafType {
STRING,
INTEGER,
BOOLEAN
}
//Gives you the familiar enum syntax
public static final Type STRING = new Type(LeafType.STRING);
public static final Type INTEGER = new Type(LeafType.INTEGER);
public static final Type BOOLEAN = new Type(LeafType.BOOLEAN);
private final LeafType leafType;
private final Type listType;
private final Object lock = new Object();
// This is the cache that prevents creation of multiple instances
private Type listOfMeType;
private Type(LeafType leafType) {
if (leafType == null) throw new RuntimeException("X");
this.leafType = leafType;
listType = null;
}
private Type(Type type) {
leafType = null;
listType = type;
}
/**
* Get the type that represents a list of this type
*/
public Type list() {
synchronized (lock) {
if (listOfMeType == null) {
listOfMeType = new Type(this);
}
return listOfMeType;
}
}
public boolean isList() {
return listType != null;
}
/**
* If this type is a list, will return what type of list it is
*/
public Type getListType() {
if (!isList()) {
throw new RuntimeException("Not a list");
}
return listType;
}
/**
* If this type is a leaf, will return what type of leaf it is
*/
public LeafType getLeafType() {
if (isList()) {
throw new RuntimeException("Not a leaf");
}
return leafType;
}
#Override
public String toString() {
if (isList()) {
return "LIST(" + getListType() + ")";
}
return getLeafType().toString();
}
}
Usage:
Simple type:
Type string = Type.STRING;
List:
Type stringList = Type.STRING.list();
List of list:
Type stringListList = Type.STRING.list().list();
And you can never get in the situation where you have two instances of Type that describe the same type, e.g.:
Type t1 = Type.BOOLEAN.list().list().list();
Type t2 = Type.BOOLEAN.list().list().list();
System.out.println(t1 == t2 ? "Same instance" : "Not same instance");
I added toString for debugging:
Type listListListInt = Type.INTEGER.list().list().list();
System.out.println(listListListInt);
Gives:
LIST(LIST(LIST(INTEGER)))
My objective is to Dynamically insert values in to the Linked List. And thereafter, I want to perform sorting or search algorithms on the List.
In addition to it, I am creating class at runtime (based on user input) using Reflection.
Thereafter, I use data provided by the user in JSON Array, to create instances of the class, and then I insert the instances in to the GenericList.
Following is the code for the Generic Linked List.
public class LinkedListNode<T> implements Serializable {
private T value;
private LinkedListNode<T> next;
public LinkedListNode(T value) {
this.value = value;
}
public void setNext(LinkedListNode<T> next) {
this.next = next;
}
public LinkedListNode<T> getNext() {
return next;
}
public T getValue() {
return value;
}
}
public class GenericList<T> implements Serializable {
private LinkedListNode<T> first = null;
public void insert(LinkedListNode<T> node) {
node.setNext(first);
first = node;
}
public void emptyList(){
first = null;
}
public void remove(){
if(first.getNext()!=null)
first = first.getNext();
else first = null;
}
}
And this is how I create instances of the class and insert it to the GenericList.
//dataToInsert => is the JSONArray. => [{field1:"value1",field2:"value1"},{field1:"value2",field2:"value2"},{field1:"value3",field2:"value3"}]
//classLoaded => package com.LinkedAnalyzerAdapter.saveTestClasses; public class order implements java.io.Serializable {public String field1;public String field2;}
Class<?> classLoaded = classLoader.loadClass("com.LinkedAnalyzerAdapter.saveTestClasses.order");
GenericList<Object> list = new GenericList<Object>();
for (int i = 0; i < dataToInsert.length(); i++) {
JSONObject jsonObj = new JSONObject();
jsonObj = dataToInsert.getJSONObject(i);
Object obj = classLoaded.newInstance();
Field[] fs = classLoaded.getDeclaredFields();
for (Field field : fs)
{
field.setAccessible(true);
Object fieldValue = jsonObj.get(field.getName());
field.set(obj, fieldValue);
}
list.insert(new LinkedListNode<Object>(obj));
}
I am successfully able to insert data in to GenericList, but after inserting I later want to sort the data based on field1, in the ascending order.
I have spent hours to solve it but unable to successfully accomplish sorting.
You should really use java.util.LinkedList instead of your own GenericList, to take advantage of the built in Collections
LinkedList<LinkedListNode<?>> list = new LinkedList<>();
Collections.sort(list, new Comparator<String>() {
#Override
public int compare(String o1, String o2) {
return ...
}
}
Used the following code to resolve the issue.
public void sortLinkedList(final String fieldToCompare){
Collections.sort(testList, new Comparator<LinkedListNode>() {
#Override
public int compare(LinkedListNode arg0, LinkedListNode arg1) {
// TODO Auto-generated method stub
Field[] fs = classtoLoad.getDeclaredFields();
for (Field field : fs){
field.setAccessible(true);
Object fieldName = field.getName();
if(fieldToCompare.equalsIgnoreCase((String) fieldName)){
try {
String value1 = (String) field.get(arg0.getValue());
String value2 = (String) field.get(arg1.getValue());
return value1.compareToIgnoreCase(value2);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
}
return 0;
}
});
}
I have a UserProfile class which contains user's data as shown below:
class UserProfile {
private String userId;
private String displayName;
private String loginId;
private String role;
private String orgId;
private String email;
private String contactNumber;
private Integer age;
private String address;
// few more fields ...
// getter and setter
}
I need to count non null fields to show how much percentage of the profile has been filled by the user. Also there are few fields which I do not want to consider in percentage calculation like: userId, loginId and displayName.
Simple way would be to use multiple If statements to get the non null field count but it would involve lot of boiler plate code and there is another class Organization for which I need to show completion percentage as well. So I created a utility function as show below:
public static <T, U> int getNotNullFieldCount(T t,
List<Function<? super T, ? extends U>> functionList) {
int count = 0;
for (Function<? super T, ? extends U> function : functionList) {
count += Optional.of(t).map(obj -> function.apply(t) != null ? 1 : 0).get();
}
return count;
}
And then I call this function as shown below:
List<Function<? super UserProfile, ? extends Object>> functionList = new ArrayList<>();
functionList.add(UserProfile::getAge);
functionList.add(UserProfile::getAddress);
functionList.add(UserProfile::getEmail);
functionList.add(UserProfile::getContactNumber);
System.out.println(getNotNullFieldCount(userProfile, functionList));
My question is, is this the best way I could count not null fields or I could improve it further. Please suggest.
You can simply a lot your code by creating a Stream over the given list of functions:
public static <T> long getNonNullFieldCount(T t, List<Function<? super T, ?>> functionList) {
return functionList.stream().map(f -> f.apply(t)).filter(Objects::nonNull).count();
}
This will return the count of non-null fields returned by each function. Each function is mapped to the result of applying it to the given object and null fields are filtered out with the predicate Objects::nonNull.
I wrote a utility class to get the total count of readable properties and the count of non null values in an object. The completion percentage can be calculated based on these.
It should work pretty well with inherited properties, nested properties, (multi-dimensional) iterables and maps.
I couldn't include the tests as well in here, because of the character limit, but here's the utility class:
import lombok.*;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.*;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class PropertyCountUtils {
/***
* See {#link #getReadablePropertyValueCount(Object, Set)}.
*/
public static PropertyValueCount getReadablePropertyValueCount(#NonNull Object object) {
return getReadablePropertyValueCount(object, null);
}
/**
* Counts the properties of the given object, including inherited and nested properties,
* returning the total property count and the count of properties with assigned values.
*
* <p>
* Properties with assigned values have a value meeting all conditions below:
* <ul>
* <li>different from null</li>
* <li>different from an empty iterable or an empty map</li>
* <li>different from an iterable containing only null values</li>
* <li>different from a map containing only null values.</li>
* </ul>
* For multidimensional Iterables and Maps, these conditions apply to each dimension.
* </p>
*
* #param object The object to inspect. It should not be null.
* #param ignoredProperties The properties to ignore or null.
* For nested properties, use dot as a separator: "property1.nestedProperty.nestedProperty2"
* #return A pair of `assignedValueCount` (properties with assigned value) and `totalCount` (total property count).
*/
public static PropertyValueCount getReadablePropertyValueCount(
#NonNull Object object, Set<String> ignoredProperties) {
PropertyValueCount countHolder = new PropertyValueCount();
processReadablePropertyValueCount(countHolder, object, ignoredProperties, null);
return countHolder;
}
/***
* #return true if the object had at least one non-null property value or no readable properties.
* <p>
* If the object is an instance of String, for example, it would have no readable nested properties.
* Also, if the object is an instance of some class for which all nested properties are ignored,
* the method would return true, since the object itself has a non-null value,
* but the caller decided to ignore all properties.
* </p>
*/
#SneakyThrows
private static boolean processReadablePropertyValueCount(
PropertyValueCount countHolder, #NonNull Object object, Set<String> ignoredProperties, String parentPath) {
boolean objectHasAssignedProperties = false;
boolean objectHasNoReadableProperties = true;
List<Field> fields = getAllDeclaredFields(object.getClass());
for (Field field : fields) {
String fieldPath = buildFieldPath(parentPath, field);
Method readMethod = getReadMethod(object.getClass(), ignoredProperties, field, fieldPath);
if (readMethod == null) {
continue;
}
countHolder.setTotalCount(countHolder.getTotalCount() + 1);
objectHasNoReadableProperties = false;
Object value = readMethod.invoke(object);
if (value == null || isCollectionWithoutAnyNonNullValue(value)) {
// no assigned value, so we'll just count the total available properties
int readablePropertyValueCount = getReadablePropertyCount(
readMethod.getGenericReturnType(), ignoredProperties, fieldPath);
countHolder.setTotalCount(countHolder.getTotalCount() + readablePropertyValueCount);
} else if (value instanceof Iterable<?> iterable) {
processPropertyValueCountInIterable(countHolder, ignoredProperties, fieldPath, iterable);
} else if (value instanceof Map<?, ?> map) {
processPropertyValueCountInIterable(countHolder, ignoredProperties, fieldPath, map.values());
} else {
countHolder.setAssignedValueCount(countHolder.getAssignedValueCount() + 1);
// process properties of nested object
processReadablePropertyValueCount(countHolder, value, ignoredProperties, fieldPath);
objectHasAssignedProperties = true;
}
}
return objectHasAssignedProperties || objectHasNoReadableProperties;
}
private static void processPropertyValueCountInIterable(
PropertyValueCount countHolder, Set<String> ignoredProperties, String fieldPath, Iterable<?> iterable) {
boolean iterableHasNonNullValues = false;
// process properties of each item in the iterable
for (Object value : iterable) {
if (value != null) {
// check if the current iterable item is also an iterable itself
Optional<Iterable<?>> nestedIterable = getProcessableCollection(value);
if (nestedIterable.isPresent()) {
processPropertyValueCountInIterable(countHolder, ignoredProperties, fieldPath, nestedIterable.get());
} else {
iterableHasNonNullValues = processReadablePropertyValueCount(
countHolder, value, ignoredProperties, fieldPath);
}
}
}
// consider the iterable as having an assigned value only if it contains at least one non-null value
if (iterableHasNonNullValues) {
countHolder.setAssignedValueCount(countHolder.getAssignedValueCount() + 1);
}
}
#SneakyThrows
private static int getReadablePropertyCount(
#NonNull Type inspectedType, Set<String> ignoredProperties, String parentPath) {
int totalReadablePropertyCount = 0;
Class<?> inspectedClass = getTargetClassFromGenericType(inspectedType);
List<Field> fields = getAllDeclaredFields(inspectedClass);
for (Field field : fields) {
String fieldPath = buildFieldPath(parentPath, field);
Method readMethod = getReadMethod(inspectedClass, ignoredProperties, field, fieldPath);
if (readMethod != null) {
totalReadablePropertyCount++;
Class<?> returnType = getTargetClassFromGenericType(readMethod.getGenericReturnType());
// process properties of nested class, avoiding infinite loops
if (!hasCircularTypeReference(inspectedClass, returnType)) {
int readablePropertyValueCount = getReadablePropertyCount(
returnType, ignoredProperties, fieldPath);
totalReadablePropertyCount += readablePropertyValueCount;
}
}
}
return totalReadablePropertyCount;
}
// In case the object being analyzed is of parameterized type,
// we want to count the properties in the class of the parameter, not of the container.
private static Class<?> getTargetClassFromGenericType(Type type) {
if (type instanceof ParameterizedType parameterizedType) {
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
if (actualTypeArguments.length > 0) {
// Inspect the last parameter type.
// For example, lists would only have one parameter type,
// but in the case of maps we would inspect the parameter representing the entry value, not the entry key.
Type inspectedTypeArgument = actualTypeArguments[actualTypeArguments.length - 1];
return inspectedTypeArgument instanceof ParameterizedType ?
getTargetClassFromGenericType(inspectedTypeArgument) :
(Class<?>) inspectedTypeArgument;
}
}
return type instanceof Class<?> ? (Class<?>) type : type.getClass();
}
private static List<Field> getAllDeclaredFields(#NonNull Class<?> inspectedClass) {
List<Field> fields = new ArrayList<>();
Collections.addAll(fields, inspectedClass.getDeclaredFields());
Class<?> superClass = inspectedClass.getSuperclass();
while (superClass != null) {
Collections.addAll(fields, superClass.getDeclaredFields());
superClass = superClass.getSuperclass();
}
return fields;
}
private static Method getReadMethod(#NonNull Class<?> inspectedClass, Set<String> ignoredProperties, Field field, String fieldPath) {
if (ignoredProperties != null && ignoredProperties.contains(fieldPath)) {
return null;
}
PropertyDescriptor propertyDescriptor;
try {
propertyDescriptor = new PropertyDescriptor(field.getName(), inspectedClass);
} catch (IntrospectionException e) {
// statement reached when the field doesn't have a getter
return null;
}
return propertyDescriptor.getReadMethod();
}
private static boolean hasCircularTypeReference(Class<?> propertyContainerClass, Class<?> propertyType) {
return propertyContainerClass.isAssignableFrom(propertyType);
}
private static String buildFieldPath(String parentPath, Field field) {
return parentPath == null ? field.getName() : parentPath + "." + field.getName();
}
private static boolean isCollectionWithoutAnyNonNullValue(Object value) {
Stream<?> stream = null;
if (value instanceof Iterable<?> iterable) {
stream = StreamSupport.stream(iterable.spliterator(), false);
} else if (value instanceof Map<?, ?> map) {
stream = map.values().stream();
}
return stream != null &&
stream.noneMatch(item -> item != null && !isCollectionWithoutAnyNonNullValue(item));
}
private static Optional<Iterable<?>> getProcessableCollection(Object value) {
if (value instanceof Iterable<?> iterable) {
return Optional.of(iterable);
} else if (value instanceof Map<?, ?> map) {
return Optional.of(map.values());
}
return Optional.empty();
}
#Data
#NoArgsConstructor
#AllArgsConstructor
#Builder
public static class PropertyValueCount {
private int assignedValueCount;
private int totalCount;
}
}
The completion percentage can be calculated like this:
PropertyCountUtils.PropertyValueCount propertyValueCount = getReadablePropertyValueCount(profile);
BigDecimal profileCompletionPercentage = BigDecimal.valueOf(propertyValueCount.getNonNullValueCount())
.multiply(BigDecimal.valueOf(100))
.divide(BigDecimal.valueOf(propertyValueCount.getTotalCount()), 2, RoundingMode.UP)
.stripTrailingZeros();
I have two objects of same type.
Class A {
String a;
List b;
int c;
}
A obj1 = new A();
A obj2 = new A();
obj1 => {a = "hello"; b = null; c = 10}
obj2 => {a = null; b = new ArrayList(); c = default value}
Can you please let me know what is the best way to combine this objects into single object?
obj3 = {a = "hello"; b = (same arraylist from obj2); c = 10}
This works as long as you have POJOs with their own getters and setters. The method updates obj with non-null values from update. It calls setParameter() on obj with the return value of getParameter() on update:
public 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();
}
}
}
}
I am using Spring Framework. I was facing the same issue on a project.
To solve it i used the class BeanUtils and the above method,
public static void copyProperties(Object source, Object target)
This is an example,
public class Model1 {
private String propertyA;
private String propertyB;
public Model1() {
this.propertyA = "";
this.propertyB = "";
}
public String getPropertyA() {
return this.propertyA;
}
public void setPropertyA(String propertyA) {
this.propertyA = propertyA;
}
public String getPropertyB() {
return this.propertyB;
}
public void setPropertyB(String propertyB) {
this.propertyB = propertyB;
}
}
public class Model2 {
private String propertyA;
public Model2() {
this.propertyA = "";
}
public String getPropertyA() {
return this.propertyA;
}
public void setPropertyA(String propertyA) {
this.propertyA = propertyA;
}
}
public class JustATest {
public void makeATest() {
// Initalize one model per class.
Model1 model1 = new Model1();
model1.setPropertyA("1a");
model1.setPropertyB("1b");
Model2 model2 = new Model2();
model2.setPropertyA("2a");
// Merge properties using BeanUtils class.
BeanUtils.copyProperties(model2, model1);
// The output.
System.out.println("Model1.propertyA:" + model1.getPropertyA(); //=> 2a
System.out.println("Model1.propertyB:" + model1.getPropertyB(); //=> 1b
}
}
Maybe something like
class A {
String a;
List<..> b;
int c;
public void merge(A other) {
this.a = other.a == null ? this.a : other.a;
this.b.addAll(other.b);
this.c = other.c == 0 ? this.c : other.c;
}
}
A a1 = new A();
A a2 = new A();
a1.a = "a prop";
a2.c = 34;
a1.merge(a2);
A.merge might return a new A object instead of modifing current.
Just accommodating boolean sync. and case sensitive(camel notation)
public boolean merge(Object obj){
if(this.equals(obj)){
return false;
}
if(!obj.getClass().isAssignableFrom(this.getClass())){
return false;
}
Method[] methods = obj.getClass().getMethods();
for(Method fromMethod: methods){
if(fromMethod.getDeclaringClass().equals(obj.getClass())
&& (fromMethod.getName().matches("^get[A-Z].*$")||fromMethod.getName().matches("^is[A-Z].*$"))){
String fromName = fromMethod.getName();
String toName ;
if(fromName.matches("^get[A-Z].*")){
toName = fromName.replace("get", "set");
}else{
toName = fromName.replace("is", "set");
}
try {
Method toMetod = obj.getClass().getMethod(toName, fromMethod.getReturnType());
Object value = fromMethod.invoke(this, (Object[])null);
if(value != null){
toMetod.invoke(obj, value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
return true;
}
If you create getters and setters for the attributes, you can use the copyProperties method from Commons BeanUtils.
Add this method to your POJO, then use it like myObject.merge(newObject). It uses generics to loop through your POJO's fields, so you don't mention any field names:
/**
* Fill current object fields with new object values, ignoring new NULLs. Old values are overwritten.
*
* #param newObject Same type object with new values.
*/
public void merge(Object newObject) {
assert this.getClass().getName().equals(newObject.getClass().getName());
for (Field field : this.getClass().getDeclaredFields()) {
for (Field newField : newObject.getClass().getDeclaredFields()) {
if (field.getName().equals(newField.getName())) {
try {
field.set(
this,
newField.get(newObject) == null
? field.get(this)
: newField.get(newObject));
} catch (IllegalAccessException ignore) {
// Field update exception on final modifier and other cases.
}
}
}
}
}
There is a dynamic solution to merge any two objects which require Reflection and Recursion.
public <T> T merge(T local, T remote, ArrayList<String> listOfClass)
throws IllegalAccessException, InstantiationException {
Class<?> clazz = local.getClass();
Object merged = clazz.newInstance();
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
Object localValue = field.get(local);
Object remoteValue = field.get(remote);
if (localValue != null) {
if (listOfClass.contains(localValue.getClass().getSimpleName())) {
field.set(merged, this.merge(localValue, remoteValue, listOfClass));
} else {
field.set(merged, (remoteValue != null) ? remoteValue : localValue);
}
} else if (remoteValue != null) {
field.set(merged, remoteValue);
}
}
return (T) merged;
}
Variable Description:
local: The object on to which the other will be merged
remote: The object which will be merged to the local object
listOfClass: The ArrayList of custom classes in the given object
The function returns a merged object which is good to go.
Kudos! :)
In your very special case it looks like you want a new object that takes the real values from both instances. Here is an implementation that will do that. The method should be add to class A so that it can access the fields.
public A specialMergeWith(A other) {
A result = new A();
result.a = (a == null ? other.a : a);
result.b = (b == null ? other.b : b);
result.c = (c == DEFAULT_VALUE ? other.c : c);
return result;
}
public static Object mergeObjects(Object source, Object target) throws Exception {
Field[] allFields = source.getClass().getDeclaredFields();
for (Field field : allFields) {
if(Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers())){
continue;
}
if (!field.isAccessible() && Modifier.isPrivate(field.getModifiers()))
field.setAccessible(true);
if (field.get(source) != null) {
field.set(target, field.get(source));
}
}
return target;
}
Using java reflection, support only for the same class.