0

メソッド MethodHandleNatives.getTargetMethod(MethodHandle)AccessibleObject を使用したい。クラス MethodHandleNatives はパブリックではありません。それで、私がそれを行う方法を知っている人はいますか?

リフレクションを介してプライベートメソッドとフィールドにアクセスできることを知っているので、これも可能かどうか尋ねています。

ありがとう。

4

1 に答える 1

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();
    }
于 2013-04-16T18:56:54.003 に答える