Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java annotations - Identifier or TypeName

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.

like image 864
Mishax Avatar asked Jun 12 '13 07:06

Mishax


People also ask

Do you know any annotation in Java Please explain at least 2 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.

How to describe a Java code?

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.

Is value a Java keyword?

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.


2 Answers

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.

like image 171
Mishax Avatar answered Sep 24 '22 01:09

Mishax


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 )
like image 44
Fedy2 Avatar answered Sep 26 '22 01:09

Fedy2