根据属性名字调用相应的get和set方法
2011-09-01 10:08
393 查看
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));
}
}
相关文章推荐
- 根据属性名调用set/get方法
- PHP __get()和__set()的使用方法(存取或者调用未定义的属性变量)
- 关于JPA封装数据库数据到实体不调用属性的get和set的方法解决办法
- NSSelectorFromString 的使用,根据字符串来调用相应名字的方法
- 动态调用Java Bean的set和get方法
- Java反射实体类属性(get ,set方法)
- 通过PropertyDescriptor反映射调用set和get方法
- SSH整合时实体类的属性生成get,set方法里的小坑
- javaEE数据库查询数据时,bean对象需要添加属性的set、get方法,否则查找不到
- php __set() __get() 方法访问属性
- 通过PropertyDescriptor反映射调用set和get方法
- 属性get和set对应的方法
- php面向对象基础概念(属性重载的魔术方法)__get(),__set(),__isset(),__unset()
- __set() __get() _isset() __unset() 在__unset() 在类中没有事先声明和已经声明过的属性调用unset的区别
- java中不利用get方法获得私有属性、不利用set方法修改私有属性、通过执行私有方法getName获得私有属性、通过执行私有方法setName更改私有属性name的值
- JS 实现对象属性的get 和set方法
- 为什么java要用get,set方法替代域的直接调用?
- 反映射调用set和get方法
- 通过PropertyDescriptor反映射调用set和get方法
- 反射调用 java bean的set和get方法