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

可紊 提交于 2020-01-06 14:16:19

问题


I want to use the method MethodHandleNatives.getTargetMethod(MethodHandle)AccessibleObject. The class MethodHandleNatives is not public. So does anybody know how I can do that?

I know that its possible to access private methods and fields via reflections, so I am asking, if this is also possible.

Thanks.


回答1:


I have found a solution.
It is not straight forward but it works =)

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();
    }


来源:https://stackoverflow.com/questions/16040726/access-non-public-java-native-classes-from-jdk-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!