10

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?

buræquete
  • 14,226
  • 4
  • 44
  • 89

2 Answers2

13

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.

buræquete
  • 14,226
  • 4
  • 44
  • 89
2

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

Anton Tupy
  • 951
  • 5
  • 16