java反射method.invoke_java反射代码留存(invokeSetMethod、invokeGetMethod)

代码:

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值