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.