【发布时间】:2012-12-06 08:36:25
【问题描述】:
我有两个项目,一个 scala 项目和一个 java 项目。我的 scala 项目在构建路径中引用了 java 项目。在我的 java 项目中,我声明了以下注释:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
public String Name();
}
在我的 scala 项目中,我注释了一些方法。即
class MyClass {
...
@MyAnnotation(Name="Blah")
def myMethod() {
...
}
}
在某个地方的另一个文件中,我正在尝试提取注释。
var methods = myClassInstance.getClass().getDeclaredMethods()
var myMethod : Method = null
for (method <- methods) {
if (method.getName().equals("myMethod")) {
myMethod = method
}
}
var annotations = myMethod.getDeclaredAnnotations()
不幸的是,annotations 始终是一个空数组。我是在做一些根本错误的事情还是我只是错过了一些小事?谢谢!
编辑
最初,我用 myAnnotation 对 myMethod 进行了两次注释,正如有人指出的那样,这是不正确的。事实证明这不是问题。我仍然得到annotations 的空数组。没有抛出异常。
【问题讨论】:
标签: java scala reflection scala-java-interop