如何强制Java子类定义注释?
时间:2020-03-06 14:22:48 来源:igfitidea点击:
如果一个类定义了一个注释,是否可以以某种方式强制其子类定义相同的注释?
例如,我们有一个简单的类/子类对,它们共享" @Author @interface"。
我想做的是强制每个其他子类定义相同的@Author批注,以防止将来出现RuntimeException异常。
TestClass.java:
import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @interface Author { String name(); } @Author( name = "foo" ) public abstract class TestClass { public static String getInfo( Class<? extends TestClass> c ) { return c.getAnnotation( Author.class ).name(); } public static void main( String[] args ) { System.out.println( "The test class was written by " + getInfo( TestClass.class ) ); System.out.println( "The test subclass was written by " + getInfo( TestSubClass.class ) ); } }
TestSubClass.java:
@Author( name = "bar" ) public abstract class TestSubClass extends TestClass {}
我知道我可以在运行时枚举所有注释并检查缺少的@ Author
,但我真的很想在编译时执行此操作(如果可能)。
解决方案
我很确定在编译时不可能做到这一点。
但是,对于"单元"测试,这显然是一项任务。如果我们具有要强制执行的约定,但使用编译器很难检查或者无法检查,则"单元"测试是一种简单的检查方法。
另一种可能性是在静态分析器中实现自定义规则。这里也有很多选择。
(我将单元格用惊吓引号引起来,因为这实际上是对惯例的测试,而不是对特定单元的测试。但是,它应该与单元测试一起运行)。
我们可以在超类上使用@Inherited进行注释(例如@EnforceAuthor),并使用编译器注释(自Java 1.6起)在编译时进行更新。然后,我们可以引用该子类,并可以检查是否缺少另一个注释(例如@Author)。这将允许取消编译并显示错误消息。
我们可以在编译时使用JSR 269进行此操作。
参见:http://today.java.net/pub/a/today/2006/06/29/validate-java-ee-annotations-with-annotation-processors.html#pluggable-annotation-processing-api