[JSR308] Annotation ambiguity

Michael Ernst mernst at cs.washington.edu
Sun May 24 18:03:55 EDT 2009


> Please consider this example:
> class S {
>  @Deprecated int foo() { return 0; }
> }
> Question: 
> Does the @Deprecated annotation apply to the method foo, or method return type?
> According to JLS, since the Deprecated class lacks any @Target annotations, it can be applied to any element.
> Therefore, it applies to both 'int' data type and method foo.

You are correct.  (This is specified in section 2.3 of the Type Annotations
specification, and further discussed in appendix B.4.)

> Which does not make any sense, in my opinion.

You have discovered a bug in the definition of the Deprecated class.
Ideally, each annotation should have a @Target meta-annotation.  If there
was one, then @Deprecated would apply only to the method foo.

However, the given behavior does not cause any problems.  Any annotation
processor that processes the @Deprecated annotation will not even look in
the type annotation position, and the existence of an extra annotation in
that position will not interfere with any other annotation processor


More information about the JSR308 mailing list