Every annotation in JDK 1.5 onwards has a corresponding class. For example, the @override annotation which tells that a method is an overridden version of the method inherited from the super class, has a corresponding Override.class.
Similarly we have the Deprecated.class for the @deprecated annotation. We can see the contents of the corresponding source file to check what all attributes/parameters are being supported by the @override annotation. The Override.class has the following contents:
/ (version 1.5 : 49.0, no super bit)
@java.lang.annotation.Target(value={java.lang.annotation.ElementType.METHOD})
@java.lang.annotation.Retention(value=java.lang.annotation.RetentionPolicy.SOURCE)
public abstract @interface java.lang.Override extends java.lang.annotation.Annotation {
}
This suggests us that the annotation @Override can have two attributes viz Target and Retention. The value accepted by Target attribute is of type METHOD and of Retention is SOURCE.
The decompiled code of the Override.class is:
package java.lang;
import java.lang.annotation.Annotation;
// Referenced classes of package java.lang:
// Object
public interface Override extends Annotation {
}
This is applicable to all annotations that you may come across in various frameworks/tools. All annotations in Spring, Hibernate, JPA are backed by corresponding classes. If you decompile the code which makes use of annotations, you will not see any reference to annotation classes as the dependency with annotations is resolved at compile time only. Thus annotations are Meta-data for the compiler which it makes use of to perform common functions for developers thus easing the life of a Java application developer.
No comments:
Post a Comment