如何在Java 12中访问类加载器的类Vector?

问题描述 投票:0回答:2

在以前的Java版本中,此代码

    Field field = ClassLoader.class.getDeclaredField("classes");
    field.setAccessible(true);
    Vector<Class<?>> classes = field.get(classLoaderInstance);

...可行,但是在Java 12中不起作用:有人知道可以获取对此Vector的引用的黑客吗?

java class classloader java-12
2个回答
0
投票

如何使用Instrumentation.getAllLoadedClasses?这对您的情况有用吗?


0
投票

我为此解决了

    MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(ClassLoader.class, 
    MethodHandles.lookup());
    VarHandle varHandle = lookup.findVarHandle(ClassLoader.class, "classes", Vector.class);
    return (Vector<Class<?>>)varHandle.get(classLoader);

...但这只能是一个临时解决方案,因为启动时的jvm表示:

    WARNING: An illegal reflective access operation has occurred
    WARNING: Illegal reflective access using Lookup on package.myClassName to class java.lang.ClassLoader
    WARNING: Please consider reporting this to the maintainers of package.myClassName
    WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
    WARNING: All illegal access operations will be denied in a future release
© www.soinside.com 2019 - 2024. All rights reserved.