简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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