- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Annotations on static forwarders for companion object vals
Tue, 2011-12-20, 13:59
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.
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.
Tue, 2011-12-20, 15:01
#2
Re: Annotations on static forwarders for companion object vals
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, noton 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}
http://www.scala-lang.org/api/current/index.html#scala.annotation.target.package
If it does not add the annotation to the method in the forwarder class, then I think a ticketwould be appropriate.
Thanks,Lukas
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, noton 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}
http://www.scala-lang.org/api/current/index.html#scala.annotation.target.package
If it does not add the annotation to the method in the forwarder class, then I think a ticketwould be appropriate.
Thanks,Lukas
On Tuesday, 20. December 2011 at 13:59, Matthew Farwell wrote:
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.
Tue, 2011-12-20, 15:41
#3
Re: Annotations on static forwarders for companion object vals
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.
On Tuesday, 20 December 2011 14:51:57 UTC+1, Lukas Rytz wrote:
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.
On Tuesday, 20 December 2011 14:51:57 UTC+1, Lukas Rytz wrote:
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, noton 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}
http://www.scala-lang.org/api/current/index.html#scala.annotation.target.package
If it does not add the annotation to the method in the forwarder class, then I think a ticketwould be appropriate.
Thanks,Lukas
On Tuesday, 20. December 2011 at 13:59, Matthew Farwell wrote:
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.
Tue, 2011-12-20, 16:11
#4
Re: Annotations on static forwarders for companion object vals
Glad it helpt..
The logic of the compiler for the forwarder class is simple: it creates a forwarder for each method in theobject. If that method has an annotation, then so does the static method in the forwarder class. In ourexample, 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 onthe getter. That's what you can change using the "target" annotations. See also here: http://www.scala-lang.org/sites/default/files/sids/rytz/Wed,%202010-01-27,%2015:10/annots.pdf
So in my eyes, the compiler's behavior is consistent and should not be changed.
Lukas
Sent with Sparrow
The logic of the compiler for the forwarder class is simple: it creates a forwarder for each method in theobject. If that method has an annotation, then so does the static method in the forwarder class. In ourexample, 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 onthe getter. That's what you can change using the "target" annotations. See also here: http://www.scala-lang.org/sites/default/files/sids/rytz/Wed,%202010-01-27,%2015:10/annots.pdf
So in my eyes, the compiler's behavior is consistent and should not be changed.
Lukas
Sent with Sparrow
On Tuesday, 20. December 2011 at 15:37, Matthew Farwell wrote:
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.
On Tuesday, 20 December 2011 14:51:57 UTC+1, Lukas Rytz wrote: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, noton 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}
http://www.scala-lang.org/api/current/index.html#scala.annotation.target.package
If it does not add the annotation to the method in the forwarder class, then I think a ticketwould be appropriate.
Thanks,Lukas
On Tuesday, 20. December 2011 at 13:59, Matthew Farwell wrote:
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.
On Tuesday, 20 December 2011 13:59:01 UTC+1, Matthew Farwell wrote: