JDK-8315447 : Invalid TypeAnnotation attached to a method body
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2023-08-31
  • Updated: 2024-06-28
Related Reports
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]
          )
```

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
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