[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">&lt;<a \
href="mailto:alessiostalla@gmail.com" \
target="_blank">alessiostalla@gmail.com</a>&gt;</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, \
&quot;Jim White&quot; &lt;<a href="mailto:james.paul.white@gmail.com" \
target="_blank">james.paul.white@gmail.com</a>&gt; wrote:<br> &gt;<br>
&gt; I&#39;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> &gt;</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&#39;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&#39;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">&gt; Don&#39;t have time to really track this down, but the \
most surprising variation is with this:<br> &gt;<br>
&gt; <a href="https://gist.github.com/jimwhite/8755a8a7e9263f5a2154" \
target="_blank">https://gist.github.com/jimwhite/8755a8a7e9263f5a2154</a><br> \
&gt;<br> &gt; Foo.java:<br>
&gt; public class Foo {<br>
&gt;       protected   int x;<br>
&gt;<br>
&gt;         int getX() { return x + 1; }<br>
&gt; }<br>
&gt;<br>
&gt; SuperFieldAccess.groovy<br>
&gt; class Bar extends Foo {<br>
&gt;       void doJob() {<br>
&gt;             println x<br>
&gt;             println super.x<br>
&gt;             println super.@x<br>
&gt;       }<br>
&gt;<br>
&gt; }<br>
&gt;<br>
&gt; Bar b = new Bar()<br>
&gt; b.@x = 1<br>
&gt; b.doJob()<br>
&gt;<br>
&gt; ===&gt;<br>
&gt;<br>
&gt; 2<br>
&gt; 1<br>
&gt; 1<br>
&gt;<br>
&gt; 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> &gt;<br>
&gt; Jim<br>
&gt;<br>
&gt; On Fri, Sep 12, 2014 at 10:41 PM, Alexander Shutyaev &lt;<a \
href="mailto:shutyaev@gmail.com" target="_blank">shutyaev@gmail.com</a>&gt; \
wrote:<br> &gt;&gt;<br>
&gt;&gt; getX() doesn&#39;t increment x. It returns a value that is x + 1. I&#39;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> &gt;&gt;<br>
&gt;&gt; 2014-09-13 2:40 GMT+04:00 Tim Yates &lt;<a href="mailto:tim.yates@gmail.com" \
target="_blank">tim.yates@gmail.com</a>&gt;:<br> &gt;&gt;&gt;<br>
&gt;&gt;&gt; The first call to  <br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;             println super.x<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; calls getX, so increments x (from 1 to 2)<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; The second call returns the value 2<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; I can&#39;t see the issue from the above email?<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; I&#39;m probably missing something though<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Tim<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; On 12 September 2014 22:15, Alexander Shutyaev &lt;<a \
href="mailto:shutyaev@gmail.com" target="_blank">shutyaev@gmail.com</a>&gt; \
wrote:<br> &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; Hi all!<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; The problem is exactly as it is stated in the subject:  field access \
(.@) not working with super variable. Here&#39;s an example:<br> &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; class Foo {<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;       int x<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;       int getX() {<br>
&gt;&gt;&gt;&gt;             return x + 1<br>
&gt;&gt;&gt;&gt;       }<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; }<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; class Bar extends Foo {<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;       void doJob() {<br>
&gt;&gt;&gt;&gt;             println super.x<br>
&gt;&gt;&gt;&gt;             println super.@x<br>
&gt;&gt;&gt;&gt;       }<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; }<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; Bar b = new Bar()<br>
&gt;&gt;&gt;&gt; b.x = 1<br>
&gt;&gt;&gt;&gt; b.doJob()<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; If you run this script, you&#39;ll get<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; 2<br>
&gt;&gt;&gt;&gt; 2<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; It means that super.@x really does super.getX(). Is this a bug? If \
yes, should I raise it in JIRA? I&#39;m ready to help with a patch, and if someone \
could point me to a class in sources that would be awesome.<br> &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; If this is not a bug, then how can I achieve this behavior? I&#39;m \
porting some java code to groovy and bumped into this.<br> &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; Thanks in advance,<br>
&gt;&gt;&gt;&gt; Alexander<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;<br>
&gt;<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