为什么我无法从 sun.misc.DelegatingClassLoader 中查找 类

Why can I not look up classes from sun.misc.DelegatingClassLoader

,我发现无法从 sun.misc.DelegatingClassLoader 中查找 class,即单独查找 class [=37] =] 装载机喜欢

Class<?> accessor = ...
accessor.getClassLoader().findClass(accessor.getName());

抛出 ClassNotFoundException。委托 class 加载器用于加载运行时生成的访问器 classes,用于将 Java 的反射 JNI 调用转换为 Java 调用。

出于某种奇怪的原因,我是 not able to find the source of the DelegatingClassLoader anywhere in the JDK sources,尽管它在我的构建中显然可用,它似乎是标准 ClassLoader 的子 class 的空实现通过查看 class 的字节码。

如果 DelegatingClassLoader 实际上只是一个简单的子 class,我不明白为什么不能通过名称查找 class。是否涉及一些 VM 魔法?好像是

private native final Class<?> findLoadedClass0(String name);

方法不会 return 为 DelegatingClassLoader 加载 class。但是,私有 classes 字段确实包含加载的 class.

我找不到任何关于这些 class 加载器与其他 class 加载器有何不同的信息。我正在寻找为什么上述查找不起作用但抛出异常的解释。

DelegatingClassLoader 的源代码出于某种原因位于 ClassDefiner.java. Interestingly, this code has a comment that references bug 4474172,这表明此 class 加载程序为 JVM 所知并具有特殊行为:

The second solution is to make the virtual machine "magically" delegate class loading for these newly-fabricated class loaders to their parent loaders, without making an upcall to Java. The disadvantage is that this is another hack in the JVM [...]. This option has been chosen.