如何强制 Java 子类定义 Annotation?
如果一个类定义了一个注释,是否可以强制其子类定义相同的注释?
例如,我们有一个简单的类/子类对,它们共享@Author @interface。
我想要做的是强制每个进一步的子类定义相同的 @Author 注释,防止将来某个地方出现 RuntimeException 。
TestClass.java:TestSubClass.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 ) );
}
}
:
@Author( name = "bar" )
public abstract class TestSubClass extends TestClass {}
我知道我可以在运行时枚举所有注释并检查缺少的 @Author
,但如果可能的话,我真的很想在编译时执行此操作。
If a class defined an annotation, is it somehow possible to force its subclass to define the same annotation?
For instance, we have a simple class/subclass pair that share the @Author @interface.
What I'd like to do is force each further subclass to define the same @Author
annotation, preventing a RuntimeException
somewhere down the road.
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 {}
I know I can enumerate all annotations at runtime and check for the missing @Author
, but I'd really like to do this at compile time, if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在超类上使用@Inherited 进行注释(例如@EnforceAuthor),并使用编译器注释(自Java 1.6 起)在编译时赶上。 然后您就有了对子类的引用,并且可以检查是否缺少另一个注释(例如@Author))。 这将允许取消编译并显示错误消息。
You could make an Annotation (e.g. @EnforceAuthor) with @Inherited on the superclass and use compiler annotations (since Java 1.6) to catch up at compile time. Then you have a reference to the subclass and can check if another Annotation (e.g. @Author)) is missing. This would allow to cancel compiling with an error message.
我非常确定这在编译时是不可能做到的。
然而,这对于“单元”测试来说是一项显而易见的任务。 如果您希望强制执行这样的约定,但编译器很难或不可能检查这些约定,那么“单元”测试是检查它们的简单方法。
另一种可能性是在静态分析器中实现自定义规则。 这里也有很多选择。
(我将单元放在引号中,因为这实际上是对约定的测试,而不是对特定单元的测试。但它应该与单元测试一起运行)。
I am quite sure that this is impossible to do at compile time.
However, this is an obvious task for a "unit"-test. If you have conventions like this that you would like enforced, but which can be difficult or impossible to check with the compiler, "unit"-tests are a simple way to check them.
Another possibility is to implement a custom rule in a static analyzer. There are many options here, too.
(I put unit in scare-quotes, since this is really a test of conventions, rather than of a specific unit. But it should run together with your unit-tests).
您可以在编译时使用 JSR 269 来做到这一点。
请参阅:http://today.java.net/pub/a/today/2006/06/29/validate-java-ee-annotations-with-annotation-processors.html#pluggable- Comments-processing-api
编辑 2020-09-20:链接已失效,存档版本在这里:https://web.archive.org/web/ 20150516080739/http://today.java.net/pub/a/today/2006/06/29/validate-java-ee-annotations-with-annotation-processors.html
You can do that with JSR 269, at compile time.
See : http://today.java.net/pub/a/today/2006/06/29/validate-java-ee-annotations-with-annotation-processors.html#pluggable-annotation-processing-api
Edit 2020-09-20: Link is dead, archived version here : https://web.archive.org/web/20150516080739/http://today.java.net/pub/a/today/2006/06/29/validate-java-ee-annotations-with-annotation-processors.html