How do I force a Java subclass to define an Annota

2019-02-12 10:13发布

问题:

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.

回答1:

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



回答2:

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



回答3:

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.