簡體   English   中英

從JDK訪問非公共(Java本地)類(7)

[英]Access non-public (java-native) classes from JDK (7)

我想使用方法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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM