Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't a missing annotation cause a ClassNotFoundException at runtime?

Consider the following code:

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()] 

But then consider this:

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

I would've expected it to throw a ClassNotFoundException, since @A is missing. But instead, it silently drops the annotation.

Is this behaviour documented in the JLS somewhere, or is it a quirk of Sun's JVM? What's the rationale for it?

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.

like image 840
Matt McHenry Avatar asked Aug 25 '10 15:08

Matt McHenry


People also ask

What causes ClassNotFoundException?

The java. lang. ClassNotFoundException is a checked exception in Java that occurs when the JVM tries to load a particular class but does not find it in the classpath.

How do I get rid of ClassNotFoundException in Java?

When you get a ClassNotFoundException, it means the JVM has traversed the entire classpath and not found the class you've attempted to reference. The solution, as so often in the Java world, is to check your classpath. You define a classpath on the command line by saying java -cp and then your classpath.

What is the difference between NoClassDefFoundError and ClassNotFoundException '?

Both ClassNotFoundException and NoClassDefFoundError are the errors when JVM or ClassLoader not able to find appropriate class while loading at run-time. ClassNotFoundException is a checked exception and NoClassDefFoundError is an Error which comes under unchecked.

In which case a NoClassDefFoundError will be thrown?

NoClassDefFoundError is thrown when the JVM is asked to load a class indirectly. e.g. when class A is using class B and class B is not on classpath, NoClassDefFoundError will be thrown.


2 Answers

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".

like image 162
jarnbjo Avatar answered Oct 05 '22 08:10

jarnbjo


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

like image 38
Guillaume Avatar answered Oct 05 '22 10:10

Guillaume