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

Annotation processors using javax.annotation.* fail when cross compiling to 8

XMLWordPrintable

    • x86
    • other

      FULL PRODUCT VERSION :
      java version "9-ea"
      Java(TM) SE Runtime Environment (build 9-ea+118)
      Java HotSpot(TM) Client VM (build 9-ea+118, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 10

      A DESCRIPTION OF THE PROBLEM :
      Since refresh of module system in build 118, some modules are now not by default on javac classpath.
      as described in http://download.java.net/java/jdk9/changes/jdk-9+118.html

      Problem occurs when we are cross compiling to java 8 as target and where it is expected that this classes are available.
      Further on, if we use new -release 8 flag java.annotations.common module is present at compile of the classes but it is still missing from annotation processors that are used in same compile cycle.

      the only way to compile code that worked fine in Java 8 or even earlier builds of JDK9. is to compile it for -source 9 -target 9 and adding also -addmods java.annotations.common -J-addmods -Jjava.annotations.common second one is the one that passes it to annotation processor.

      Trying to use -J-addmods or -addmods in combination with -target 8 or -release 8, doesn't work as well.

      This is preventing us from compiling WildFly and related projects.

      REGRESSION. Last worked in version 8u92

      ADDITIONAL REGRESSION INFORMATION:
      Create simple annotation processor that tries to use javax.annotation.Generated

      annotation it will process

      public @interface TestAnnotation {
      }
      annotation processor

      @SupportedSourceVersion(SourceVersion.RELEASE_8)
      public class TestAnnotationProcessor extends AbstractProcessor {

          @Override
          public Set<String> getSupportedAnnotationTypes() {
              return Collections.singleton(TestAnnotation.class.getName());
          }

          @Override
          public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
              try {
                  Class generated = Class.forName(Generated.class.getName()); //try to load annotation from java.annotations.common module
              } catch (ClassNotFoundException e) {
                  processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Could not load javax.annotation.Generated class");
                  throw new RuntimeException(e);
              }
              return true;
          }
      }
      and register it in META-INF/services/javax.annotation.processing.Processor
      with content of "TestAnnotationProcessor"

      create simple class that will use annotation that is processed with processor

      @TestAnnotation //to trigger annotation processor
      public class ClassWithAnnotation {

           public void someMethod() throws Exception {
          //try to load class from java.annotations.common, to see if "main" javac classpath has classes available
              Class generated = Class.forName(Generated.class.getName());
          }
      }

      than first compile the processor and put it into jar file.
      once you have processor.jar follow the step to reproduce


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      //try to compile for java 8 target:
      javac -d out -classpath processor.jar; -source 8 -target 8 code\src\main\java\ClassWithAnnotation.java
      //new way to compile for java 8, by using new -release 8 flag
      javac -d out -classpath processor.jar; -release 8 code\src\main\java\ClassWithAnnotation.java
      //use java 9 as target and add module java.annotations.common, to javac compile
      javac -d out -classpath processor.jar; -addmods java.annotations.common code\src\main\java\ClassWithAnnotation.java
      //use java 9 as target and add module java.annotations.common, to javac and pass it to annotation processor by using -J<params>
      javac -d out -classpath processor.jar; -addmods java.annotations.common -J-addmods -Jjava.annotations.common code\src\main\java\ClassWithAnnotation.java


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      all 4 should succeed, but only 4th command results in successful compile

      even 3 is somewhat expected to work, as we do add module to javac, but it is not passed over to annotation processor.

      ACTUAL -
      1 & 2 & 3 fail with
      An annotation processor threw an uncaught exception.
      Consult the following stack trace for details.
      java.lang.NoClassDefFoundError: javax/annotation/Generated
              at processor.TestAnnotationProcessor.process(TestAnnotationProcessor.java:28)
              at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(jdk.compiler@9-ea/JavacProcessingEnvironment.java:960)
              at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(jdk.compiler@9-ea/JavacProcessingEnvironment.java:876)
              at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$2100(jdk.compiler@9-ea/JavacProcessingEnvironment.java:106)
              at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(jdk.compiler@9-ea/JavacProcessingEnvironment.java:1183)
              at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(jdk.compiler@9-ea/JavacProcessingEnvironment.java:1291)
              at com.sun.tools.javac.main.JavaCompiler.processAnnotations(jdk.compiler@9-ea/JavaCompiler.java:1229)
              at com.sun.tools.javac.main.JavaCompiler.compile(jdk.compiler@9-ea/JavaCompiler.java:903)
              at com.sun.tools.javac.main.Main.compile(jdk.compiler@9-ea/Main.java:269)
              at com.sun.tools.javac.main.Main.compile(jdk.compiler@9-ea/Main.java:144)
              at com.sun.tools.javac.Main.compile(jdk.compiler@9-ea/Main.java:55)
              at com.sun.tools.javac.Main.main(jdk.compiler@9-ea/Main.java:41)
      Caused by: java.lang.ClassNotFoundException: javax.annotation.Generated
              at java.net.URLClassLoader.findClass(java.base@9-ea/URLClassLoader.java:384)
              at java.lang.ClassLoader.loadClass(java.base@9-ea/ClassLoader.java:486)
              at java.lang.ClassLoader.loadClass(java.base@9-ea/ClassLoader.java:419)
              ... 12 more


      4 works.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public @interface TestAnnotation {
      }
      annotation processor

      @SupportedSourceVersion(SourceVersion.RELEASE_8)
      public class TestAnnotationProcessor extends AbstractProcessor {

          @Override
          public Set<String> getSupportedAnnotationTypes() {
              return Collections.singleton(TestAnnotation.class.getName());
          }

          @Override
          public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
              try {
                  Class generated = Class.forName(Generated.class.getName()); //try to load annotation from java.annotations.common module
              } catch (ClassNotFoundException e) {
                  processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Could not load javax.annotation.Generated class");
                  throw new RuntimeException(e);
              }
              return true;
          }
      }
      and register it in META-INF/services/javax.annotation.processing.Processor
      with content of "TestAnnotationProcessor"

      create simple class that will use annotation that is processed with processor

      @TestAnnotation //to trigger annotation processor
      public class ClassWithAnnotation {

           public void someMethod() throws Exception {
          //try to load class from java.annotations.common, to see if "main" javac classpath has classes available
              Class generated = Class.forName(Generated.class.getName());
          }
      }

      for compilable example see https://github.com/ctomc/jdk9-compiler
      ---------- END SOURCE ----------

            sadayapalam Srikanth Adayapalam (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved: