Merging two objects in Java

前端 未结 8 774
离开以前
离开以前 2020-12-15 23:25

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         


        
相关标签:
8条回答
  • 2020-12-16 00:10

    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! :)

    0 讨论(0)
  • 2020-12-16 00:18

    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;
    }
    
    0 讨论(0)
提交回复
热议问题