0

Let's assume I have the following interface annotation @MyAnnotation.

@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
@interface MyAnnotation {
}

I want to implement in such a way that when someone consumes the annotation at compile time they will be told that they must extend a certain base class for example

@MyAnnotation
class Concrete extends Base {

}

The Base class is an abstract class that contains helper methods etc. So the idea is if anyone wishes to use the annotation MyAnnotation they would get a helpful hint to say that they must extend class Base. Is this possible at all?

I believe the type checker framework (https://checkerframework.org/manual/) accomplishes similar aspirations to what I'm after in that it has a SubtypeOf annotation but that seems to accept classes that extend java.lang.annotation.Annotation. Thanks in advance as to any help with the above.

bhreinb
  • 181
  • 2
  • 13
  • do you use any framework that supports annotation processing or AOP? – energy2522 May 23 '22 at 13:33
  • For now I'm not using annotation processing or AOP. I thought this could be possible to do by providing a class that extends class `javax.annotation.processing.AbstractProcessor`? – bhreinb May 23 '22 at 16:43
  • as you want to use `javax.annotation.processing.AbstractProcessor`, this link may help you: https://www.baeldung.com/java-annotation-processing-builder. The strategy that I would pick to identify if a Class is extended or not is reflection. You get the Class and then get the information from it. – energy2522 May 23 '22 at 19:09

0 Answers0