Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeatable annotation target subset mismatch compiler error

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?

like image 746
buræquete Avatar asked Jan 10 '18 01:01

buræquete


2 Answers

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.

like image 161
buræquete Avatar answered Sep 23 '22 13:09

buræquete


Add annotation @Target(ElementType.TYPE) to Infos.

like image 36
Anton Tupy Avatar answered Sep 25 '22 13:09

Anton Tupy