Helper是为了将非null属性从对象复制到另一个?(JAVA)

Art*_*ald 37 java helper

请参阅以下课程

public class Parent {

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

    // getters and setters   

}
Run Code Online (Sandbox Code Playgroud)

假设我已经创建了一个父对象,如下所示

Parent parent = new Parent();

parent.setName("A meaningful name");
parent.setAge(20);
Run Code Online (Sandbox Code Playgroud)

请注意,根据上面的代码,birthDate属性为null.现在我想从父对象复制到另一个非空属性.就像是

SomeHelper.copyNonNullProperties(parent, anotherParent);
Run Code Online (Sandbox Code Playgroud)

我需要它,因为我想更新anotherParent对象而不用空值覆盖其非null.

你知道这样的帮手吗?

我接受最少的代码作为答案是否没有帮助

问候,

小智 86

我说你已经有了一个解决方案,因为你问过很多时间.但是,它没有标记为已解决,也许我可以帮助其他用户.

你有没有定义的一个子类试过BeanUtilsBean了的org.commons.beanutils包?实际上,BeanUtils使用这个类,所以这是dfa提出的解决方案的改进.

检查该类的源代码,我认为你可以copyProperty通过检查空值来覆盖该方法,如果值为null则不执行任何操作.

像这样的东西:

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

然后你可以实例化一个NullAwareBeanUtilsBean并用它来复制你的bean,例如:

BeanUtilsBean notNull=new NullAwareBeanUtilsBean();
notNull.copyProperties(dest, orig);
Run Code Online (Sandbox Code Playgroud)

  • copyProperty() 是静态方法http://commons.apache.org/proper/commons-beanutils/javadocs/v1.9.3/apidocs/index.html (2认同)

Moh*_*sen 5

只需使用您自己的复制方法:

void copy(Object dest, Object source) throws IntrospectionException, IllegalArgumentException, IllegalAccessException,
        InvocationTargetException {
    BeanInfo beanInfo = Introspector.getBeanInfo(source.getClass());
    PropertyDescriptor[] pdList = beanInfo.getPropertyDescriptors();
    for (PropertyDescriptor pd : pdList) {
        Method writeMethod = null;
        Method readMethod = null;
        try {
            writeMethod = pd.getWriteMethod();
            readMethod = pd.getReadMethod();
        } catch (Exception e) {
        }

        if (readMethod == null || writeMethod == null) {
            continue;
        }

        Object val = readMethod.invoke(source);
        writeMethod.invoke(dest, val);
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

使用PropertyUtils(commons-beanutils)

for (Map.Entry<String, Object> e : PropertyUtils.describe(parent).entrySet()) {
         if (e.getValue() != null && !e.getKey().equals("class")) {
                PropertyUtils.setProperty(anotherParent, e.getKey(), e.getValue());
         }
}
Run Code Online (Sandbox Code Playgroud)

在Java8中:

    PropertyUtils.describe(parent).entrySet().stream()
        .filter(e -> e.getValue() != null)
        .filter(e -> ! e.getKey().equals("class"))
        .forEach(e -> {
        try {
            PropertyUtils.setProperty(anotherParent, e.getKey(), e.getValue());
        } catch (Exception e) {
            // Error setting property ...;
        }
    });
Run Code Online (Sandbox Code Playgroud)

  • @OrangeDog,因为 BeanUtils.copyProperties 会将 null 复制到之前必须包含值的属性中。 (2认同)