Hello all,
According to scala.annotation.meta
documentation:
By default, annotations on (val-, var- or plain) constructor parameters end up on the parameter, not on any other entity. Annotations on fields by default only end up on the field.
However if I defined a class like:
class Foo(@Deprecated value: String) {
override def toString = s"Foo(value=$value)"
}
Then classOf[Foo].getDeclaredField("value").getDeclaredAnnotations
returns Array(@java.lang.Deprecated())
while I would expect value
field to have no annotation at all.
Is it expected or am I missing anything ?
Iām using scala 2.12.6.
Full example using Scala REPL:
Welcome to Scala 2.12.6 (OpenJDK 64-Bit Server VM, Java 1.8.0_181).
Type in expressions for evaluation. Or try :help.
scala> class Foo(@Deprecated value: String) { override def toString = s"Foo(value=$value)" }
defined class Foo
scala> classOf[Foo].getDeclaredField("value").getDeclaredAnnotations
res0: Array[java.lang.annotation.Annotation] = Array(@java.lang.Deprecated())
scala> import scala.annotation.meta._
import scala.annotation.meta._
scala> class Foo2(@(Deprecated @param) value: String) { override def toString = s"Foo2(value=$value)" }
defined class Foo2
scala> classOf[Foo2].getDeclaredField("value").getDeclaredAnnotations
res1: Array[java.lang.annotation.Annotation] = Array(@java.lang.Deprecated())
Thanks in advance.
Regards