Tuesday, 15 July 2014

java - reflection copy non null properties from one object to another BeanUtils -



java - reflection copy non null properties from one object to another BeanUtils -

having this:

public class parent { private string name; private int age; private date birthdate; private work work; static class work{ private int years; private string employer; } // getters , setters public static void main(string[] args) { parent c = new parent; c.setage(55) work work=new parent.work(); work.setemployer("example"); c.setwork(work); //save c in db... } }

i want re-create no-null attributes using reflection. approach described here beanutils works well, copies no-null wrapped object, , not no-null field values:

//fetch c db... parent sameparent= new parent; sameparent.setwork(new parent.work()); //directly http://stackoverflow.com/questions/1301697/helper-in-order-to-copy-non-null-properties-from-object-to-another-java#answer-3521314 beanutilsbean notnull=new nullawarebeanutilsbean(); notnull.copyproperties(c, sameparent);

now, parent c have field age=55. field work.employer null because object work has been overridden. possible modify @override copyproperty method beanutilsbean recursively re-create desired (not null) attributes wrapped objects?

otherwise, know other way?

you can accomplish simple tweaking in nullawarebeanutilsbean. did here check type of property copied , if not primitive type recursively phone call copyproperties method. java auto-boxing converts primitives wrapper classes i'm using set identify primitive types. alternatively check class types of packages idetify custom objects , recursive calls custom , avoid java objects string, date etc. note class not handle primitive values. if age in target set toa value , age in source not initializes it'll still overwrite. illustration if age in source set 20 , used didnt set age new object re-create overwrite value 20 0. may need set additional logic within if-primitive check.

import java.lang.reflect.invocationtargetexception; import java.util.hashset; import java.util.set; import org.apache.commons.beanutils.beanutilsbean; public class nullawarebeanutilsbean extends beanutilsbean { private static final set<class<?>> primitivetypes = new hashset<class<?>>( arrays.aslist(boolean.class, character.class, byte.class, short.class, short.class, integer.class, long.class, float.class, double.class, void.class, string.class, date.class)); private static final set<class<?>> primitivetypes = new hashset<class<?>>() { { add(boolean.class); add(character.class); add(byte.class); add(short.class); add(integer.class); add(long.class); add(float.class); add(double.class); add(void.class); } }; @override public void copyproperty(object dest, string name, object value) throws illegalaccessexception, invocationtargetexception { if (value == null) return; if (primitivetypes.contains(value.getclass())) { super.copyproperty(dest, name, value); } else { seek { object childobj = getpropertyutils().getsimpleproperty(dest, name); if (childobj == null || orig instanceof list && !((list)orig).isempty() && ((list)orig).get(0).getclass().isenum()) { childobj=orig; }else{ copyproperties(childobj, orig); } super.copyproperty(dest, name, childobj); } grab (nosuchmethodexception e) { e.printstacktrace(); } } } }

java apache-commons-beanutils

No comments:

Post a Comment