views:

45

answers:

1

Best done with examples. Bare with me..

Let's supposed i have this interface:

public interface MyInterface {
   void doStuff();
}

With a concrete implementation:

public class HardCoreConcrete implements MyInterface {
   void doStuff() {
      // i really do stuff, honest
   }
}

And suppose i have this annotation:

@Target(ElementType.class)
@Retension(RetensionPolicy.RUNTIME)
public @interface MyAnnotation {
   Class<MyInterface> clazz;
}

It would be used like this:

@MyAnnotation(clazz = HardCoreConcrete.class)
public class SomeotherClass {
...
}

Why does this not work? My compiler complains that for clazz it expected type MyInterface! But HardCoreConcrete implements MyInterface.

Am I doing something wrong? Is this not allowed? Am i out of luck?

+3  A: 

You need

public @interface MyAnnotation {    
   Class<? extends MyInterface> clazz;
}
Thilo
Just to explain: `Class<MyInterface>` can hold **only** the `Class` object describing `MyInterface`. It can hold nothing else (and thus is pretty useless in an annotation).
Joachim Sauer
JavaRocky