Skip to content

Java Annotations

July 19, 2013

Java annotations (@interface) are special kinds of interfaces that act like metadata. Special because they have a superinterface of java.lang.annotation.Annotation. Notice, that all interfaces implicitly have a superinterface that is NOT an annotation type itself. Also annotations do not allow subclassing or generic definition of the annotation (however Generics can be used in the methods on the annotation).

Annotation type methods are no argument methods that can only return a select list of possible return types.

It is a compile-time error if the return type of a method declared in an annotation type is not one of the following: a primitive type, StringClass, any parameterized invocation of Class, an enum type (§8.9), an annotation type, or an array type (§10) whose element type is one of the preceding types.

More specifics on Java annotations can be found in these 3 sections of the Java Language Specification: Annotation TypesAnnotations, and Binary Compatibility of Annotations.

How are annotations typically used

The JDK has some predefined annotations. @Override is an example of a source retention type (available only to the compiler during compilation), and @Deprecated which is runtime retention (so reflection can be used to find these methods).

Compile time annotation processing (JSR-269) can be done by implementing javax.annotation.processing.Processor interface OR extending an abstract class javax.annotation.processing.AbstractProcessor. Once that’s done, you can pass that class to the javac compiler:
javac -processorpath -processor <class1,class2,etc>
Multiple parameters can be passed to javac with respect to compile time annotation processing.

Runtime annotation processing is done by most container managed environments, like Spring. Since the container is responsible for most setup and creation of objects, it only makes sense to have the container use primarily runtime annotations.


From → Java

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: