为什么我不能从sun.misc.DelegatingClassLoader查找类 [英] Why can I not look up classes from sun.misc.DelegatingClassLoader

查看:135
本文介绍了为什么我不能从sun.misc.DelegatingClassLoader查找类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过这个问题,我发现无法从 sun.misc.DelegatingClassLoader ,即在自己的类加载器上查找类,例如

From this question, I found that it is not possible to look up a class from a sun.misc.DelegatingClassLoader, i.e. looking up a class on its own class loader like

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

引发 ClassNotFoundException 。委托类加载器用于加载运行时生成的访问器类,以将Java的反射性JNI调用转换为Java调用。

throws a ClassNotFoundException. The delegating class loader is used for loading runtime-generated accessor classes for converting Java's reflective JNI calls into Java invocations.

出于某些奇怪的原因,我无法找到<$ c的来源$ c> DelegatingClassLoader 在JDK源代码中的任何地方,即使它在我的构建中显然可用,但它似乎是标准 ClassLoader的子类的空实现来查看类的字节码。

For some strange reason, I am not able to find the source of the DelegatingClassLoader anywhere in the JDK sources even though it is clearly available in my build where it seems to be an empty implementation of a subclass of the standard ClassLoader from looking at the class's byte code.

如果 DelegatingClassLoader 实际上只是一个简单的子类,我不明白为什么无法通过其名称查找类。是否涉及一些VM魔术?似乎

If the DelegatingClassLoader is however really only a simple subclass, I do not understand why it is not possible to look up a class by its name. Is there some VM magic involved? It seems like the

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

方法不返回 DelegatingClassLoader 。但是,私有的 classes 字段确实包含已加载的类。

method does not return the loaded class for a DelegatingClassLoader. The private classes field does however include the loaded class.

我无法找到有关这些方法的任何信息。类加载器应该与任何其他类加载器不同。我正在寻找一个解释,为什么上面的查找不起作用但会引发异常。

I was not able to find any information on how these class loaders are supposed to be different from any other class loaders. I am looking for an explanation why the above lookup does not work but throws an exception.

推荐答案

DelegatingClassLoader的源代码适用于任何情况原因位于 ClassDefiner.java 。有趣的是,此代码的注释引用了 bug 4474172 ,这表明该类loader是JVM已知的并且具有特殊的行为:

The source for DelegatingClassLoader is for whatever reason located in ClassDefiner.java. Interestingly, this code has a comment that references bug 4474172, which suggests that this class loader is known to the JVM and has special behavior:


第二种解决方案是使虚拟机神奇地成为
的委托这些新构造的类装入器的类装入到其父装入器
,而不会引起对Java的调用。
的缺点是,这是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.

这篇关于为什么我不能从sun.misc.DelegatingClassLoader查找类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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