繁体   English   中英

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

[英]How can I disallow to use my own annotation for all classes instead of classes implementing concrete interface

我有以下代码。 我只需要允许对ICommand接口的classes-instances使用此批注(CommandName)。 我该怎么做?

    @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();
}

在编译时,您不能这样做。 在运行时,只需检查类(由CommandName批注注释)即可,如果它们正在实现ICommand接口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM