Scala 2.8:使用带有数组参数的 Java 注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2842174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala 2.8: use Java annotation with an array parameter
提问by Ingo Fischer
I'm trying to implement an JavaEE Session Bean with Scala 2.8.
Because it's a Remote Session Bean, i have to annotate it with the following Java Annotation:
我正在尝试使用 Scala 2.8 实现 JavaEE 会话 Bean。
因为它是一个远程会话 Bean,所以我必须使用以下 Java 注释对其进行注释:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Remote {
Class[] value() default {};
}
I only found this examplefor scala 2.7. In Scala 2.7, its possible to define the session bean like this:
我只为 Scala 2.7找到了这个例子。在 Scala 2.7 中,可以像这样定义会话 bean:
@Remote {val value = Array(classOf[MyEJBRemote])}
class MyEJB
...
How can i use this annotation the same way with Scala 2.8? I already tried many different versions, all resulting in "annotation argument needs to be a constant", "illegal start of simple expression". All of these definitions don't work:
我如何以与 Scala 2.8 相同的方式使用此注释?我已经尝试了许多不同的版本,都导致“注释参数需要是一个常量”,“简单表达式的非法开始”。所有这些定义都不起作用:
@Remote{val value = Array(classOf[MyEJBRemote])}
@Remote(val value = Array(classOf[MyEJBRemote]))
@Remote(Array(classOf[MyEJBRemote]))
采纳答案by retronym
You've got the syntax right in your answer. The problem is that the @Remote annotation uses the raw type Classrather than Class<?>. Java raw types are an unfortunate consequence of the backwards compatibility constraints from Java 1.4 to Java 1.5, and common source of bugs in the Scala compiler.
您的答案中的语法是正确的。问题在于 @Remote 注释使用原始类型Class而不是Class<?>. Java 原始类型是从 Java 1.4 到 Java 1.5 的向后兼容性约束的不幸结果,也是 Scala 编译器中常见的错误来源。
I found bug #3429describing basically the same problem, and added your particular problem as another test case.
我发现错误#3429描述了基本相同的问题,并将您的特定问题添加为另一个测试用例。
The only workaround would be to take the source code from the problematic annotation, replace Classwith Class<?>, recompile them, and put that JAR in front of the classpath to Scalac. Other than that, you should vote for the bug adding your email to the CC list.
唯一的解决方法是从有问题的注释中获取源代码,替换Class为Class<?>,重新编译它们,然后将该 JAR 放在 Scalac 的类路径前面。除此之外,您应该投票支持将您的电子邮件添加到抄送列表的错误。
回答by Ingo Fischer
As always a competent answer... thank you! This is a real show-stopper to use Scala in a JavaEE application. Changing the annotation is not an option for me. I wonder why it worked with Scala 2.7x. On this pagethe author implements the annotation like this:
一如既往,一个称职的答案......谢谢!这是在 JavaEE 应用程序中使用 Scala 的真正亮点。更改注释对我来说不是一个选择。我想知道为什么它适用于 Scala 2.7x。在这个页面上,作者实现了这样的注释:
@Remote {val value = Array(classOf[ITest])}
class TestBean extends ITest { ...
Which seems to work. Unfortunately, Scala 2.7x is also not an option for me...
这似乎有效。不幸的是,Scala 2.7x 也不适合我......
回答by Ingo Fischer
Okay, i found out that you CAN use an array as an annotation parameter as seen here. So in principle, this shouldwork:
好吧,我发现,你可以使用数组作为注解的参数所看到这里。所以原则上,这应该有效:
@Remote(value = Array(classOf[MyEJBRemote]))
class MyEJB extends MyEJBRemote {
Here is my MyEJBRemote:
这是我的 MyEJBRemote:
trait MyEJBRemote {
}
So the array is okay, but my next problem is a type mismatch coming from classOf[MyEJBRemote]. As it seems it's not possible to have a .class as an annotation parameter. This has also been discussed here, without any solution to this. Will do further investigation on this...
所以数组没问题,但我的下一个问题是来自classOf[MyEJBRemote]的类型不匹配。似乎不可能将 .class 作为注释参数。这也已讨论here,没有任何解决方案。将对此进行进一步调查...

