[英]Annotating constructor parameters in Scala
Annotating constructor parameters seems to do nothing when compiled to bytecode. 编译为字节码时,注释构造函数参数似乎无效。 I get no compiler warnings either. 我也没有编译器警告。
The following works. 以下作品。 getAnnotations
for the name
field returns javax.annotation.Nullable
. name
字段的getAnnotations
返回javax.annotation.Nullable
。
class Person {
@Nullable var name: String = _;
}
The following doesn't, neither with val
or var
. 以下没有, val
或var
都没有。
class Person(@Nullable var name: String)
This is probably not intentional, so is there something I am missing or should I go file a bug report? 这可能不是故意的,所以我有什么遗漏或者我应该提交错误报告吗?
You need to specify what should get annotated when you specify annotations on constructor parameters. 在构造函数参数上指定注释时,需要指定应注释的内容。
To do that annotate your annotation with one ore more annotations from scala.annotation.target
, eg getter
, setter
or as in your case field
: 要做到这一点,请使用scala.annotation.target
一个或多个注释来注释您的注释,例如getter
, setter
或在您的case field
:
import annotation.target.field
class Person(@(Nullable @field) var name: String)
You can also use type aliases for that: 您还可以使用类型别名:
type NullableField = Nullable @field
class Person(@NullableField var name: String)
Now this specific annotation and others are in the package scala.annotation.meta rather than scala.annotation.target 现在这个特定的注释和其他包都在scala.annotation.meta包而不是scala.annotation.target中
import scala.annotation.meta.{field, param}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.