Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8085485 | emb-9 | Andreas Lundblad | P4 | Resolved | Fixed | team |
Code:
$ cat RepAnno.java
import java.lang.reflect.*;
import java.lang.annotation.*;
@Target({ElementType.FIELD, ElementType.METHOD})
@Repeatable(TC.class)
@interface T { int value(); }
@Target(ElementType.METHOD)
@interface TC { T[] value(); }
public class RepAnno {
@T(1) @T(2) public int field;
@T(1) @T(2) public void method() {};
}
Compile result:
$ javac RepAnno.java
RepAnno.java:13: error: containing annotation type (TC) is applicable to more targets than repeatable annotation type (T)
@T(1) @T(2) public int field;
^
1 error
Compiler detects that TC (target: METHOD) is applicable to more targets than T (target: FIELD, METHOD). It's confusing error message because developer can see that the situation is opposite and T is applicable to more targets than TC.
JSL (9.6.3. Repeatable Annotation Types) says:
"4. T is applicable to at least the same kinds of program element as TC (§9.6.4.1). Specifically, if the kinds of program element where T is applicable are denoted by the set m1, and the kinds of program element where TC is applicable are denoted by the set m2, then each kind in m2 must occur in m1, except that:
* If the kind in m2 is java.lang.annotation.ElementType.ANNOTATION_TYPE, then at least one of java.lang.annotation.ElementType.ANNOTATION_TYPE or java.lang.annotation.ElementType.TYPE or java.lang.annotation.ElementType.TYPE_USE must occur in m1.
* If the kind in m2 is java.lang.annotation.ElementType.TYPE, then at least one of java.lang.annotation.ElementType.TYPE or java.lang.annotation.ElementType.TYPE_USE must occur in m1.
* If the kind in m2 is java.lang.annotation.ElementType.TYPE_PARAMETER, then at least one of java.lang.annotation.ElementType.TYPE_PARAMETER or java.lang.annotation.ElementType.TYPE_USE must occur in m1."
I.e. in our case m2 = { METHOD }, m1 = { FIELD, METHOD } and definitely each kind in m2 occurs in m1. Also the situation is not described in the list of exceptions.
The error message should inform that target list of container is not applicable in this situation. It would be correct and informative.
$ cat RepAnno.java
import java.lang.reflect.*;
import java.lang.annotation.*;
@Target({ElementType.FIELD, ElementType.METHOD})
@Repeatable(TC.class)
@interface T { int value(); }
@Target(ElementType.METHOD)
@interface TC { T[] value(); }
public class RepAnno {
@T(1) @T(2) public int field;
@T(1) @T(2) public void method() {};
}
Compile result:
$ javac RepAnno.java
RepAnno.java:13: error: containing annotation type (TC) is applicable to more targets than repeatable annotation type (T)
@T(1) @T(2) public int field;
^
1 error
Compiler detects that TC (target: METHOD) is applicable to more targets than T (target: FIELD, METHOD). It's confusing error message because developer can see that the situation is opposite and T is applicable to more targets than TC.
JSL (9.6.3. Repeatable Annotation Types) says:
"4. T is applicable to at least the same kinds of program element as TC (§9.6.4.1). Specifically, if the kinds of program element where T is applicable are denoted by the set m1, and the kinds of program element where TC is applicable are denoted by the set m2, then each kind in m2 must occur in m1, except that:
* If the kind in m2 is java.lang.annotation.ElementType.ANNOTATION_TYPE, then at least one of java.lang.annotation.ElementType.ANNOTATION_TYPE or java.lang.annotation.ElementType.TYPE or java.lang.annotation.ElementType.TYPE_USE must occur in m1.
* If the kind in m2 is java.lang.annotation.ElementType.TYPE, then at least one of java.lang.annotation.ElementType.TYPE or java.lang.annotation.ElementType.TYPE_USE must occur in m1.
* If the kind in m2 is java.lang.annotation.ElementType.TYPE_PARAMETER, then at least one of java.lang.annotation.ElementType.TYPE_PARAMETER or java.lang.annotation.ElementType.TYPE_USE must occur in m1."
I.e. in our case m2 = { METHOD }, m1 = { FIELD, METHOD } and definitely each kind in m2 occurs in m1. Also the situation is not described in the list of exceptions.
The error message should inform that target list of container is not applicable in this situation. It would be correct and informative.
- backported by
-
JDK-8085485 Confusing (incorrect) error message on repeatable annotations
- Resolved
- relates to
-
JDK-8073534 Confusing / incorrect error message regarding annotations on non-declarations
- Closed