注释案例类参数

时间:2018-07-23 19:28:01

标签: scala annotations case-class

我正在尝试注释案例类的参数。

this question开始,以及 并且从这个scaladoc开始,看来我正在做的事情应该起作用,但是由于某些原因,它却没有:

class Foo extends java.lang.annotation.Annotation { def annotationType = getClass }
case class Bar(@Foo @getter bar: String) 
classOf[Bar].getDeclaredMethods.map(_.getDeclaredAnnotations.length)

res66: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

用Java编写注释。另外,@getter应该用作注释。

Foo.java

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Foo {
}

Bar.scala

case class Bar(@(Foo @getter) bar: String)

然后

classOf[Bar].getDeclaredMethods.map(_.getDeclaredAnnotations.length)
// Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

Why annotations written in Scala are not accessible at runtime?

相关问题