根据属性名字调用相应的get和set方法

package com.ynztpwl.reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import com.ynztpwl.entity.Person;

public class BeanReflectUtil {
/**
* 组装类中某个属性的get方法名称 StringUtil.upperFirst(String str)是自己封装的,将首字母变大写的小方法;
*
* @param c
* 所反射方法所在的类的Class
* @param filedName
* :字段名称
* @return 返回该字段的get方法
* @throws SecurityException
* @throws NoSuchMethodException
*/
public static Method assembleGetMethod(Class<?> c, String filedName)
throws SecurityException, NoSuchMethodException {
String methodName = "get" + BeanReflectUtil.upperFirst(filedName);// 组装方法名
Class<?>[] nullClasses = null;// getter方法所需要的参数类型的Class
return c.getDeclaredMethod(methodName, nullClasses);
}

/**
* 组装类中某个属性的set方法名称
*
* @param c:所反射方法所在的类的Class
* @param filedName
* :字段名称
* @param classArgsType:setter方法所需的参数类型
* @return :返回该字段的setter方法
* @throws SecurityException
* @throws NoSuchMethodException
*/
public static Method assembleSetMethod(Class<?> c, String filedName,
Class<?>[] classArgsType) throws SecurityException,
NoSuchMethodException {
String methodName = "set" + BeanReflectUtil.upperFirst(filedName);
return c.getDeclaredMethod(methodName, classArgsType);
}

/**
* 调用组装类中某个属性的getter方法
*
* @param c:所反射方法所在的类的Class
* @param filedName:字段名称
* @param obj:所反射方法所在的类的实体对象
* @return :getter的返回值
* @throws SecurityException
* @throws NoSuchMethodException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static Object invokeGetMethod(Class<?> c, String filedName,
Object obj) throws SecurityException, NoSuchMethodException,
IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
Method method = assembleGetMethod(c, filedName); // 得到属性的getter
Object[] nullObjects = null; // 定义方法所要的参数值
return method.invoke(obj, nullObjects);
}

/**
* 调用组装类中某个属性的setter方法
*
* @param c:所反射方法所在的类的Class
* @param filedName:字段名称
* @param obj:所反射方法所在的类的实体对象
* @param argsType:setter方法所需要的参数类型
* @param argsValue:setter方法所需要的参数值
* @return setter方法的返回值
* @throws SecurityException
* @throws NoSuchMethodException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static void invokeSetMethod(Class<?> c, String filedName,
Object obj, Class<?>[] argsType, String[] argsValue)
throws SecurityException, NoSuchMethodException,
IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
Method method = assembleSetMethod(c, filedName, argsType);
method.invoke(obj, argsValue);
}

/**
* 将首字母转化为大写的小方法
*
* @param str
* :需要转化的字符
* @return 转化后的结果
*/
public static String upperFirst(String str) {
String first = str.substring(0, 1);
String last = str.substring(1);
return first.toUpperCase() + last;
}

public static void main(String[] args) throws SecurityException,
IllegalArgumentException, NoSuchMethodException,
IllegalAccessException, InvocationTargetException {
Person person = new Person();
BeanReflectUtil.invokeSetMethod(Person.class, "name", person,
new Class<?>[] { String.class }, new String[] { "李芸" });
System.out.println(BeanReflectUtil.invokeGetMethod(Person.class,
"name", person));
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值