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

javac fails to detect missing import with annotation processing enabled

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P4 P4
    • None
    • 9
    • tools
    • None

      javac 9 fails to report an error for a missing import in the following demo. The missing import is unused, and another class on the classpath contains a reference to the missing symbol. The issue only reproduces with annotation processing enabled.

      This affects 9-ea+177 but not 1.8.0_152-ea-b04 and earlier versions.

      Repro:

      === ./P.java
      import static java.nio.charset.StandardCharsets.UTF_8;

      import java.io.IOError;
      import java.io.IOException;
      import java.io.OutputStream;
      import java.util.Set;
      import javax.annotation.processing.AbstractProcessor;
      import javax.annotation.processing.RoundEnvironment;
      import javax.annotation.processing.SupportedAnnotationTypes;
      import javax.lang.model.element.TypeElement;

      @SupportedAnnotationTypes("*")
      public class P extends AbstractProcessor {
        boolean first = true;
        @Override
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
          if (first) {
            try (OutputStream os =
                processingEnv.getFiler().createSourceFile("no.such.Hello").openOutputStream()) {
              os.write("package no.such; public class Hello {}".getBytes(UTF_8));
            } catch (IOException e) {
              throw new IOError(e);
            }
            first = false;
          }
          return false;
        }
      }
      === ./no/such/Bar.java
      package no.such;
      public class Bar {
        Foo.Inner i;
      }
      === ./no/such/Foo.java
      package no.such;
      public class Foo {
        public static class Inner {}
      }
      === ./no/such/T.java
      package no.such;
      import no.such.Foo.Inner;
      @Deprecated
      class T {
        Bar b;
        Hello h;
      }

      $ javac P.java no/such/Bar.java no/such/Foo.java
      $ rm no/such/Foo*
      $ javac -processor P no/such/T.java

      The compilation succeeds unexpectedly. T.java references no.such.Foo, which was deleted and is not present on the classpath or sourcepath. Performing the same compilation except compiling the generated source explicitly fails with the expected error:

      $ javac -sourcepath : -cp lib.jar no/such/T.java no/such/Hello.java
      no/such/T.java:2: error: package no.such.Foo does not exist
      import no.such.Foo.Inner;
                        ^
      1 error

      I suspect that the error is being deferred when it is first detected since the missing symbol could be generated during a subsequent annotation processing round, and javac is failing to detect the error during later rounds.

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

              Created:
              Updated:
              Resolved: