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

Surprising 'multiple elements' behaviour from getTypeElement when cross-compiling with --release

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 16
    • 14
    • core-libs
    • None

      The following example involves an annotation processor that uses Elements#getTypeElement to try to load com.sun.tools.javac.util.Context (which is defined in jdk.compiler). A definition of the class is also on the -classpath, from a JDK 8 tools.jar.

      === P.java ===
      import java.util.Set;
      import javax.annotation.processing.AbstractProcessor;
      import javax.annotation.processing.RoundEnvironment;
      import javax.annotation.processing.SupportedAnnotationTypes;
      import javax.lang.model.SourceVersion;
      import javax.lang.model.element.TypeElement;
      import javax.tools.Diagnostic;

      @SupportedAnnotationTypes("*")
      public class P extends AbstractProcessor {
        @Override
        public SourceVersion getSupportedSourceVersion() {
          return SourceVersion.latestSupported();
        }

        @Override
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
          TypeElement typeElement =
              processingEnv.getElementUtils().getTypeElement("com.sun.tools.javac.util.Context");
          processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, String.valueOf(typeElement));
          return false;
        }
      }
      === T.java ===
      @Deprecated class T {}
      ===

      With the default target language level, a 'Multiple elements' note gets logged, and the element cannot be loaded:

      ```
      $JAVA14_HOME/bin/javac P.java
      $JAVA14_HOME/bin/javac -fullversion -processor P -cp $JAVA8_HOME/lib/tools.jar:. T.java
      javac full version "14-ea+30-1385"
      Note: Multiple elements named 'com.sun.tools.javac.util.Context' in modules 'unnamed module, jdk.compiler' were found by javax.lang.model.util.Elements.getTypeElement.
      Note: null
      Note: null
      ```

      When cross-compiling (with --release 13) no note is logged, and the element is loaded.

      ```
      $JAVA14_HOME/bin/javac -processor P -cp $JAVA8_HOME/lib/tools.jar:. --release 13 T.java
      Note: com.sun.tools.javac.util.Context
      Note: com.sun.tools.javac.util.Context
      ```

      Is this inconsistency deliberate? If not, which of the two examples is correct?

      Is the issue with `--release 13` that the ct.sym data for previous releases only includes modules that are supposed to be visible, so it doesn't have enough information to detect the clash?

            jlahoda Jan Lahoda
            cushon Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: