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

javac crashes when an annotation with a MODULE target is used on a method

    XMLWordPrintable

Details

    • generic
    • generic

    Description

      A DESCRIPTION OF THE PROBLEM :
      We've run into an AssertionError crash in JDK 8 javac when a method has an annotation with MODULE in its @Target meta-annotation. (The full stack is reproduced below.) The MODULE target was introduced in JDK 9. When an annotation targeting MODULE is used on a class, the JDK 8 javac emits a warning:

      warning: unknown enum constant ElementType.MODULE

      However, the compiler does not crash in that case. It would be very useful if the compiler also did not crash when such annotations are used on methods. This would ease adoption of the MODULE target by libraries, since they would not need to worry about causing compiler crashes in clients that still build with the JDK 8 javac. For further discussion, see this issue in the jspecify github repo:

      https://github.com/jspecify/jspecify/issues/302

      Two other things. This issue was reported before in JDK-8279557 but that issue did not have an input to reproduce, which we provide. Second, if we agree this should be fixed and on a fix strategy (e.g., emitting a warning instead of crashing), I am willing to try to submit a pull request to fix.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Place the provided A.java and B.java files in the same directory. Then, in that directory, compile A.java with a more recent javac (e.g., from JDK 17), targeting release 8:

      /path/to/jdk17/javac -source 8 -target 8 A.java

      Then, compile B.java using the JDK 8 javac:

      /path/to/jdk8/javac -cp . B.java

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      B.java compiles without crashing
      ACTUAL -
      JDK 8 javac crashes with an AssertionError:

      $ /Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home/bin/javac -source 8 -target 8 A.java
      warning: [options] bootstrap class path not set in conjunction with -source 8
      1 warning
      $ /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home/bin/javac -cp . B.java
      warning: unknown enum constant ElementType.MODULE
      An exception has occurred in the compiler (1.8.0_345). Please file a bug against the Java compiler via the Java bug reporting page (http://bugreport.java.com) after checking the Bug Database (http://bugs.java.com) for duplicates. Include your program and the following diagnostic in your report. Thank you.
      java.lang.AssertionError: annotationType(): unrecognized Attribute name MODULE (class com.sun.tools.javac.util.SharedNameTable$NameImpl)
      at com.sun.tools.javac.util.Assert.error(Assert.java:133)
      at com.sun.tools.javac.code.TypeAnnotations.annotationType(TypeAnnotations.java:231)
      at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.separateAnnotationsKinds(TypeAnnotations.java:294)
      at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.visitMethodDef(TypeAnnotations.java:1066)
      at com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:778)
      at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
      at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.scan(TypeAnnotations.java:275)
      at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:57)
      at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.visitClassDef(TypeAnnotations.java:1042)
      at com.sun.tools.javac.tree.JCTree$JCClassDecl.accept(JCTree.java:693)
      at com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)
      at com.sun.tools.javac.code.TypeAnnotations$TypeAnnotationPositions.scan(TypeAnnotations.java:275)
      at com.sun.tools.javac.code.TypeAnnotations$1.run(TypeAnnotations.java:127)
      at com.sun.tools.javac.comp.Annotate.flush(Annotate.java:152)
      at com.sun.tools.javac.comp.Annotate.enterDone(Annotate.java:129)
      at com.sun.tools.javac.comp.Enter.complete(Enter.java:512)
      at com.sun.tools.javac.comp.Enter.main(Enter.java:471)
      at com.sun.tools.javac.main.JavaCompiler.enterTrees(JavaCompiler.java:982)
      at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:857)
      at com.sun.tools.javac.main.Main.compile(Main.java:523)
      at com.sun.tools.javac.main.Main.compile(Main.java:381)
      at com.sun.tools.javac.main.Main.compile(Main.java:370)
      at com.sun.tools.javac.main.Main.compile(Main.java:361)
      at com.sun.tools.javac.Main.compile(Main.java:56)
      at com.sun.tools.javac.Main.main(Main.java:42)

      ---------- BEGIN SOURCE ----------
      $ tail -n +1 *.java
      ==> A.java <==
      import static java.lang.annotation.ElementType.METHOD;
      import static java.lang.annotation.ElementType.MODULE;

      import java.lang.annotation.Target;

      @Target({METHOD, MODULE})
      @interface A {}

      ==> B.java <==
      class B {
        @A void b() {}
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      We do not know of a workaround other than removing the annotation from methods or changing the annotation itself to not target MODULE.

      Attachments

        1. A.java
          0.2 kB
        2. B.java
          0.0 kB

        Issue Links

          Activity

            People

              vromero Vicente Arturo Romero Zaldivar
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: