Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are interfaces not allowed as annotation members?

Consider this code:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Bar {
    Foo foo() default FooImpl.FooConstant;
}

Compiler error:

annotation value not of an allowable type

If I replace Foo with FooImpl the code is accepted.

What's the reason for this behavior?

like image 233
soc Avatar asked Aug 23 '11 12:08

soc


People also ask

Can an annotation implement an interface?

From the Annotations Reference: Return types are restricted to primitives, String, Class, enums, annotations, and arrays of the preceding types. So, yes, the type can be an enum that implements an interface, but not the interface itself.

Are annotations interfaces?

Annotation types are a form of interface, which will be covered in a later lesson. For the moment, you do not need to understand interfaces. The body of the previous annotation definition contains annotation type element declarations, which look a lot like methods. Note that they can define optional default values.

Which annotation can be used to ensure that an interface?

You can annotate with @Service an implementation and autowire the interface. Spring will check for any object implementing this interface.

Why are annotations better than marker interfaces?

Marker interfaces define a type that is implemented by instances of the marked class; marker annotations do not. The existence of this type allows you to catch errors at compile time that you couldn't catch until runtime if you used a marker annotation.


2 Answers

If I replace Foo with FooImpl the code is accepted.

I would be very surprised if this compiled, unless FooImpl is an enum.

Annotation members may only contain the following:

  • primitive type
  • String
  • Class literal
  • annotation
  • enum item
  • or 1-dimensional arrays of any of the above

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.

Source: JLS

like image 104
Sean Patrick Floyd Avatar answered Sep 17 '22 07:09

Sean Patrick Floyd


http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.7

The annotation member types must be one of: primitive, String, Class, an Enum, an array of any of the above

It is a compile-time error if the element type is not commensurate with the ElementValue.

Hope this helps!

Found the same in this documentation as well:

http://download.oracle.com/javase/1.5.0/docs/guide/language/annotations.html

"Return types are restricted to primitives, String, Class, enums, annotations, and arrays of the preceding types." As mentioned "interface" is not allowed.

like image 33
Vicky Avatar answered Sep 20 '22 07:09

Vicky