为什么缺少注释不会在运行时导致 ClassNotFoundException?

考虑以下代码:

返回文章页面

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;


@Retention(RetentionPolicy.RUNTIME)
@interface A{}

C.java:

import java.util.*;


@A public class C {
public static void main(String[] args){
System.out.println(Arrays.toString(C.class.getAnnotations()));
}
}

按预期进行编译和运行工作:

$ javac *.java
$ java -cp . C
[@A()]

但是想想这个:

$ rm A.class
$ java -cp . C
[]

我原以为它会抛出一个 ClassNotFoundException,因为 @A丢失了。但实际上,它默默地丢弃了注释。

这种行为是否在 JLS 的某个地方有文档记录,或者是 Sun 的 JVM 的一个特例?理由是什么?

对于像 javax.annotation.Nonnull这样的东西似乎很方便(无论如何它本来就应该是 @Retention(CLASS)) ,但是对于其他许多注释来说,它似乎会在运行时导致各种各样的坏事情发生。

14561 次浏览

Quoting the JLS:

9.6.1.2 Retention Annotations may be present only in the source code, or they may be present in the binary form of a class or interface. An annotation that is present in the binary may or may not be available at run-time via the reflective libraries of the Java platform.

The annotation type annotation.Retention is used to choose among the above possibilities. If an annotation a corresponds to a type T, and T has a (meta-)annotation m that corresponds to annotation.Retention, then:

  • If m has an element whose value is annotation.RetentionPolicy.SOURCE, then a Java compiler must ensure that a is not present in the binary representation of the class or interface in which a appears.
  • If m has an element whose value is annotation.RetentionPolicy.CLASS, or annotation.RetentionPolicy.RUNTIME a Java compiler must ensure that a is represented in the binary representation of the class or interface in which a appears, unless m annotates a local variable declaration. An annotation on a local variable declaration is never retained in the binary representation.

If T does not have a (meta-)annotation m that corresponds to annotation.Retention, then a Java compiler must treat T as if it does have such a meta-annotation m with an element whose value is annotation.RetentionPolicy.CLASS.

So RetentionPolicy.RUNTIME ensures that the annotation is compiled into the binary but an annotation present in the binary doesn't have to be available at runtime

In the earlier public drafts for JSR-175 (annotations), it was discussed if the compiler and runtime should ignore unknown annotations, to provide a looser coupling between the usage and declaration of annotations. A specific example was the use of applications server specific annotations on an EJB to control the deployment configuration. If the same bean should be deployed on a different application server, it would have been convenient if the runtime simply ignored the unknown annotations instead of raising a NoClassDefFoundError.

Even if the wording is a little bit vague, I assume that the behaviour you are seeing is specified in JLS 13.5.7: "... removing annotations has no effect on the correct linkage of the binary representations of programs in the Java programming language." I interpret this as if annotations are removed (not available at runtime), the program should still link and run and that this implies that the unknown annotations are simply ignored when accessed through reflection.

The first release of Sun's JDK 5 did not implement this correctly, but it was fixed in 1.5.0_06. You can find the relevant bug 6322301 in the bug database, but it does not point to any specifications except claiming that "according to the JSR-175 spec lead, unknown annotations must be ignored by getAnnotations".

if you actually have code that reads @A and does something with it, the code has a dependency on class A, and it will throw ClassNotFoundException.

if not, i.e. no code cares specificly about @A, then it's arguable that @A doesn't really matter.