justwrote
justwrote

Reputation: 35

How do I get the values of an Annotation from the Scala presentation compiler?

Since I want to generate Scala code from an annotated Scala class, I need to get the values from annotations of the class.

public @interface TestAnnotation {
    public String name();
    public String description();
    public String[] tags() default { "Test" };
}
@TestAnnotation(name = "TestName", description = "TestDescription")
class MyClass

My Problem is, that the Scala presentation compiler doesn't give me a value for tags. I'm accessing the values with the following code:

import tools.nsc.interactive.Global._    

val ast = ...
val ans = ast.symbol.annotations // which returns me a List of AnnotationInfo
ans.head.assocs // returns: List((name, "TestName"), (description, "TestDescription"))

So how can I get the default value of tags?

Upvotes: 2

Views: 669

Answers (1)

Lukas Rytz
Lukas Rytz

Reputation: 1894

The same question was recently asked on the scala-language mailing list. Here's the link to the thread.

Short answer: this is currently not possible. Implementing it requires a major amount of work, unfortunately. The reason is that the Scala compiler would need to have a complete Java parser. Currently, it can only parse declarations in Java source code, it skips all the "righthand sides" (method bodies, field definitions). See the thread.

Upvotes: 1

Related Questions