从JDK访问非公共(Java本地)类(7) [英] Access non-public (java-native) classes from JDK (7)

查看:93
本文介绍了从JDK访问非公共(Java本地)类(7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用方法MethodHandleNatives.getTargetMethod(MethodHandle)AccessibleObject. MethodHandleNatives类不是公共的. 那么有人知道我该怎么做吗?

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.

谢谢.

推荐答案

我找到了解决方案.
它不是直截了当的,但它可以工作=)

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

这篇关于从JDK访问非公共(Java本地)类(7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆