为什么 JVM class 加载器必须在首次使用前不报告问题?

Why must JVM class loaders not report problems until first active use?

我正在阅读下面的文章,但不明白以下段落背后的原因:

Class loaders can opt to load a type early in anticipation of eventual use. If this strategy is chosen, the class loader must not report any problem (by throwing a subclass of java.lang.LinkageError) encountered during loading until the type's first active use. In other words, a type must appear to be loaded only when needed.

这背后有什么具体原因吗?可能会出现什么样的问题,为什么需要在该类型的首次活跃使用期间专门报告?我对 JVM 的了解有限,因此不胜感激任何其他资源。

http://www.developer.com/java/other/article.php/2248831/Java-Class-Loading-The-Basics.htm

显然,这是 Java 语言规范第 12.2.1 The loading process 节(强调我的)规定的:

The loading process is implemented by the class ClassLoader and its subclasses.

Different subclasses of ClassLoader may implement different loading policies. In particular, a class loader may cache binary representations of classes and interfaces, prefetch them based on expected usage, or load a group of related classes together. These activities may not be completely transparent to a running application if, for example, a newly compiled version of a class is not found because an older version is cached by a class loader. It is the responsibility of a class loader, however, to reflect loading errors only at points in the program where they could have arisen without prefetching or group loading.

If an error occurs during class loading, then an instance of one of the following subclasses of class LinkageError will be thrown at any point in the program that (directly or indirectly) uses the type:

  • ClassCircularityError: A class or interface could not be loaded because it would be its own superclass or superinterface (§8.1.4, §9.1.3, §13.4.4).
  • ClassFormatError: The binary data that purports to specify a requested compiled class or interface is malformed.
  • NoClassDefFoundError: No definition for a requested class or interface could be found by the relevant class loader.

Because loading involves the allocation of new data structures, it may fail with an OutOfMemoryError.

如果加载程序预取了一组相关的 class,但是违规的 class 从未实际使用过,那么抛出错误将没有用也没有必要。

至于可能发生的问题类型的示例,可能在 class 级别变量声明中使用格式错误的正则表达式可能会在加载时导致问题。或者可能存在依赖项问题,其中缺少一个导入。

那么,如果从未使用过 class,为什么会抛出错误?