例如,使用以下定义
class A
class B extends A
trait MyTrait {
@MyAnnotation
def foo(): A
}
class MyClass extends MyTrait {
@MyAnnotation
def foo() = new B
}
Scala生成一个合成方法foo(),它在MyClass中返回A.查看生成的字节码(使用javap),我可以看到:
public net.bdew.mytest.test.B foo();
flags: ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: new #15 // class net/bdew/mytest/test/B
3: dup
4: invokespecial #19 // Method net/bdew/mytest/test/B."<init>":()V
7: areturn
LocalVariableTable:
Start Length Slot Name Signature
0 8 0 this Lnet/bdew/mytest/test/MyClass;
LineNumberTable:
line 22: 0
RuntimeVisibleAnnotations:
0: #13()
这是真正的方法,并且注释存在于其中。
public net.bdew.mytest.test.A foo();
flags: ACC_PUBLIC, ACC_BRIDGE, ACC_SYNTHETIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokevirtual #24 // Method foo:()Lnet/bdew/mytest/test/B;
4: areturn
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lnet/bdew/mytest/test/MyClass;
LineNumberTable:
line 20: 0
这是合成的,没有注释。有没有办法让它出现在它上面?
编辑:澄清MyAnnotation是在java中定义的,具有运行时保留。