JDK-8315447 : Invalid Type Annotation attached to a method instead of a lambda
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 22
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2023-08-31
  • Updated: 2025-03-27
  • Resolved: 2025-03-25
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 25
25 b16Fixed
Related Reports
Blocks :  
Relates :  
Relates :  
Description
Javac compiles following source:
```
import java.lang.annotation.Repeatable;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.TYPE_USE;
import java.util.function.Supplier;

@Target(TYPE_USE)
@Repeatable(AC.class)
@interface A {
}

@Target(TYPE_USE)
@interface AC {
    A[] value();
}

@Target(TYPE_USE)
@interface B {}

class Test {
    void test() {
        Supplier<Integer> s = () -> (@A @A @B Integer) 1;
    }
}
```

Into a class with @AC annotation attached to the test method body and with invalid bytecode offset:
```
  void test();
    descriptor: ()V
    flags: (0x0000)
    Code:
      stack=1, locals=2, args_size=1
         0: invokedynamic #7,  0              // InvokeDynamic #0:get:()Ljava/util/function/Supplier;
         5: astore_1
         6: return
      RuntimeInvisibleTypeAnnotations:
        0: #30(#31=[@#32(),@#32()]): CAST, offset=4, type_index=0
          AC(
            value=[@A,@A]
          )
```

The correct container annotation AC should be located in the lambda synthetic method with B.

This is preventing the migration of CombinationsTargetTest3 to the ClassFile API; the test should be moved to CF API as part of the fix.


Comments
Changeset: f5a0db43 Branch: master Author: Chen Liang <liach@openjdk.org> Date: 2025-03-25 19:01:22 +0000 URL: https://git.openjdk.org/jdk/commit/f5a0db43b76ea58f54d87c49d97c7d87ee1b4ba6
25-03-2025

A pull request was submitted for review. Branch: master URL: https://git.openjdk.org/jdk/pull/24206 Date: 2025-03-24 20:13:42 +0000
24-03-2025

It seems this is caused by LambdaToMethod::apportionTypeAnnotations missing a call to fix container annotation position before using the position information.
24-03-2025

Per my debugging, it appears that when a container annotation is generated for @Repeatable type annotations, the container has a wrong loc, so it doesn't get outputted into the generated bytecode. However, I have no idea how the wrong loc is attached to the container (such as in which pass, for when the container is generated, it copies loc info from the first type anno, but the bytecode locations are not initialized yet) for I'm not a javac guru, and I hope some other engineers experienced in javac can help diagnose the exact cause.
31-08-2023

The bug affect conversion of test/langtools/tools/javac/annotations/typeAnnotations/classfile/CombinationsTargetTest3.java to Classfile API. Parsing of type annotation with invalid bytecode offset fails.
31-08-2023