Quantcast

[groovy-user] instantiate an Java inner class from outside the Java outer class in groovy

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[groovy-user] instantiate an Java inner class from outside the Java outer class in groovy

samuel cheung
Hi,

How can I instantiate an Java inner class from outside the Java outer
class in groovy?

In Java, I do this:
Outer out = new Outer();
Outer.Inner in = out.new Inner();

But if I do the same Groovy, it does not compile.
I need to reference that Java Class from my groovy script.

Thank you for any tips.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [groovy-user] instantiate an Java inner class from outside the Java outer class in groovy

Guillaume Laforge
Administrator
On 31/10/05, samuel cheung <[hidden email]> wrote:
> How can I instantiate an Java inner class from outside the Java outer
> class in groovy?
>
> In Java, I do this:
> Outer out = new Outer();
> Outer.Inner in = out.new Inner();
>
> But if I do the same Groovy, it does not compile.
> I need to reference that Java Class from my groovy script.

Hmm, and what about new Outer.Inner() ? or new out.Inner() ?
Does it work better?
If not... we may have a problem and you should file a bug in JIRA.
Sorry for the inconvenience if that's the case :-(

--
Guillaume Laforge
http://glaforge.free.fr/blog/groovy
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [groovy-user] instantiate an Java inner class from outside the Java outer class in groovy

Jochen Theodorou
In reply to this post by samuel cheung
samuel cheung schrieb:

> Hi,
>
> How can I instantiate an Java inner class from outside the Java outer
> class in groovy?
>
> In Java, I do this:
> Outer out = new Outer();
> Outer.Inner in = out.new Inner();
>
> But if I do the same Groovy, it does not compile.
> I need to reference that Java Class from my groovy script.
>
> Thank you for any tips.


at the moment the creation of inner classes does not work as it should.
Write some gluecode in Java and call that from groovy and you will be
fine. This will be surely fixed sometime, but currently it's a bug. Oh,
and it would be nice if you can fill an entry in our bugtracking jira,
so the bug does not get lost.

bye blackdrag

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [groovy-user] instantiate an Java inner class from outside the Java outer class in groovy

Russel Winder
In reply to this post by Guillaume Laforge
On Mon, 2005-10-31 at 11:30 +0100, Guillaume Laforge wrote:

> On 31/10/05, samuel cheung <[hidden email]> wrote:
> > How can I instantiate an Java inner class from outside the Java outer
> > class in groovy?
> >
> > In Java, I do this:
> > Outer out = new Outer();
> > Outer.Inner in = out.new Inner();
> >
> > But if I do the same Groovy, it does not compile.
> > I need to reference that Java Class from my groovy script.
>
> Hmm, and what about new Outer.Inner() ? or new out.Inner() ?
> Does it work better?
new Outer.Inner ( )  only if the inner class is a nested top-level
class.  If it is a member class then  the code above is what you have to
do.  new out.Inner ( ) never works -- out is parsed as a package name in
this expression.

This is already reported at GROOVY-917.

Also there are GROOVY-69, possibly GROOVY-664?

> If not... we may have a problem and you should file a bug in JIRA.
> Sorry for the inconvenience if that's the case :-(

This issue came up last in 2005.06 via GROOVY-910.  Of course GROOVY-916
is still a problem so basically handling inner classes in Groovy is a
mess.
--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [groovy-user] instantiate an Java inner class from outside the Java outer class in groovy

Russel Winder
In reply to this post by Jochen Theodorou
On Mon, 2005-10-31 at 11:42 +0100, Jochen Theodorou wrote:

> at the moment the creation of inner classes does not work as it should.
> Write some gluecode in Java and call that from groovy and you will be
> fine. This will be surely fixed sometime, but currently it's a bug. Oh,
> and it would be nice if you can fill an entry in our bugtracking jira,
> so the bug does not get lost.

Already in so it got lost anyway ;-)

--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [groovy-user] instantiate an Java inner class from outside the Java outer class in groovy

Jochen Theodorou
Russel Winder schrieb:

[...]
> Already in so it got lost anyway ;-)

no! it's not lost, only the focus is on other things at the moment (a
long moment). If you look at ConstructorCallExpression, which is used
for "new", then you will see it does not have a place for "new
out.Inner()". This has to be changed.

bye blackdrag


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [groovy-user] instantiate an Java inner class from outside the Java outer class in groovy

Russel Winder
On Mon, 2005-10-31 at 14:19 +0100, Jochen Theodorou wrote:

> > Already in so it got lost anyway ;-)
>
> no! it's not lost, only the focus is on other things at the moment (a
> long moment). If you look at ConstructorCallExpression, which is used
> for "new", then you will see it does not have a place for "new
> out.Inner()". This has to be changed.

Indeed, too much to do and too few people actually able to put real time
on the code -- this includes me just now I'm afraid, I can chip in
occasionally but I can't do any really serious stuff for the moment.

new out.Inner ( )

is creating an instance of the Inner class in package out.  Hopefully
you meant

out.new Inner ()

which is the way of instantiating the member class Inner of the class of
object out.  I think the ANTLR code will need to change to handle this
construct.
--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [groovy-user] instantiate an Java inner class from outside the Java outer class in groovy

Martin C. Martin


Russel Winder wrote:

> Hopefully
>
>you meant
>
>out.new Inner ()
>
>which is the way of instantiating the member class Inner of the class of
>object out.  I think the ANTLR code will need to change to handle this
>construct.
>  
>
Didn't we start with the ANTLR for Java?  So wouldn't this already be in
there; it's just a case of writing the back end?  Unless this part of
the grammar had to be completely redone...

- Martin

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [groovy-user] instantiate an Java inner class from outside the Java outer class in groovy

Russel Winder
On Mon, 2005-10-31 at 09:20 -0500, Martin C. Martin wrote:

> Didn't we start with the ANTLR for Java?  So wouldn't this already be in
> there; it's just a case of writing the back end?  Unless this part of
> the grammar had to be completely redone...

As I said in GROOVY-917, if you have the Java file:

        public class X {
          public class Y {
             int z ;
           }
        }

compile it and then execute the Groovy script:

        def x = new X ( )
        def y = x.new Y ( )

then the result is the message:

./test.groovy: 4: unexpected token: new @ line 4, column 11.
   def y = x.new Y ( )
             ^

1 Error

which I believe to be a parser message.  This implies the ANTLR grammar
does not accept this construct, i.e. reference-type '.' 'new'.  This is
a wee bit terminal. I don't think the back-end has a chance ;-)
--
Russel.
====================================================
Dr Russel Winder                +44 20 7585 2200
41 Buckmaster Road              +44 7770 465 077
London SW11 1EN, UK             [hidden email]

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [groovy-user] instantiate an Java inner class from outside the Java outer class in groovy

Jochen Theodorou
In reply to this post by Russel Winder
Russel Winder schrieb:

[...]
> new out.Inner ( )
>
> is creating an instance of the Inner class in package out.  Hopefully
> you meant
>
> out.new Inner ()

ups, mixed public static inner classes in ;)

bye blackdrag
Loading...