File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Annotation attributes with type parameters

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you define a Java interface, it's possible to declare a method with type parameters, for example like this:

The same thing does not work in an annotation. This, for example, is illegal:

I can get what I'm after by using the raw type Enum:

What exactly is the reason why it is not possible to declare annotation attributes with type parameters?

(Also posted here, but not getting any useful answers there...).
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha, found an acceptable answer. I can just do this:

(Note the Enum<?> instead of the raw type Enum).

Section 9.6 of the Java Language Specification says:
It is a compile-time error if the return type of a method declared in an annotation type is any type other than one of the following: one of the primitive types, String, Class and any invocation of Class, an enum type (§8.9), an annotation type, or an array (§10) of one of the preceding types. It is also a compile-time error if any method declared in an annotation type has a signature that is override-equivalent to that of any public or protected method declared in class Object or in the interface annotation.Annotation.

And then it says this under "Discussion":
Note that this does not conflict with the prohibition on generic methods, as wildcards eliminate the need for an explicit type parameter.

In other words, it suggests using wildcards instead of type parameters. Probably allowing type parameters would have made annotations a lot more complicated, so they were just left out of annotations, with the idea that wildcards should be enough for what you'd need.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic