How to add constant and literal to annotation?

I try to add a constant and a literal to the annotation, but in the end I get that "the annotation argument must be constant". I installed some sample code that also causes the same error.

HelloWorld.scala

package test @TestAnnotation(Constants.test + ", world!") object HelloWorld { def main(args: Array[String]) { println("Hello, world!") } } 

Constants.scala

 package test object Constants { final val test = "Hello" } 

TestAnnotation.java

 package test; @interface TestAnnotation { String value(); } 
+1
source share
1 answer

I looked at http://www.scala-lang.org/old/node/9363 and expected to tell you that you need static and / or final (which you already have) on your Constants.test , but I ran your code, and it works in section 2.10.4

 $ sbt run [info] Compiling 2 Scala sources and 1 Java source to .../so22717836annotation/target/scala-2.10/classes... [info] Running HelloWorld Hello, world! 

here is the code:

 $ for i in `echo build.sbt; find src/main -type f`; do \ echo '###' $i '###'; echo; perl -pe 's/^/ /' $i; echo; done 

build.sbt

 name := "so22717836" version := "0.0.1" scalaVersion := "2.10.4" 

Src / home / java / test / TestAnnotation.java

 package test; public @interface TestAnnotation { String value(); } 

Src / main / scala / HelloWorld.scala

 import test._ @TestAnnotation(Constants.test + ", world!") object HelloWorld { def main(args: Array[String]) { println("Hello, world!") } } 

SRC / Primary / scala / Constants.scala

 object Constants { final val test = "Hello" } 
0
source

Source: https://habr.com/ru/post/970256/


All Articles