- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Problem with Java method parameter annotations in Scala object
Tue, 2009-03-17, 10:55
Hello,
I noticed that method parameter annotation in a Scala object are set in
the singleton object instance, and not in the static method in the
"facade", and so I don't know how to have annotated parameter in static
method.
For example, that code:
8<-----------------------------------------------
object ObjectTestAnnotation {
//@InjectService is a simple java annotation with Runtime retention,
see at the end for details
def methodWithParamAnnot(@InjectService("service id") s:String) = s
}
8<-----------------------------------------------
Compile into two classes, ObjectTestAnnotation and ObjectTestAnnotation$
which look like that:
8<-----------------------------------------------
% javap test.ObjectTestAnnotation
....
public static final java.lang.String
methodWithParamAnnot(java.lang.String);
...
% javap test.ObjectTestAnnotation\$
....
public java.lang.String methodWithParamAnnot(java.lang.String);
....
8<-----------------------------------------------
In that example, the @InjectService annotation is actually set in
ObjectTestAnnotation$#methodWithParamAnnot and not in
ObjectTestAnnotation#methodWithParamAnnot : this can be view with
Javassist (for example) [note: if anybody knows a tool that show
annotation information given the compiled class, I would really
appreciate to know it, javap doesn't seem to show them].
That means that this differ from the Java equivalent code, where it's
the static method that has the annotated parameter.
This is not a problem if one query the reflexion API (call seems to be
well forwarded to the object), but it's problematic when one use a
bytecode manipulation libraries, like Javassist - and especially when
it's done in a third party framework for which one can't tell to look
into Foo or Foo$ depending of the method.
In this case, the framework directly look for the
"ObjectTestAnnotation.class" file, find the static method but without
the annotation.
I understand that it's something like Java/Scala compatibility corner
case, and so I wanted to know if it was a known/wanted behaviour ? And
if it is, is there a way to have Scala compiled into static method with
annotated parameters ?
Thanks in advance for the help,
Francois Armand
PS: To contextualize the problem, this behaviour was encoutered when I
tried to use Scala with the Tapestry 5 web framework, and more precisely
with the injection framework. This IOC container use Java class for
service binding definition, and these "module" class are heavily rewind
by Javassist, and must follow a lots of convention to be legal. One of
this convention is the use of static method with annotated parameter.
PS-2: InjectService annotation definition :
8<-----------------------------------------------
@Target({PARAMETER, FIELD})
@Retention(RUNTIME)
@Documented
public @interface InjectService {
String value();
}
8<-----------------------------------------------
Wed, 2009-03-18, 10:47
#2
Re: Problem with Java method parameter annotations in Scala obj
martin odersky wrote:
> I think that's something we should change. Can you file a ticket?
>
Thanks for the answer. The ticket is here :
https://lampsvn.epfl.ch/trac/scala/ticket/1802
I think that's something we should change. Can you file a ticket?
Thanks