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

Lint warns about missing annotations with CLASS retention

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      The problem is system independent.

      Tested on Debian 12.7 with:

      * javac 1.8.0_422
      * javac 23

      A DESCRIPTION OF THE PROBLEM :
      Annotations with a retention of `CLASS` are a good example of optional dependencies:

      * They are not useful at runtime.
      * They are not useful, when compiling dependent projects. Unless those projects have compile-time tools that use those annotations, the annotations can be safely ignored.

      The `javac` compiler, however generates a warning for each missing annotation, regardless of its retention:

      Cannot find annotation method 'value()' in type 'foo.Bar': class file for foo.Bar not found

      Due to this behavior, marking a library with `CLASS` annotations as an optional Maven/Gradle dependency generates unnecessary warnings to consumers and can cause compilation failures, when `-Xlint:all` is used together with `-Werror`:

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create a library A, with a `provided` (Maven) or `compileOnly` (Gradle) dependency on biz.aQute.bnd:biz.aQute.bnd.annotation:7.0.0,
      2. Annotate an element of A with `@BaselineIgnore`.
      3. Use library A in an application B.
      4. Set the compiler options for B to `-Xlint:all` and `-Werror`

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The application B compiles correctly
      ACTUAL -
      A compilation error due to a warning.

      ---------- BEGIN SOURCE ----------


      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Currently there are several sub-optimal workarounds:

      1. Library A can mark the annotation library as runtime dependency.
      2. The application B can add the same annotation library to its compile only dependencies.
      3. The application B can use `-Xlink:all,-classfile`.

      FREQUENCY : always


            acobbs Archie Cobbs
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: