我想使用 MethodHandleNatives.getTargetMethod(MethodHandle)AccessibleObject 方法。 MethodHandleNatives 类不是公共(public)的。 那么有人知道我该怎么做吗?
我知道可以通过反射访问私有(private)方法和字段,所以我问这是否也可能。
谢谢。
最佳答案
我找到了解决办法。
这并不简单,但很有效 =)
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();
}
关于java - 从 JDK 访问非公共(public)(java 原生)类 (7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16040726/