views:

315

answers:

4

Hi,

See the following class

public class Parent {

    private String name;
    private int age;
    private Date birthDate;

    // getters and setters   

}

Suppose i have created a parent object as follows

Parent parent = new Parent();

parent.setName("A meaningful name");
parent.setAge(20);

Notice according to code above birthDate property is null. Now i want to copy ONLY non null properties from parent object to another. Something like

SomeHelper.copyNonNullProperties(parent, anotherParent);

I need it because i want to update anotherParent object without overrides its non null with null values.

Do you know some helper like this one ?

I accept minimal code as answer whether no helper in mind

regards,

A: 

sure - use reflection: http://java.sun.com/developer/technicalArticles/ALT/Reflection/

andersonbd1
A: 

This sounds like a job for .... ReflectionMan! (I hope that's not anybody's userid here).

There's an excellent article about a bean copier here: http://codemonkeyism.com/beautiful-java-reflection-and-the-beancopier .

Another possibility seems to be Dozer: http://dozer.sourceforge.net/

CPerkins
Incidentally, I'm having trouble with the form of URL here that allows for an alias: OpenSquareBracket URL CloseSquareBracket OpenSquareBracket alias CloseSquareBracket doesn't seem to work.
CPerkins
+1  A: 

you can use Apache Common BeanUtils, more specifically the copyProperties helper in BeanUtils class:

 BeanUtils.copyProperties(parent, anotherParent);

however why do you want copy only non-null properties? if a property in parent is null, by simply copying it you have null also in anotherParent right?

Just guessing... you want to update a bean with another bean?

dfa
Yes, i want to update another bean without overrides its non null values.
Arthur Ronald F D Garcia
+1 for a sane suggestion for using libraries... sometimes I feel like we're the lone voices in the wilderness on this one
skaffman
Hi, sorry but it does not work. If i have a non null anotherParent.getBirthDate() and i call BeanUtils.copyProperties(parent, anotherParent) anotherParent.getBirthDate() will return null
Arthur Ronald F D Garcia
+1  A: 

I Supopse you already have a solution, since a lot of time has happened since you asked. However, it is not marked as solved, and maybe I can help other users.

Have you tried by defining a subclass of the BeanUtilsBean of the org.commons.beanutils package? Actually, BeanUtils uses this class, so this is an improvement of the solution propsed by dfa. Checking at the source code of that class (http://www.docjar.com/html/api/org/apache/commons/beanutils/BeanUtilsBean.java.html), I think you can overwrite the copyProperty method, by checking for null values and doing nothing if the value is null.

Something like this:

package foo.bar.copy;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtilsBean;

public class NullAwareBeanUtilsBean extends BeanUtilsBean{

    @Override
    public void copyProperty(Object dest, String name, Object value)
            throws IllegalAccessException, InvocationTargetException {
        if(value==null)return;
        super.copyProperty(dest, name, value);
    }

}

Then you can just instantiate a NullAwareBeanUtilsBean and use it to copy your beans, for example:

BeanUtilsBean notNull=new NullAwareBeanUtilsBean();
notNull.copyProperties(dest, orig);
SergiGS
@SergiGS Congratulations! And welcome to StackOverflow
Arthur Ronald F D Garcia