メソッド MethodHandleNatives.getTargetMethod(MethodHandle)AccessibleObject を使用したい。クラス MethodHandleNatives はパブリックではありません。それで、私がそれを行う方法を知っている人はいますか?
リフレクションを介してプライベートメソッドとフィールドにアクセスできることを知っているので、これも可能かどうか尋ねています。
ありがとう。
メソッド MethodHandleNatives.getTargetMethod(MethodHandle)AccessibleObject を使用したい。クラス MethodHandleNatives はパブリックではありません。それで、私がそれを行う方法を知っている人はいますか?
リフレクションを介してプライベートメソッドとフィールドにアクセスできることを知っているので、これも可能かどうか尋ねています。
ありがとう。
私は解決策を見つけました。
それは簡単ではありませんが、うまくいきます=)
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();
}