Java tutorial
/* * Copyright 1999-2004 Alibaba.com All right reserved. This software is the confidential and proprietary information of * Alibaba.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only * in accordance with the terms of the license agreement you entered into with Alibaba.com. */ package com.jinmibao.common.util; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; /** * BeanUtil.java??TODO ?? * * @author chongan.wangca 2012-3-30 ?8:23:10 */ public class BeanUtil { private final static Logger logger = LoggerFactory.getLogger(BeanUtil.class); public static void copyProperties(Object source, Object target) { BeanUtils.copyProperties(source, target); } /** * ? ????? ? * * @param source * @param target */ public static void copyPropertiesIfNull(Object source, Object target) { if (source == null || target == null) { throw new RuntimeException("Source Or Target Can't Be Null"); } Method[] methods = target.getClass().getDeclaredMethods(); List<String> notNullPropertyList = new ArrayList<String>(); for (Method method : methods) { if (!method.getName().startsWith("get")) { continue; } try { Object methodResult = method.invoke(target); if (methodResult != null) { if (method.getReturnType().getName().equals("java.lang.String") && StringUtil.isBlank((String) methodResult)) { continue; } notNullPropertyList.add(BeanUtils.findPropertyForMethod(method).getName()); } } catch (Exception e) { logger.error("", e); } } BeanUtils.copyProperties(source, target, ListUtil.list2Array(notNullPropertyList)); } }