Annotations on static forwarders for companion object vals

49 views
Skip to first unread message

Matthew Farwell

unread,
Dec 20, 2011, 7:59:01 AM12/20/11
to scala...@googlegroups.com
While looking at this question on stack overflow I found that annotations that are set on vals in companion objects aren't carried over to the static forwarder in the class. Howver, they are carried over for methods. Is there a reason for this, or is it a bug/feature?

Here is an example:

object Foobar {
  @DataPoints
  val value = 5;
  
  @DataPoints
  def method() = 5
}

class Foobar {
}

with the above code, the method() static forwarder that appears in the .class has the @DataPoints annotation, but the value() static forward does not.

Is it that the annotation may not be applicable to a method, but may only be applicable to a field, and therefore should not be copied? (ElementType.FIELD/METHOD). If this is the case then the correct behviour would be to issue a warning).

If anybody knows, and could say whether or not I should submit a bug report/enhancement request, I'd be grateful.

Thanks.

Matthew Farwell.

Matthew Farwell

unread,
Dec 20, 2011, 8:05:15 AM12/20/11
to scala...@googlegroups.com
To be clear, the @DataPoints annotation doesn't have a restriction on the type of element, so can apply to method or field.

Matthew Farwell.

Lukas Rytz

unread,
Dec 20, 2011, 8:51:57 AM12/20/11
to scala...@googlegroups.com
Hi Matthew,

The Scala compiler does not look at the intended annotation targets of Java annotations
(ElementType.X), so this should not be the reason.

However, by default, a field annotation in Scala only ends up on the field in bytecode, not
on the getter. It might be that this applies also to the forwarder class.

Therefore, please try the following:

object Foobar {
  @(DataPoints @scala.annotation.target.getter)
  val value = 5
}



If it does not add the annotation to the method in the forwarder class, then I think a ticket
would be appropriate.

Thanks,
Lukas

Matthew Farwell

unread,
Dec 20, 2011, 9:37:16 AM12/20/11
to scala...@googlegroups.com
Cool. Thanks for the quick reply.

If I add the @(DataPoints @scala.annotation.target.getter), then the annotation does indeed appear on the static forwarder.

Followup question: Because these forwarders are added by the compiler automatically, should these annotations be added automatically, in this case?

I'm asking this question because 1) it works for methods, and 2) this is an internal implementation detail of the compiler, so to workaround this problem takes some in depth knowledge of what the compiler is doing (the compiler creates static forwarders, the compiler creates an accessor method for a value, etc). I have to admit that I saw the target annotations scaladoc, but didn't realise exactly how to use them from that. Maybe I'll submit a patch for the scaladoc.

Thanks.

Matthew Farwell.

Lukas Rytz

unread,
Dec 20, 2011, 9:59:25 AM12/20/11
to scala...@googlegroups.com
Glad it helpt..

The logic of the compiler for the forwarder class is simple: it creates a forwarder for each method in the
object. If that method has an annotation, then so does the static method in the forwarder class. In our
example, the method is the getter of the field.

Now for annotations, if you annotate a field, by default, the getter does only end up on the field, not on
the getter. That's what you can change using the "target" annotations. See also here:

So in my eyes, the compiler's behavior is consistent and should not be changed.

Lukas


Sent with Sparrow

Reply all
Reply to author
Forward
0 new messages