ClassLoader:可能配置为使用惰性而不是静态解析吗? [英] ClassLoader : possible configure to use lazy and not static resolution?

查看:125
本文介绍了ClassLoader:可能配置为使用惰性而不是静态解析吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了 JLS第12章。执行,它说的是分辨率


Resolution是检查从Test到其他类和其他类的符号引用的过程。通过加载提到的其他类和接口并检查引用是否正确来实现接口。


在初始链接时,解析步骤是可选的。一个实现可以解析来自很早链接的类或接口的符号引用,甚至可以递归地解决来自进一步引用的类和接口的所有符号引用。 ...


实现可以选择仅在积极使用符号引用时才对其进行解析;如果所有符号引用均一致使用此策略,则表示最懒决议的形式。在这种情况下,如果Test具有对另一个类的几个符号引用,则这些引用可能在使用时一次被解析,或者如果在程序执行期间从未使用过这些引用,则可能根本不解析。 p>

例如,一个实现可以选择单独解析类或接口中的每个符号引用,仅当使用它时(延迟或后期解析),或者在类时一次全部解析它们正在验证(静态分辨率)。这意味着在某些实现中,在初始化类或接口之后,解析过程可能会继续。


所以我的问题是我是否可以选择选择/强制使用 lazy 初始化?也许需要编写自定义类加载器?也许在启动时在类加载器中的ClassNotFoundException可能会被忽略?


我在 main 中有条件地创建了一个对象,实际发生,并且jar中缺少相应的类。但是 NoClassDefFound 甚至在 main 开始执行之前就被抛出。

解决方案

NoClassDefFoundError 错误:


如果Java虚拟机或 ClassLoader 实例
试图加载类的定义(作为普通方法调用
或使用 new 表达式创建新实例的一部分)
,但找不到该类的定义。


与实例化所讨论类的实例无关的错误。因此,对于您的示例,如果外部类为该类型定义一个字段,就足够了。您的包含主类的类无法加载,因为它取决于在运行时类路径上不可用的类型。



如果有疑问,请仔细阅读您的import语句,并删除要动态加载的类的import * )。然后尝试摆脱错误标记,而无需为该类再次添加导入。



* )-我认为,此动态加载的类位于其他包中,因此需要导入。


I read JLS Chapter 12. Execution so it says about resolution

Resolution is the process of checking symbolic references from Test to other classes and interfaces, by loading the other classes and interfaces that are mentioned and checking that the references are correct.

The resolution step is optional at the time of initial linkage. An implementation may resolve symbolic references from a class or interface that is being linked very early, even to the point of resolving all symbolic references from the classes and interfaces that are further referenced, recursively. ...

An implementation may instead choose to resolve a symbolic reference only when it is actively used; consistent use of this strategy for all symbolic references would represent the "laziest" form of resolution. In this case, if Test had several symbolic references to another class, then the references might be resolved one at a time, as they are used, or perhaps not at all, if these references were never used during execution of the program.

For example, an implementation may choose to resolve each symbolic reference in a class or interface individually, only when it is used (lazy or late resolution), or to resolve them all at once while the class is being verified (static resolution). This means that the resolution process may continue, in some implementations, after a class or interface has been initialized.

So my question is whether I can select to choose/force to use lazy intialization? Maybe it requires writing a custom class loader? Or maybe ClassNotFoundException in class loader at start up might be ignored?

I have a conditional creation of an Object in main which should never actually happen and that corresponding class is missing from jar. But NoClassDefFound is thrown even before main starts execution.

解决方案

The NoClassDefFoundError error:

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

The error not related to instantiating an instance of the class in question. So for your example, it may be enough, if the "outer" class defines a field for that type. Your class, that contains the main, can't be loaded, because it depends on a type that is not available on the classpath at runtime.

If in doubt, look through your import statements and remove the import*) for the class that you want to load dynamically. Then try to get rid of the error markers without adding an import for that class again.

*) - I assume, that this dynamically loaded class is in a different package and you need an import for it.

这篇关于ClassLoader:可能配置为使用惰性而不是静态解析吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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