[英]Are java annotations with typed class parameter possible?
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)
@Retention(RetentionPolicy.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! 我的编译器抱怨,对于clazz,它期望类型MyInterface! But HardCoreConcrete implements MyInterface. 但是HardCoreConcrete实现了MyInterface。
Am I doing something wrong? 难道我做错了什么? Is this not allowed? 这是不允许的? Am i out of luck? 我运气不好吗?
You need 你需要
public @interface MyAnnotation {
Class<? extends MyInterface> clazz;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.