简体   繁体   中英

Java - Force a class implementing an interface to declare an annotation

I'm trying to force class SomeImpl to declare an annotation @SomeAnnotation if it is implementing interface SomeInterface. Solution can modify SomeInterface declaration.

public interface SomeInterface {}

public class SomeImpl implements SomeInterface -> compilation error

@SomeAnnotation
public class SomeInterfaceImpl implements SomeInterface -> compilation ok

Use case: I'm using objects implementing SomeInterface in a method (not written by me, it's an external library) that always read the annotation @SomeAnnotation and it can not be empty or default.

Thanks!

看来这只能使用注释处理器( https://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/Processor.html )来完成。

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