[prev in list] [next in list] [prev in thread] [next in thread]
List: groovy-user
Subject: Re: [groovy-user] field access (.@) not working with super
From: Jim White <james.paul.white () gmail ! com>
Date: 2014-09-13 21:05:34
Message-ID: CAMpkrpSvLf-XFWW2ip3pcgJfwRh2asWFgRrJEyz4p-rdB-Au1g () mail ! gmail ! com
[Download RAW message or body]
On Sat, Sep 13, 2014 at 4:23 AM, Alessio Stalla <alessiostalla@gmail.com>
wrote:
>
> On 13 Sep 2014 10:08, "Jim White" <james.paul.white@gmail.com> wrote:
> >
> > I'm a little surprised that Groovy is treating package-scoped fields in
> a Java class as if they were private but that is what is happening.
> >
>
> Actually this is the expected behaviour in Java too. Package-scoped
> symbols are accessible only from classes in the same runtime package - that
> is, defined in the same package *and loaded by the same classloader*. That
> condition clearly is not satisfied for any two Groovy and Java classes,
> even if in their source code the package name is the same.
>
Yes, but I'm pretty sure I was testing with precompiled Groovy classes and
so they should all have been in the same classloader. But maybe the setup
is different than I thought. Don't have time to dig into it further for
for now though.
Jim
> > Don't have time to really track this down, but the most surprising
> variation is with this:
> >
> > https://gist.github.com/jimwhite/8755a8a7e9263f5a2154
> >
> > Foo.java:
> > public class Foo {
> > protected int x;
> >
> > int getX() { return x + 1; }
> > }
> >
> > SuperFieldAccess.groovy
> > class Bar extends Foo {
> > void doJob() {
> > println x
> > println super.x
> > println super.@x
> > }
> >
> > }
> >
> > Bar b = new Bar()
> > b.@x = 1
> > b.doJob()
> >
> > ===>
> >
> > 2
> > 1
> > 1
> >
> > Note that if Foo is in Java and field x is package (or private) scoped,
> then b.@x = 1 will fail. If Foo is in Groovy then b.x = 1 succeeds
> because you get a public setter automagically.
> >
> > Jim
> >
> > On Fri, Sep 12, 2014 at 10:41 PM, Alexander Shutyaev <shutyaev@gmail.com>
> wrote:
> >>
> >> getX() doesn't increment x. It returns a value that is x + 1. I've put
> this line there to ilustrate that there is no difference between super.x
> and super.@x although there should be. Also you can safely remove this
> line (super.x) and leave just super.@x and it will still return 2 instead
> of 1.
> >>
> >> 2014-09-13 2:40 GMT+04:00 Tim Yates <tim.yates@gmail.com>:
> >>>
> >>> The first call to
> >>>
> >>> println super.x
> >>>
> >>> calls getX, so increments x (from 1 to 2)
> >>>
> >>> The second call returns the value 2
> >>>
> >>> I can't see the issue from the above email?
> >>>
> >>> I'm probably missing something though
> >>>
> >>> Tim
> >>>
> >>> On 12 September 2014 22:15, Alexander Shutyaev <shutyaev@gmail.com>
> wrote:
> >>>>
> >>>> Hi all!
> >>>>
> >>>> The problem is exactly as it is stated in the subject: field access
> (.@) not working with super variable. Here's an example:
> >>>>
> >>>> class Foo {
> >>>>
> >>>> int x
> >>>>
> >>>> int getX() {
> >>>> return x + 1
> >>>> }
> >>>>
> >>>> }
> >>>>
> >>>> class Bar extends Foo {
> >>>>
> >>>> void doJob() {
> >>>> println super.x
> >>>> println super.@x
> >>>> }
> >>>>
> >>>> }
> >>>>
> >>>> Bar b = new Bar()
> >>>> b.x = 1
> >>>> b.doJob()
> >>>>
> >>>> If you run this script, you'll get
> >>>>
> >>>> 2
> >>>> 2
> >>>>
> >>>> It means that super.@x really does super.getX(). Is this a bug? If
> yes, should I raise it in JIRA? I'm ready to help with a patch, and if
> someone could point me to a class in sources that would be awesome.
> >>>>
> >>>> If this is not a bug, then how can I achieve this behavior? I'm
> porting some java code to groovy and bumped into this.
> >>>>
> >>>> Thanks in advance,
> >>>> Alexander
> >>>
> >>>
> >>
> >
>
[Attachment #3 (text/html)]
<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sat, Sep 13, 2014 \
at 4:23 AM, Alessio Stalla <span dir="ltr"><<a \
href="mailto:alessiostalla@gmail.com" \
target="_blank">alessiostalla@gmail.com</a>></span> wrote:<br><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"><span class=""><p dir="ltr"><br> On 13 Sep 2014 10:08, \
"Jim White" <<a href="mailto:james.paul.white@gmail.com" \
target="_blank">james.paul.white@gmail.com</a>> wrote:<br> ><br>
> I'm a little surprised that Groovy is treating package-scoped fields in a \
Java class as if they were private but that is what is happening. <br> ></p>
</span><p dir="ltr">Actually this is the expected behaviour in Java too. \
Package-scoped symbols are accessible only from classes in the same runtime package - \
that is, defined in the same package *and loaded by the same classloader*. That \
condition clearly is not satisfied for any two Groovy and Java classes, even if in \
their source code the package name is the same.</p></blockquote><div>Yes, but I'm \
pretty sure I was testing with precompiled Groovy classes and so they should all have \
been in the same classloader. But maybe the setup is different than I thought. \
Don't have time to dig into it further for for now though. \
</div><div><br></div><div>Jim</div><blockquote class="gmail_quote" style="margin:0 0 \
0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div \
class="h5"> <p dir="ltr">> Don't have time to really track this down, but the \
most surprising variation is with this:<br> ><br>
> <a href="https://gist.github.com/jimwhite/8755a8a7e9263f5a2154" \
target="_blank">https://gist.github.com/jimwhite/8755a8a7e9263f5a2154</a><br> \
><br> > Foo.java:<br>
> public class Foo {<br>
> protected int x;<br>
><br>
> int getX() { return x + 1; }<br>
> }<br>
><br>
> SuperFieldAccess.groovy<br>
> class Bar extends Foo {<br>
> void doJob() {<br>
> println x<br>
> println super.x<br>
> println super.@x<br>
> }<br>
><br>
> }<br>
><br>
> Bar b = new Bar()<br>
> b.@x = 1<br>
> b.doJob()<br>
><br>
> ===><br>
><br>
> 2<br>
> 1<br>
> 1<br>
><br>
> Note that if Foo is in Java and field x is package (or private) scoped, then \
b.@x = 1 will fail. If Foo is in Groovy then b.x = 1 succeeds because you get a \
public setter automagically.<br> ><br>
> Jim<br>
><br>
> On Fri, Sep 12, 2014 at 10:41 PM, Alexander Shutyaev <<a \
href="mailto:shutyaev@gmail.com" target="_blank">shutyaev@gmail.com</a>> \
wrote:<br> >><br>
>> getX() doesn't increment x. It returns a value that is x + 1. I've \
put this line there to ilustrate that there is no difference between super.x and \
super.@x although there should be. Also you can safely remove this line (super.x) and \
leave just super.@x and it will still return 2 instead of 1.<br> >><br>
>> 2014-09-13 2:40 GMT+04:00 Tim Yates <<a href="mailto:tim.yates@gmail.com" \
target="_blank">tim.yates@gmail.com</a>>:<br> >>><br>
>>> The first call to <br>
>>><br>
>>> println super.x<br>
>>><br>
>>> calls getX, so increments x (from 1 to 2)<br>
>>><br>
>>> The second call returns the value 2<br>
>>><br>
>>> I can't see the issue from the above email?<br>
>>><br>
>>> I'm probably missing something though<br>
>>><br>
>>> Tim<br>
>>><br>
>>> On 12 September 2014 22:15, Alexander Shutyaev <<a \
href="mailto:shutyaev@gmail.com" target="_blank">shutyaev@gmail.com</a>> \
wrote:<br> >>>><br>
>>>> Hi all!<br>
>>>><br>
>>>> The problem is exactly as it is stated in the subject: field access \
(.@) not working with super variable. Here's an example:<br> >>>><br>
>>>> class Foo {<br>
>>>><br>
>>>> int x<br>
>>>><br>
>>>> int getX() {<br>
>>>> return x + 1<br>
>>>> }<br>
>>>><br>
>>>> }<br>
>>>><br>
>>>> class Bar extends Foo {<br>
>>>><br>
>>>> void doJob() {<br>
>>>> println super.x<br>
>>>> println super.@x<br>
>>>> }<br>
>>>><br>
>>>> }<br>
>>>><br>
>>>> Bar b = new Bar()<br>
>>>> b.x = 1<br>
>>>> b.doJob()<br>
>>>><br>
>>>> If you run this script, you'll get<br>
>>>><br>
>>>> 2<br>
>>>> 2<br>
>>>><br>
>>>> It means that super.@x really does super.getX(). Is this a bug? If \
yes, should I raise it in JIRA? I'm ready to help with a patch, and if someone \
could point me to a class in sources that would be awesome.<br> >>>><br>
>>>> If this is not a bug, then how can I achieve this behavior? I'm \
porting some java code to groovy and bumped into this.<br> >>>><br>
>>>> Thanks in advance,<br>
>>>> Alexander<br>
>>><br>
>>><br>
>><br>
><br>
</p>
</div></div></blockquote></div><br></div></div>
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic