Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ecrabor8 opened this issue Apr 12, 2025 · 2 comments

Comments

@ecrabor8
Copy link

ecrabor8 commented Apr 12, 2025

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.

@ecrabor8 ecrabor8 added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 12, 2025
@ecrabor8
Copy link
Author

ecrabor8 commented Apr 12, 2025

sorry missing the markdown in the code above. @foo is really @Foo as you will note.

@Gedochao
Copy link
Contributor

@ecrabor8 I took the liberty of wrapping code from your original post in Markdown to improve readability.

@Gedochao Gedochao added area:annotations and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants