让我们假设我有这样的接口:是否可以使用类型化的类参数进行java注释?
public interface MyInterface {
void doStuff();
}
有了具体的实现:
public class HardCoreConcrete implements MyInterface {
void doStuff() {
// i really do stuff, honest
}
}
再假设我有这样的注解:
@Target(ElementType.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
Class<MyInterface> clazz;
}
它会像这样使用:
@MyAnnotation(clazz = HardCoreConcrete.class)
public class SomeOtherClass {
...
}
为什么这不起作用?我的编译器抱怨说,对于clazz,它期望输入MyInterface!但HardCoreConcrete实现MyInterface。
我做错了什么?这是不允许的?我运气不好吗?
那么从你的响应时间,你做了2分钟!嘿。 – JavaRocky 2010-02-22 02:30:47
另请参阅http://stackoverflow.com/questions/9940610/how-do-you-ensure-a-class-annotation-parameter-extends-an-interface-at-compile-t。 – Gregor 2013-03-12 14:34:33