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

Annotations not visible on model elements before they are generated

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 18
    • tools
    • None
    • behavioral
    • minimal
    • The fix under consideration is restoring historical behavior from pre-JDK12 days so as to address a regression - hence the risk is minimal.
    • Java API

      Summary

      Ensure that when a program refers to a missing annotation type, the model elements still expose the missing type, so as to be compliant with javax.lang.model specification

      Problem

      The fix for JDK-8206325 ("AssertionError in TypeSymbol.getAnnotationTypeMetadata") introduced an inadvertent side effect as a result of which only valid annotation types are exposed in model elements. This goes against the javax.lang.model specification which states:

      If a program refers to a missing class or interface Xyz, the returned model must contain
      no less information than if the declaration of class or interface Xyz were assumed to be 
      "class Xyz {}", "interface Xyz {}", "enum Xyz {}", "@interface Xyz {}", or "record Xyz {}".

      Solution

      As part of the fix for JDK-8206325, the code chunk that gathers up the annotations on a symbol was surrounded by a check that tests whether the annotation is indeed a valid annotation. This check is tighter than it ought to be as it would preclude missing annotation types from being gathered into the set of annotations on a target symbol.

      The solution is to relax the check so we tolerate missing types and expose them in the model.

      Specification

      Excerpt from the documentation on the package-info for javax.lang.model.element:

      During annotation processing, operating on incomplete or erroneous 
      programs is necessary; however, there are fewer guarantees about 
      the nature of the resulting model. If the source code is not syntactically 
      well-formed or has some other irrecoverable error that could not be 
      removed by the generation of new classes or interfaces, a model may 
      or may not be provided as a quality of implementation issue. If a 
      program is syntactically valid but erroneous in some other fashion, 
      any returned model must have no less information than if all the 
      method bodies in the program were replaced by "throw new RuntimeException();". 
      If a program refers to a missing class or interface Xyz, the returned model 
      must contain no less information than if the declaration of class or 
      interface Xyz were assumed to be "class Xyz {}", "interface Xyz {}", 
      "enum Xyz {}", "@interface Xyz {}", or "record Xyz {}". If a program 
      refers to a missing class or interface Xyz<K1, ... ,Kn>, the returned 
      model must contain no less information than if the declaration of 
      Xyz were assumed to be "class Xyz<T1, ... ,Tn> {}" or 
      "interface Xyz<T1, ... ,Tn> {}" 

      To explicit specification update; restoring previous behavior.

      Link to Pull Request: https://github.com/openjdk/jdk/pull/6662

            sadayapalam Srikanth Adayapalam (Inactive)
            cushon Liam Miller-Cushon
            Maurizio Cimadamore
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: