使用代理实现对私有方法的访问
2011-08-08 12:45
309 查看
public static Object invokePrivateMethod(Object obj, String methodName,
Class[] types, Object[] args) throws Exception {
Object ret = null;
try {
// getMethod() は public のみが対象となるため getDeclaredMethod() を使用します。
Method callee = obj.getClass().getDeclaredMethod(methodName, types);
// Accessible フラグを true とすることで、private, package,
// protected でもアクセスできるようになります。
callee.setAccessible(true);
// 実際にメソッドを呼び出します。
ret = callee.invoke(obj, args);
}
// メソッド内の例外は InvocationTargetException にラップされて送付されます。
catch (InvocationTargetException e) {
if (e.getTargetException() instanceof Exception) {
throw (Exception) e.getTargetException();
}
System.err.println(e.getTargetException().getMessage());
throw e;
}
// それ以外の例外が発生したときは、今回はそのまま例外をスローすることにしています。
// テスト時はコンソールに例外が出力されるので、それを参考にして下さい。
catch (Exception e) {
e.printStackTrace();
throw e;
}
return ret;
}
调用:第一个参数类对象,第二个参数方法名,点三个参数方法参数类型及个数,第四个参数参数值
invokePrivateMethod(logic, "checkTelegramParam", new Class[] { String.class, String.class, String.class, String.class }, new Object[] { shirei, destGwNo, procDate, procTime });
注意第一个参数一定是类的实例,不然出错。
第二种方法将以上方法详细化了:
public static Object invokeMethod(final Object object, final String methodName, final Class<?>[] parameterTypes,
final Object[] parameters) throws Exception {
Method method = getDeclaredMethod(object, methodName, parameterTypes);
if (method == null) {
throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + object + "]");
}
method.setAccessible(true);
try {
return method.invoke(object, parameters);
} catch (Exception e) {
throw e;
}
}
/**
*
*/
protected static Method getDeclaredMethod(Object object, String methodName, Class<?>[] parameterTypes) {
for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass
.getSuperclass()) {
try {
return superClass.getDeclaredMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e) {//NOSONAR
// Field is not in this class,get super class
}
}
return null;
}
第二种方法与第一种方法是一个意思。
Class[] types, Object[] args) throws Exception {
Object ret = null;
try {
// getMethod() は public のみが対象となるため getDeclaredMethod() を使用します。
Method callee = obj.getClass().getDeclaredMethod(methodName, types);
// Accessible フラグを true とすることで、private, package,
// protected でもアクセスできるようになります。
callee.setAccessible(true);
// 実際にメソッドを呼び出します。
ret = callee.invoke(obj, args);
}
// メソッド内の例外は InvocationTargetException にラップされて送付されます。
catch (InvocationTargetException e) {
if (e.getTargetException() instanceof Exception) {
throw (Exception) e.getTargetException();
}
System.err.println(e.getTargetException().getMessage());
throw e;
}
// それ以外の例外が発生したときは、今回はそのまま例外をスローすることにしています。
// テスト時はコンソールに例外が出力されるので、それを参考にして下さい。
catch (Exception e) {
e.printStackTrace();
throw e;
}
return ret;
}
调用:第一个参数类对象,第二个参数方法名,点三个参数方法参数类型及个数,第四个参数参数值
invokePrivateMethod(logic, "checkTelegramParam", new Class[] { String.class, String.class, String.class, String.class }, new Object[] { shirei, destGwNo, procDate, procTime });
注意第一个参数一定是类的实例,不然出错。
第二种方法将以上方法详细化了:
public static Object invokeMethod(final Object object, final String methodName, final Class<?>[] parameterTypes,
final Object[] parameters) throws Exception {
Method method = getDeclaredMethod(object, methodName, parameterTypes);
if (method == null) {
throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + object + "]");
}
method.setAccessible(true);
try {
return method.invoke(object, parameters);
} catch (Exception e) {
throw e;
}
}
/**
*
*/
protected static Method getDeclaredMethod(Object object, String methodName, Class<?>[] parameterTypes) {
for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass
.getSuperclass()) {
try {
return superClass.getDeclaredMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e) {//NOSONAR
// Field is not in this class,get super class
}
}
return null;
}
第二种方法与第一种方法是一个意思。
相关文章推荐
- 使用Java反射机制实现访问其他类的私有方法
- 使用Java反射机制实现访问其他类的私有方法
- 使用Java反射机制实现访问其他类的私有方法
- 使用Java反射机制实现访问其他类的私有方法
- 使用Java反射机制实现访问类中的私有变量或者方法
- 使用Java反射机制实现访问类中的私有变量或者方法
- Java使用付费代理的两种实现方法
- 在使用私有属性公有方法访问的时候在get方法里使用判断
- 派生于泛型基类的实现类VSTS生成的访问器无法调用基类的私有字段或方法
- 使用nginx的rewrite实现代理指定目录命令方法
- WCF局域网内使用代理无法访问解决方法
- Effective Java Item3:使用私有构造方法或者枚举类型实现单例(转自sunjavaduke)
- 使用Apache的Proxy模块实现对被代理网站的访问
- 使用花生壳、路由器、个人电脑进行站点部署并实现外网域名访问图文详解(花生壳设置方法 使用方法 使用教程)
- [转]apache禁止使用IP访问的实现方法
- Android低阶之【chapter - 6】Handler结合网络访问的使用方法,通过Handler实现异步网络请求
- php 使用curl模拟ip和来源进行访问的实现方法
- Nginx中的用户认证配置及阻止用户使用代理访问的方法
- 使用私有构造方法或者枚举类型实现单例
- 使用ASP.Net创建代理实现Javascript跨域访问