Skip to content

Instantly share code, notes, and snippets.

@syhily
Last active August 29, 2015 14:27
Show Gist options
  • Save syhily/0dca68e0f7988954dd0c to your computer and use it in GitHub Desktop.
Save syhily/0dca68e0f7988954dd0c to your computer and use it in GitHub Desktop.
BeanUtil.java
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.springframework.util.Assert;
/**
* 类名称: BeanUtil <br>
* 类描述: 基于Spring的BeanUtils改造,银行路由自带Bean拷贝方法。<br>
* 针对指定的属性名进行拷贝,如果属性名相同类型不同,则不予拷贝。
* @author yufan.sheng
* @version 1.0.0
* @since 15/7/31 上午9:14
*/
public abstract class BeanUtil {
/**
* Copy the given property values of the given source bean into the target bean.
* <p>Note: The source and target classes do not have to match or even be derived
* from each other, as long as the properties match. Any bean properties that the
* source bean exposes but the target bean does not will throw IntrospectionException.
*
* @param source the source bean
* @param target the target bean
* @param names the properties that would be copied
*/
public static void copyProperties(Object source, Object target, String... names) {
copyProperties(source, target, null, names);
}
/**
* Copy the given property values of the given source bean into the target bean.
* <p>Note: The source and target classes do not have to match or even be derived
* from each other, as long as the properties match. Any bean properties that the
* source bean exposes but the target bean does not will throw IntrospectionException.
*
* @param source the source bean
* @param target the target bean
* @param editable the class (or interface) to restrict property setting to
* @param names the properties that would be copied
*/
public static void copyProperties(Object source, Object target, Class<?> editable, String... names) {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
if (names.length < 1) {
return;
}
Class<?> sourceClass = source.getClass();
Class<?> targetClass = target.getClass();
if (editable != null) {
if (!editable.isInstance(target)) {
throw new IllegalArgumentException("Target class [" + target.getClass().getName() +
"] not assignable to Editable class [" + editable.getName() + "]");
}
targetClass = editable;
}
for (String name : names) {
try {
PropertyDescriptor sourceProperty = new PropertyDescriptor(name, sourceClass);
PropertyDescriptor targetProperty = new PropertyDescriptor(name, targetClass);
if (!sourceProperty.getPropertyType().equals(targetProperty.getPropertyType())) {
LOGGER.warn("Target property [{}] is not match the type..", name);
continue;
}
Method sourceReadMethod = sourceProperty.getReadMethod();
Method targetWriteMethod = targetProperty.getWriteMethod();
targetWriteMethod.invoke(target, sourceReadMethod.invoke(source));
} catch (IntrospectionException e) {
LOGGER.info("IntrospectionException - {}", e);
} catch (InvocationTargetException e) {
LOGGER.info("InvocationTargetException - {}", e);
} catch (IllegalAccessException e) {
LOGGER.info("IllegalAccessException - {}", e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment