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

Diagnostics for invalid type annotations in class files

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • None
    • tools

      When javac reads a class file with invalid RuntimeVisibleTypeAnnotations or RuntimeInvisibleTypeAnnotations attributes, it adds the type annotations to the corresponding type. If the attributes contain an annotation with an invalid type path that doesn't correspond to a location in the annotated type, the invalid annotation is ignored.

      The relevant logic is here: https://github.com/openjdk/jdk/blob/8bd3cd51562ff9e76fa0e3d49d38e6e19210f878/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java#L2464-L2470

      Note that there's an assertion that attributesByType is empty after type annotations have been processed, since if it was non-empty that would indicate a bug where TypeAnnotationLocator and TypeAnnotationTypeMapping disagreed with each other. But there is no assertion that attributesByPath is empty, since it may be non-empty for invalid class files containing annotations that couldn't be located on the annotated type. Also note that the details of that logic may change in JDK-8337795.

      This feature request proposes having javac emit a diagnostic for those invalid annotation.

      This change would have compatibility impact for existing class files with invalid annotations. Perhaps it could be configured with -Xlint:classfile, or a new option. If this is pursued, it would require a CSR and likely release notes.

      There are some known issues in the ecosystem where tools write invalid type annotations to class files, for example this kotlinc bug: https://youtrack.jetbrains.com/issue/KT-55128

            Unassigned Unassigned
            cushon Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: