JDK-8341779 : [REDO BACKPORT] type annotations are not visible to javac plugins across compilation boundaries (JDK-8225377)
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 13,21
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2024-10-08
  • Updated: 2025-06-25
  • Resolved: 2025-04-16
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 21
21.0.8 b01Fixed
Related Reports
CSR :  
Causes :  
Cloners :  
Relates :  
Relates :  
Sub Tasks
JDK-8341780 :  
Description
javac fails to associate type annotations with TypeMirrors for some symbols loaded from the classpath, which prevents plugins from accessing those annotations across compilation boundaries. The annotations are present if the same symbol is compiled from source in the compilation where the plugin runs.

=== ./test/B.java
abstract class B extends A {}
=== ./test/A.java
import java.lang.annotation.ElementType;
import java.util.List;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE_USE)
@Retention(RetentionPolicy.RUNTIME)
@interface TypeAnnotation {}

abstract class A implements List<@TypeAnnotation String> {}
=== ./plugin/p/P.java
package p;

import com.sun.source.util.JavacTask;
import com.sun.source.util.Plugin;
import com.sun.source.util.TaskEvent;
import com.sun.source.util.TaskListener;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;

public class P implements Plugin {

  @Override
  public String getName() {
    return "P";
  }

  @Override
  public void init(JavacTask javacTask, String... strings) {
    javacTask.addTaskListener(
        new TaskListener() {
          @Override
          public void finished(TaskEvent e) {
            if (e.getKind() != TaskEvent.Kind.ENTER) {
              return;
            }
            TypeElement b = javacTask.getElements().getTypeElement("B");
            for (TypeMirror i :
                ((TypeElement) ((DeclaredType) b.getSuperclass()).asElement()).getInterfaces()) {
              System.err.printf("%s %s\n", i, i.getAnnotationMirrors());
            }
          }
        });
  }
}
=== ./plugin/module-info.java
module p { 
  requires transitive jdk.compiler; 
  provides com.sun.source.util.Plugin with p.P; 
} 
===

$ javac $(find plugin -name '*.java')

# when both compilation units are compiled from source, the type annotations are visible

$ javac --processor-module-path plugin -Xplugin:P test/A.java test/B.java 
java.util.List<@TypeAnnotation java.lang.String> 
java.util.List<@TypeAnnotation java.lang.String> 

# when 'A' is loaded from the classpath, the type annotations on its supertype are not visible

$ javac --processor-module-path plugin -Xplugin:P -classpath test test/B.java 
java.util.List<java.lang.String> 

Comments
There was an issue discovered late in the 21.0.8 cycle and this change isn't enabled by default. '-XDaddTypeAnnotationsToSymbol=true' needs to be added to enable it in JDK 21.0.8+. See JDK-8360406.
25-06-2025

Changeset: 42165463 Branch: master Author: Liam Miller-Cushon <cushon@openjdk.org> Committer: Severin Gehwolf <sgehwolf@openjdk.org> Date: 2025-04-16 08:18:29 +0000 URL: https://git.openjdk.org/jdk21u/commit/421654630dfcc4b697fdd61c8a46c1d2f899d8fb
30-04-2025

Changeset: 42165463 Branch: master Author: Liam Miller-Cushon <cushon@openjdk.org> Committer: Severin Gehwolf <sgehwolf@openjdk.org> Date: 2025-04-16 08:18:29 +0000 URL: https://git.openjdk.org/jdk21u-dev/commit/421654630dfcc4b697fdd61c8a46c1d2f899d8fb
16-04-2025

Adding 22-na since JDK-8225377 didn't get backed out from JDK 22 and better. Therefore no redo necessary. It got backed out from JDK 21 and earlier releases with JDK-8322883.
28-03-2025

[jdk21u-fix-request] Approval Request from Liam Miller-Cushon for backport. Fixes a bug in javac's implementation of TypeMirror to allow accessing type-use annotations on types loaded from class files. Risk: See discussion in CSR JDK-8346471. Testing: Tier 1 tests pass. The fix is also present in JDK 22 and newer releases. Dependencies: If approved, JDK-8337795 and JDK-8337998 should also be backported.
26-03-2025

A pull request was submitted for review. Branch: master URL: https://git.openjdk.org/jdk21u-dev/pull/1251 Date: 2024-12-17 21:28:33 +0000
09-01-2025

There was some related discussion in: https://mail.openjdk.org/pipermail/compiler-dev/2024-July/027344.html https://mail.openjdk.org/pipermail/compiler-dev/2024-August/027446.html
04-12-2024