Skip to content

Trait method parameter annotated with RetentionPolicy.RUNTIME annotation is not indicated in .class file for the method parameter #22991

Open
@ecrabor8

Description

@ecrabor8

Compiler version

3.6.4

Minimized code

ls $(find . -name "*.scala") $(find . -name "*.java")
# ./bar/src/bar/Bar.scala	./foo/src/foo/Foo.java
// cat build.mill
import mill._, javalib._, scalalib._

object foo extends JavaModule {
}

object bar extends ScalaModule {
  def scalaVersion = "3.6.4"
  def moduleDeps = Seq(foo)
}
// cat $(find . -name "*.java")
package foo;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
public @interface Foo {
}
// cat $(find . -name "*.scala")
package bar

import foo.Foo

trait Barly {
  def bar(@Foo v: Int) = ()
}

class Bar extends Barly{
}

Output

Run

./mill bar.assembly
javap -v -cp  ./out/bar/assembly.dest/out.jar bar.Bar

.....


  public void bar(int);    
    descriptor: (I)V       
    flags: (0x1041) ACC_PUBLIC, ACC_BRIDGE, ACC_SYNTHETIC
    Code:
      stack=2, locals=2, args_size=2
         0: aload_0        
         1: iload_1        
         2: invokestatic  #20                 // InterfaceMethod bar/Barly.bar$:(Lbar/Barly;I)V
         5: return
      LineNumberTable:
        line 9: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       6     0  this   Lbar/Bar;
            0       6     1     v   I
    MethodParameters:
      Name                           Flags    
      v                              final

Now

Do the same thing after cleaning up the out directory and changing to

  def scalaVersion = "2.13.16"

in the build.mill
.....

 public void bar(int);
    descriptor: (I)V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=2, locals=2, args_size=2
         0: aload_0
         1: iload_1
         2: invokestatic  #18                 // InterfaceMethod bar/Barly.bar$:(Lbar/Barly;I)V
         5: return
      LineNumberTable:
        line 9: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       6     0  this   Lbar/Bar;
            0       6     1     v   I
    RuntimeVisibleParameterAnnotations:
      parameter 0:
        0: #14()
          foo.Foo
    MethodParameters:
      Name                           Flags
      v                              final

Expectation

The expectation is that RuntimeVisibleParameterAnnotations remain in the scala 3 .class file as seen in the scala 2 .class file.
IE This section not seen in scala 3 but occurred in scala 2:

 RuntimeVisibleParameterAnnotations:
      parameter 0:
        0: #14()
          foo.Foo

Of course the javap output is reflecting that the runtime method parameter annotations are not present for the trait method in the scala 3 compiled file. This is a problem trying to convert a scala 2 application.

The issue has occurred with use of Java Jersey JaxRS annotations for a web project trying to convert from Scala 2 to Scala 3. Nothing works in Scala 3 regarding traits with JaxRS annotations on methods required in subclasses.
It is further observed that the issue is with trait methods parameter annotations and not in class methods parameter annotations.
For eg adding a bar2 method to the class as:
....

class Bar extends Barly{
  def bar2(@Foo v: Int) = ()
}

has following seen with javap output
....

 public void bar2(int);
    descriptor: (I)V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=0, locals=2, args_size=2
         0: return
      LineNumberTable:
        line 10: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       1     0  this   Lbar/Bar;
            0       1     1     v   I
    RuntimeVisibleParameterAnnotations:
      parameter 0:
        0: #23()
          foo.Foo
    MethodParameters:
      Name                           Flags
      v                              final

Thank you for the attention.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions