0

I've two objects objclient and objserver of the same type Object,(i.e. Dog, Cat..)

when I receive objclient in my endpoint I need to replace its attributes with the non-null ones in objserver without doing explicitly, for example :

private void eraser(Object clientObject, Object serverObject){
  //set only non null attributes of serverObject to clientObject
}
Rakib Hasan
  • 317
  • 1
  • 5
  • What's the question? – tbjorch May 06 '22 at 13:39
  • how to do it does reflect.Field is the best choice ? – Abdessalam Idali Lahcen May 06 '22 at 13:47
  • 1
    Does this answer your question? [Copy non-null properties from one object to another using BeanUtils or similar](https://stackoverflow.com/questions/41125384/copy-non-null-properties-from-one-object-to-another-using-beanutils-or-similar) – Ashish Patil May 06 '22 at 14:05
  • Another potential duplicate: [Helper in order to copy non null properties from object to another](https://stackoverflow.com/q/1301697/12567365) - not exactly your use-case, but very similar answers which could be adapted, I think. Bottom line: you can use reflection directly, but why not use a tested and widely used library which wraps it up for you? – andrewJames May 06 '22 at 14:22

3 Answers3

0

You can use e.g. ModelMapper if you want to map values between different java objects without explicitly having to map every single field.

tbjorch
  • 1,544
  • 1
  • 8
  • 21
0

BeanUtils.copyProperties is very common in spring-boot projects, we can use it with passing ignoreProperties (null case) & we can find the null case by using a custom method like below:

    private ClientObject eraser(ClientObject clientObject, ServerObject serverObject){
        BeanUtils.copyProperties(serverObject, clientObject, getNullPropertyNames(serverObject));
        return clientObject;
    }

    public static String[] getNullPropertyNames (Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for(java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) emptyNames.add(pd.getName());
        }

        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

It will set only non null attributes of serverObject to clientObject.

Rakib Hasan
  • 317
  • 1
  • 5
0

Actually the best solution comparing to @Rakib's one is:

private void erase(Object clientObject, Object defaultObject) throws IllegalAccessException, NoSuchFieldException {
            Field[] Fields = defaultObject.getClass().getDeclaredFields();
        
            for (Field field : Fields) {
                field.setAccessible(true);
                Object value = field.get(defaultObject);
                if(value !=null)
                    field.set(clientObject,value);
            }
    }