Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8043984

Confusing (incorrect) error message on repeatable annotations

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 9
    • 8, 9
    • tools
    • None
    • b61
    • Not verified

        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.

              alundblad Andreas Lundblad (Inactive)
              spikalev Sergei Pikalev (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Created:
                Updated:
                Resolved: