問題描述
考慮以下代碼:
A.java:
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()));
}
}
編譯和運行按預期工作:
Compiling and running works as expected:
$ javac *.java
$ java -cp . C
[@A()]
但是再考慮一下:
$ rm A.class
$ java -cp . C
[]
我預計它會拋出 ClassNotFoundException
,因為缺少 @A
.但相反,它會默默地刪除注釋.
I would've expected it to throw a ClassNotFoundException
, since @A
is missing. But instead, it silently drops the annotation.
這種行為是否記錄在 JLS 中的某個地方,還是 Sun 的 JVM 的怪癖?這樣做的理由是什么?
Is this behaviour documented in the JLS somewhere, or is it a quirk of Sun's JVM? What's the rationale for it?
對于 javax.annotation.Nonnull
之類的東西似乎很方便(無論如何,它似乎應該是 @Retention(CLASS)
),但對于許多其他注釋似乎它可能會導致運行時發生各種不好的事情.
It seems convenient for things like javax.annotation.Nonnull
(which seems like it should've been @Retention(CLASS)
anyway), but for many other annotations it seems like it could cause various bad things to happen at runtime.
推薦答案
在 JSR-175(注解)的早期公共草案中,討論了編譯器和運行時是否應該忽略未知注解,以提供更松散的耦合注釋的使用和聲明.一個具體的例子是在 EJB 上使用應用程序服務器特定的注釋來控制部署配置.如果同一個 bean 應該部署在不同的應用服務器上,如果運行時簡單地忽略未知注解而不是引發 NoClassDefFoundError 會很方便.
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.
即使措辭有點模糊,我認為您看到的行為在 JLS 13.5.7: "... 刪除注釋對 Java 中程序的二進制表示的正確鏈接沒有影響編程語言."我將此解釋為好像注釋已刪除(在運行時不可用),程序仍應鏈接并運行,這意味著在通過反射訪問時會簡單地忽略未知注釋.
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.
Sun 的 JDK 5 的第一個版本沒有正確實現這一點,但在 1.5.0_06 中已修復.你可以在bug數據庫中找到相關的bug6322301,但是沒有指向任何規范,除了聲稱根據 JSR-175 規范指南,getAnnotations 必須忽略未知注釋".
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".
這篇關于為什么缺少注釋不會在運行時導致 ClassNotFoundException?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!