[英]How can I disallow to use my own annotation for all classes instead of classes implementing concrete interface
I have the following code. 我有以下代码。 I need to allow usage of this annotation (CommandName) ONLY for classes-instances of ICommand interface. 我只需要允许对ICommand接口的classes-instances使用此批注(CommandName)。 How can I do it? 我该怎么做?
@Retention(RetentionPolicy.RUNTIME) // Make this annotation accessible at runtime via reflection.
@Target({ElementType.TYPE}) // This annotation can only be applied to class methods.
public @interface CommandName {
String value();
}
In compile time, you cant do that. 在编译时,您不能这样做。 In runtime, just check the classes - which is annotated by CommandName annotation - if they are implementing the ICommand interface. 在运行时,只需检查类(由CommandName批注注释)即可,如果它们正在实现ICommand接口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.