简体   繁体   English

如何不允许对所有类使用我自己的注释,而不是对实现具体接口的类进行使用

[英]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.

相关问题 Spring:注入实现接口的所有具体类的列表 - Spring: Injecting list of all concrete classes implementing an interface 如何正确组织一组在不同包中实现接口的具体类? - How to correctly organize a set of concrete classes implementing an interface in different packages? Android:如何在自己的类中使用为“活动”定义的方法? - Android: How can I use methods defined for Activities in my own classes? 如何禁用WebApplicationInitializer类的Servlet类路径扫描,但仍显式使用自己的实例? - How can I disable Servlet classpath scanning for WebApplicationInitializer classes but still explictly use my own instance? 使用 JAX-WS 时如何使用我自己的自定义客户端类? - How can I use my own custom client classes when using JAX-WS? 如何检索实现自定义接口的所有类? - How can I retrieve all classes that implement a custom interface? 我可以为具有特定注释的类创建通用接口吗? - Can I make a generic interface for classes that have a specific annotation? 如何获得在特定OSGi Bundle中实现接口的所有类 - How get all classes implementing an interface in a specific OSGi Bundle 如何使用自己的 Swagger 注释? - How can I use my own annotation for Swagger? 如何在实现接口的所有类中设计公共静态方法 - How to design a common static method in all classes implementing an interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM