I have the following annotation;
@Repeatable(Infos.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.Type, ElementType.Constructor})
public @interface Info {
String[] value() default {};
}
and as you see, it is repeatable, and using the wrapper class Infos
which is;
@Retention(RetentionPolicy.RUNTIME)
public @interface Infos {
Info[] value();
}
but I am getting the following compiler error on Info
class;
target of container annotation is not a subset of target of this annotation
What is the reason and solution to this error?
The problem is due to the lack of @Target
definition on container annotation class Infos
, since Info
has the following targets;
@Target({ElementType.Type, ElementType.Constructor})
public @interface Info { .. }
which means this annotation can only be put on types and constructors, but container class should also have some targets defined since it is an annotation itself. Since the warning also mentions, this set of targets should be a subset of the original annotations targets. For example;
@Target(ElementType.Type)
public @interface Infos { .. }
that will allow us to repeat Info
annotation on types, but not on constructors;
@Info(..)
@Info(..)
class SomeClass { .. }
Also it should be noted that, you cannot add a new target type to the container annotation, since the main annotation does not contain it as a target, this will be meaningless. Since again;
container class can only contain a subset of main annotation target set.
Add annotation @Target(ElementType.TYPE) to Infos.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With