代码:
public class JavaBeanUtil {
private static final Logger logger = LoggerFactory.getLogger(JavaBeanUtil.class);
private static final String SET_METHOD_PREFIX = "set";
private static final String GET_METHOD_PREFIX = "get";
/**
* @title invokeSetMethod
* @description 反射设置字段值
* @param: target
* @param: fieldName
* @param: param
* @updateTime 2020-02-25 12:24
* @return void
*/
public static void invokeSetMethod(Object target, String fieldName, Object param) {
if (param == null) {
logger.error("param == null");
return;
}
Class<?> targetClass = target.getClass();
String methodName = SET_METHOD_PREFIX + StringUtils.upperFirstChar(fieldName);
try {
Method setMethod = targetClass.getDeclaredMethod(methodName, param.getClass());
setMethod.invoke(target, param);
} catch (NoSuchMethodException e) {
e.printStackTrace();
logger.error(String.format("未在类型:%s中找到名为:%s的方法", targetClass.getSimpleName(), methodName), e);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
logger.error(String.format("%s.%s方法执行错误", targetClass.getSimpleName(), methodName), e);
}
}
/***
* @title invokeGetMethod
* @description 反射获取字段值
* @param: target
* @param: fieldName
* @updateTime 2020-02-25 12:24
* @return java.lang.Object
*/
public static Object invokeGetMethod(Object target, String fieldName) {
Class<?> targetClass = target.getClass();
String methodName = GET_METHOD_PREFIX + StringUtils.upperFirstChar(fieldName);
try {
Method getMethod = targetClass.getDeclaredMethod(methodName);
return getMethod.invoke(target);
} catch (NoSuchMethodException e) {
e.printStackTrace();
logger.error(String.format("未在类型:%s中找到名为:%s的方法", targetClass.getSimpleName(), methodName), e);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
logger.error(String.format("%s.%s方法执行错误", targetClass.getSimpleName(), methodName), e);
}
return null;
}
/***
* @title attributeCopy
* @description 属性拷贝,null 不拷贝
* @param: source
* @param: target
* @updateTime 2020-02-25 12:23
* @return void
*/
public static void attributeCopy(Object source, Object target) {
Class<?> sourceClass = source.getClass();
Class<?> targetClass = target.getClass();
Field[] sourceFields = sourceClass.getDeclaredFields();
Field[] targetFields = targetClass.getDeclaredFields();
for (Field sourceField : sourceFields) {
Object sourceValue = invokeGetMethod(source, sourceField.getName());
if (sourceValue == null) {
continue;
}
for (Field targetField : targetFields) {
if (sourceField.getName().equals(targetField.getName())) {
invokeSetMethod(target, targetField.getName(), sourceValue);
}
}
}
}
}
测试:
public class JavaBeanUtilTest {
@Test public void invokeSetMethod() {
CaseRefundEntity entity = new CaseRefundEntity();
String filedName = "submitStaffId";
JavaBeanUtil.invokeSetMethod(entity, filedName, 3434L);
// JavaBeanUtil.invokeSetMethod(entity, "submitStaffId", 3434);
System.out.println(entity.getSubmitStaffId());
}
@Test public void invokeGetMethod() {
CaseRefundEntity entity2 = new CaseRefundEntity();
String filedName = "submitStaffId";
entity2.setSubmitStaffId(222L);
System.out.println(JavaBeanUtil.invokeGetMethod(entity2, filedName));
}
@Test public void attributeCopy() {
CaseRefundEntity entity2 = new CaseRefundEntity();
entity2.setSubmitStaffId(222L);
entity2.setId(223343L);
CaseRefundEntity entity = new CaseRefundEntity();
JavaBeanUtil.attributeCopy(entity2, entity);
System.out.println(entity.toString());
System.out.println(entity2.toString());
}
}