Scala 2.8: use Java annotation with an array parameter
Asked Answered
I

3

10

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]))
Imamate answered 16/5, 2010 at 0:17 Comment(0)
T
6

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.

Tamera answered 16/5, 2010 at 15:47 Comment(0)
I
1

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

Imamate answered 16/5, 2010 at 12:0 Comment(0)
I
1

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

Imamate answered 17/5, 2010 at 11:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.