斯卡拉2.11反射和说明(JAVA)与参数 [英] Scala 2.11 reflection and annotations (Java) with parameters
本文介绍了斯卡拉2.11反射和说明(JAVA)与参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用Java编写一个简单的类级别注解:
I have a simple class-level annotation written in Java:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Collection {
String name();
}
使用,如:
@Collection(name="mytable")
case class Foo(...)
我要反思的类斯卡拉2.11获取名称参数的值。我怎样才能得到这个信息?我到这里:
I need to introspect classes in Scala 2.11 to obtain the value of the name parameter. How can I get this info? I'm up to here:
val sym = currentMirror.classSymbol(Class.forName(fullName))
val anno = sym.annotations.head
val annoType = anno.tree.tpe // I can get this...works
println(anno.tree.children.tail) // prints List(name = "mytable")
我接近!我可以看到我的名字参数和它的值,但是这似乎并不像一个地图或任何友好访问。我怎样才能让我的标注的参数的值?
I'm close! I can see my name parameter and its value but this doesn't seem to be accessible like a Map or anything friendly. How can I get the value of my annotation's parameter?
推荐答案
树API实现产品,以获得元素了,所以这是怎样的一个哈克论证,但你可以得到你的元素:
The tree api implements product to get elements out, so this is kind of a hacky demonstration, but you can get out your element:
println(anno.tree.children.last.productElement(1)) // prints "mytable"
这篇关于斯卡拉2.11反射和说明(JAVA)与参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文