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

Unnecessary "unknown enum constant" warning emitted by javac when dependency has optional annotations with enums

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 19
    • tools
    • generic
    • generic

      With this setup:

      // p.E
      package p;
      public enum E { E }

      // p.A
      package p;
      import java.lang.annotation.*;
      @Retention(RetentionPolicy.RUNTIME)
      public @interface A {
          p.E e();
      }

      // q.Test
      package q;
      import p.*;
      @A(e = E.E)
      public class Test {}

      // x.Test2
      package x;
      public class Test2 {
          q.Test test;
      }

      We can do:

      $ javac q/Test.java
      $ rm -Rf p
      $ javac x/Test2.java


      This produces a warning:

      warning: unknown enum constant E.E
        reason: class file for p.E not found
      1 warning


      The warning is likely due to the implementation in https://bugs.openjdk.org/browse/JDK-6550655, which is very similar to this case, but not identical. In JDK-6550655, only the enum p.E is deleted, not the annotation p.A, in case of which the warning certainly makes sense as a runtime error could occur when accessing the annotation using reflection API, due to the missing enum.

      In this case, there can't be any runtime exception, because the annotation itself is absent. I think that no warning should be emitted.

      More context:
      - https://stackoverflow.com/q/75864102/521799
      - https://bugs.openjdk.org/browse/JDK-6550655
      - https://github.com/jOOQ/jOOQ/issues/14865 (real world use-case)

      The real world use-case is:
      - The p package containing annotations and enums is JAXB
      - The q package containing library code that references the annotations is a library whose Maven dependency on p is optional.
      - The x package is user code, depending on the q package, wanting to opt out of the transitive dependency p, which should be fine, but isn't.

      There is currently no way to suppress this warning, so compiling x with -Werror produces an unnecessary error.

            jlahoda Jan Lahoda
            leder Lukas Eder
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: