我想使用方法MethodHandleNatives.getTargetMethod(MethodHandle)AccessibleObject。 MethodHandleNatives类不公开。 那么有人知道我该怎么做吗?
我知道可以通过反射访问私有方法和字段,所以我想问,如果这也是可能的。
感谢。
答案 0 :(得分:1)
我找到了解决方案 这不是直截了当但它起作用=)
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();
}