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

4.7.20.2: Clarify placement of type annotations on nested types

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 12
    • 11
    • specification
    • vm

      (See http://mail.openjdk.java.net/pipermail/compiler-dev/2018-November/012649.html )

      From JVMS11 4.7.20.2:

      > If the value of path_length is 0, then the annotation appears directly on the type itself.

      From the examples in Table 4.7.20.2-E ("type_path structures for @C Outer . @B Middle . @A Inner"), for type `Outer.Middle.Inner` the annotation `@C` with `type_path=[]` appears on `Outer`. That requires interpreting "the type itself" of `Outer.Middle.Inner` as `Outer`. However the full type referred to by that qualified name should be `Outer.Middle.Inner`; `Outer` is merely an enclosing type.

      Additionally, the annotation `@A` with `type_path=[INNER, INNER]` appears on `Inner`, and there's no discussion of whether the member classes are inner classes or static member classes. So we'd expect implementations to emit the same type path for `@A` regardless of whether `Inner` and `Middle` were static. However, the original intent of the JSR 308 EG included that distinction, and that intent was reflected in the implementation: javac emits `type_path_kind=1` type_path entries only for nested types where there are multiple locations which are admissible for type annotations (see JLS 9.7.4).

      ## Proposed text for 4.7.20.2

      Original: "If a nested type T1.T2 is used in a declaration or expression, then an annotation may appear on the name of the top level type or any member type."
      Proposed: "If a nested type T1.T2 is used in a declaration or expression, then an annotation may appear on the name of the innermost member type and any enclosing type for which a type annotation is admissible (JLS 9.7.4)."

      Original: "If the value of path_length is 0, then the annotation appears directly on the type itself."
      Proposed: "If the value of path_length is 0,
        * and the type being annotated is a nested type, then the annotation applies to the outermost part of the type for which a type annotation is admissible,
        * otherwise the annotation appears directly on the type itself."

      ## Additional examples

      For table 4.7.20.2-E "type_path structures for @A Outer . @B Middle . @C Inner" :

      class Outer {
        class Middle {
          class Inner {}
        }
      }

      @A; path_length=0; []
      @B; path_length=1; [{type_path_kind: 1; type_argument_index: 0}]
      @C; path_length=2; [{type_path_kind: 1; type_argument_index: 0}, {type_path_kind: 1; type_argument_index: 0}]

      For table 4.7.20.2-F "type_path structures for Outer . @A MiddleStatic . @B Inner" :

      class Outer {
        static class MiddleStatic {
          class Inner {}
        }
      }

      Type annotations on the simple name `Outer` are not admissible since the type name to its right (`MiddleStatic`) does not refer to an inner class of `Outer`.
      @A; path_length=0; []
      @B; path_length=1; [{type_path_kind: 1; type_argument_index: 0}]

      For table 4.7.20.2-G "type_path structures for Outer . MiddleStatic . @A InnerStatic" :

      class Outer {
        static class MiddleStatic {
          static class InnerStatic {}
        }
      }

      Type annotations on the simple name `Outer` are not admissible since the type name to its right (`MiddleStatic`) does not refer to an inner class of `Outer`.
      Type annotations on the simple name `MiddleStatic` are not admissible since the type name to its right (`InnerStatic`) does not refer to an inner class of `MiddleStatic`.
      @A; path_length=0; []

            abuckley Alex Buckley
            cushon Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: