How to access Annotation defined on case class field at Runtime
constructor, parameters, reflection, scala
Solution
You just need to do the following :
case class TestAnno(arg1 : String, @(Prefer @field)("abc") agr2 : String)
More info here http://www.scala-lang.org/api/current/#scala.annotation.meta.package
Problem
I've the following java Annotation defined ``` @Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String value() default ""; } ``` And I've the following scala case class defined: ``` @Prefer("xyz") case class TestAnno(arg1 : String, @Prefer("abc") agr2 : String) ``` I'm able see the annotation defined at case class level using reflection but I'm not able to access the annotation defined for `arg2` member of case class `TestAnno`. De-compiling the code I see that neither the variable declaration or it's scala accessor seem to have the annotation. Only the constructor definition and `copy` method seem to retain the annotation for the parameters as defined in case class declaration. Is there some other way to force the scala compiler to generate annotations for the fields declared in case class or I have to read the constructor and use a library such as ASM ByteCode Library or ParaNamer to find which parameters have which annotations? Need a solution which will work primarily for Scala case classes.