我想使用MethodHandleNatives.getTargetMethod(MethodHandle)AccessibleObject.方法类MethodHandleNatives不是公共的。有人知道我是怎么做到的吗?
我知道通过反射访问私有方法和字段是可能的,所以我在问,这是否也是可能的。
谢谢。
发布于 2013-04-17 02:56:54
我找到了一个解决方案。
它不是直接的,但它是有效的=)
MethodHandle mh; // a MethodHandle Object
Class<?> mhn;
try {
mhn = Class.forName("java.lang.invoke.MethodHandleNatives");
Constructor<?> con = mhn.getDeclaredConstructor();
con.setAccessible(true);
Object mhnInstance = con.newInstance();
Method getTargetMethod = mhn.getDeclaredMethod("getTargetMethod", new Class<?>[]{MethodHandle.class});
getTargetMethod.setAccessible(true);
Method inside = (Method) getTargetMethod.invoke(mhnInstance, mh);
System.out.println("INSIDE = " + inside.toGenericString());
} catch (Throwable e) {
e.printStackTrace();
}https://stackoverflow.com/questions/16040726
复制相似问题