Scala 2.8:使用带有数组参数的 Java 注释

发布于 2024-09-01 20:55:00 字数 784 浏览 3 评论 0原文

我正在尝试使用 Scala 2.8 实现 JavaEE 会话 Bean。
因为它是一个远程会话 Bean,所以我必须使用以下 Java 注释对其进行注释:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Remote {
  Class[] value() default {};
} 

我只找到 此示例适用于 scala 2.7。 在 Scala 2.7 中,可以像这样定义会话 bean:

@Remote {val value = Array(classOf[MyEJBRemote])}
class MyEJB
...

How can i use this comment the same way with Scala 2.8?我已经尝试了许多不同的版本,所有结果都是“注释参数需要是一个常量”,“简单表达式的非法开始”。 所有这些定义都不起作用:

@Remote{val value = Array(classOf[MyEJBRemote])}
@Remote(val value = Array(classOf[MyEJBRemote]))
@Remote(Array(classOf[MyEJBRemote]))

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:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Remote {
  Class[] value() default {};
} 

I only found this example for scala 2.7.
In Scala 2.7, its possible to define the session bean like this:

@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:

@Remote{val value = Array(classOf[MyEJBRemote])}
@Remote(val value = Array(classOf[MyEJBRemote]))
@Remote(Array(classOf[MyEJBRemote]))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷情 2024-09-08 20:55:00

你的答案中的语法是正确的。问题在于 @Remote 注释使用原始类型 Class 而不是 Class。 Java 原始类型是从 Java 1.4 到 Java 1.5 的向后兼容性限制的一个不幸的结果,也是 Scala 编译器中错误的常见来源。

我发现 bug #3429 描述了基本相同的问题,并将您的特定问题添加为另一个测试用例。

唯一的解决方法是从有问题的注释中获取源代码,将 Class 替换为 Class,重新编译它们,然后将该 JAR 放在类路径前面到斯卡拉克。除此之外,您应该投票支持将您的电子邮件添加到抄送列表的错误。

You've got the syntax right in your answer. The problem is that the @Remote annotation uses the raw type Class rather 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.

I found bug #3429 describing basically the same problem, and added your particular problem as another test case.

The only workaround would be to take the source code from the problematic annotation, replace Class with 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.

谁对谁错谁最难过 2024-09-08 20:55:00

好的,我发现您可以使用数组作为注释参数,如 此处
所以原则上,这应该工作:

@Remote(value = Array(classOf[MyEJBRemote]))
class MyEJB extends MyEJBRemote {

这是我的MyEJBRemote:

trait MyEJBRemote {
}

所以数组没问题,但我的下一个问题是来自classOf[MyEJBRemote]的类型不匹配。看来不可能将 .class 作为注释参数。
这也已经讨论过 here,对此没有任何解决方案。
将对此做进一步调查...

Okay, i found out that you CAN use an array as an annotation parameter as seen here.
So in principle, this should work:

@Remote(value = Array(classOf[MyEJBRemote]))
class MyEJB extends MyEJBRemote {

Here is my 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...

兲鉂ぱ嘚淚 2024-09-08 20:55:00

一如既往的干练回答...谢谢!
这是在 JavaEE 应用程序中使用 Scala 的真正亮点。更改注释对我来说不是一个选择。
我想知道为什么它可以与 Scala 2.7x 一起使用。在此页面上,作者实现了这样的注释

@Remote {val value = Array(classOf[ITest])}
class TestBean extends ITest { ...

:似乎有效。不幸的是,Scala 2.7x 也不适合我......

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 page the author implements the annotation like this:

@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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文