通过反映射调用私有方法
2009-05-05 14:37
113 查看
public class ClassMethodUtil {
/**
* @author yuxin 执行对象内特定的私有或公用函数
* @param clsType
* 函数所在的类
* @param obj
* 函数所在的对象
* @param methodName
* 函数名称
* @param parameterTypes
* 函数参数的类型数组
* @param args
* 函数参数数组
* @return 方法执行后的返回值
*/
public static Object methoInvoke(Class clsType, Object obj, String methodName, Class[] parameterTypes, Object[] args)
throws Exception {
try {
Method method = clsType.getDeclaredMethod(methodName,
parameterTypes);
method.setAccessible(true);
return method.invoke(obj, args);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
}
}
/**
* @author yuxin 取得对象内特定的私有或公用字段
* @param clsType
* 字段所在的类
* @param obj
* 字段所在的对象
* @param fieldName
* 字段名称
* @return 字段值
*/
public static Object getField(Class clsType, Object obj, String fieldName)
throws Exception {
try {
Field field = clsType.getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(obj);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
}
}
/**
* @author yuxin 取得对象内特定的私有或公用字段
* @param clsType
* 字段所在的类
* @param obj
* 字段所在的对象
* @param fieldName
* 字段名称
* @param value
* 字段值
*/
public static void setField(Class clsType, Object obj, String fieldName,
Object value) throws Exception {
try {
Field field = clsType.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(obj, value);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
}
}
}
/**
* @author yuxin 执行对象内特定的私有或公用函数
* @param clsType
* 函数所在的类
* @param obj
* 函数所在的对象
* @param methodName
* 函数名称
* @param parameterTypes
* 函数参数的类型数组
* @param args
* 函数参数数组
* @return 方法执行后的返回值
*/
public static Object methoInvoke(Class clsType, Object obj, String methodName, Class[] parameterTypes, Object[] args)
throws Exception {
try {
Method method = clsType.getDeclaredMethod(methodName,
parameterTypes);
method.setAccessible(true);
return method.invoke(obj, args);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
}
}
/**
* @author yuxin 取得对象内特定的私有或公用字段
* @param clsType
* 字段所在的类
* @param obj
* 字段所在的对象
* @param fieldName
* 字段名称
* @return 字段值
*/
public static Object getField(Class clsType, Object obj, String fieldName)
throws Exception {
try {
Field field = clsType.getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(obj);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
}
}
/**
* @author yuxin 取得对象内特定的私有或公用字段
* @param clsType
* 字段所在的类
* @param obj
* 字段所在的对象
* @param fieldName
* 字段名称
* @param value
* 字段值
*/
public static void setField(Class clsType, Object obj, String fieldName,
Object value) throws Exception {
try {
Field field = clsType.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(obj, value);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception(e);
}
}
}
相关文章推荐
- 通过反射,如何操作私有成员变量(取/赋值),如何调用私有方法?
- PHP通过反射方法调用执行类中的私有方法
- 通过反射修改类的私有字段值,调用私有方法
- 通过JAVA的反射调用类中的公有私有方法
- php面向对象_get(),_set()的用法 (通过另外类 或者子类调用私有方法时)
- Java通过反射调用其他类的私有属性和私有方法
- 通过反射创建私有化类的对象,调用类的私有方法,改变私有属性
- 通过反射修改类的私有字段值,调用私有方法
- javascript:模拟类私有属性,通过类方法调用例子
- 如何通过C#.NET中的反射机制来创建C#.NET泛型类的实例,并调用其方法??
- 通过ScriptManager在客户端来调用服务器端方法或者webService的方法
- web developer tips (83):通过反射从堆栈里获取调用的方法名称
- python下面通过ctypes模块调用c++库的方法
- python下面通过ctypes模块调用c++库的方法
- 用接口实现通过改外部文件实现判断调用接口内的方法
- 显示当前日期和时间(通过调用System.currentTimeMills ()方法)
- Linux Debugging(三): C++函数调用的参数传递方法总结(通过gdb+反汇编)
- 通过反射调用动态连接库中的方法
- 解决jQuery.Validate.js在先调用form()方法后没法获取通过js修改的验证规则
- Java 通过反射调用方法