The Java Language Specification states that a normal annotation is in the format
NormalAnnotation:
@ TypeName ( ElementValuePairsopt )
A single-element annotation is in the format:
SingleElementAnnotation:
@ Identifier ( ElementValue )
I do not understand, why this inconsistency, why is a normal annotation a TypeName
and a single element annotation an Identifier
? I believe the reference to Identifier
might be a mistake in the specification since Identifier is not qualified and javac accepts AnnotationDeclarations that are qualified, for both normal annotations and single element annotations.
Annotation 1: @Deprecated It is a marker annotation. It indicates that a declaration is obsolete and has been replaced by a newer form. The Javadoc @deprecated tag should be used when an element has been deprecated. @deprecated tag is for documentation and @Deprecated annotation is for runtime reflection.
Declaring annotation types with @interface. You can declare an annotation type by specifying the @ symbol immediately followed by the interface reserved word and an identifier. For example, Listing 1 declares a simple annotation type that you might use to annotate thread-safe code.
It's not a regular keyword, as it's not listed in section 3.9 of the JLS. In particular, you can use it as an identifier anywhere you like, as far as I'm aware.
The reference to Identifier
is a mistake in the specification since an identifier cannot be qualified (you can write @java.lang.SuppressWarnings("unchecked")
but java.lang.SuppressWarnings
is not a legal identifier). Javac accepts AnnotationDeclarations that are qualified, for both normal annotations and single element annotations. The mistake appears to be recently introduced; older versions of the JLS do not have this problem.
I think there is a mistake in the grammar documentation. The single annotation-element and the marker annotations are shorthand for the normal annotation.
You can also see the Java 1.5/1.6 specification: http://docs.oracle.com/javase/specs/jls/se5.0/html/interfaces.html#9.7
The single-element annotation is indicated as:
SingleElementAnnotation:
@ TypeName ( ElementValue )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With