Quantcast

Java <-> Groovy converters

classic Classic list List threaded Threaded
17 messages Options
12
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Java <-> Groovy converters

Edward Povazan
Hello,

Having used some of the Groovy AST classes of late, and also looking  
at the ASTNode hierarchy representing a Java class in Eclipse, I  
started to think about converters from Java to Groovy (or perhaps the  
other way around).
It seems like a fun task. However, is there any practical use for  
something like this? Does anyone have any scenarios where this would  
be useful? Just curious.

-Ed

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

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

Re: Java <-> Groovy converters

paulk_asert

One that springs to mind is converting Java Unit tests to Groovy Unit tests.

You could start out with an autoconversion and then add extra Grooviness by
hand over time.

Paul.

Edward Povazan wrote:

> Hello,
>
> Having used some of the Groovy AST classes of late, and also looking at
> the ASTNode hierarchy representing a Java class in Eclipse, I started to
> think about converters from Java to Groovy (or perhaps the other way
> around).
> It seems like a fun task. However, is there any practical use for
> something like this? Does anyone have any scenarios where this would be
> useful? Just curious.
>
> -Ed
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

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

Re: Java <-> Groovy converters

Jochen Theodorou
In reply to this post by Edward Povazan
Edward Povazan schrieb:
> Hello,
>
> Having used some of the Groovy AST classes of late, and also looking at
> the ASTNode hierarchy representing a Java class in Eclipse, I started to
> think about converters from Java to Groovy (or perhaps the other way
> around).

The problem with Groovy->Java is, that not all constructs are doable in
Java. Just think of checked Exceptions for example. Java->Groovy should
be easy on a syntactic level, but the lack of inner classes complicates
this a bit. And after that is done, there is always the semantic level.
Java does not have multimethods. I am not against such a tool, but it
must make clear, that it can not be used to convert one working program
into another.

> It seems like a fun task. However, is there any practical use for
> something like this? Does anyone have any scenarios where this would be
> useful? Just curious.

well, maybe for people starting with Groovy.

bye blackdrag

--
Jochen "blackdrag" Theodorou
Groovy Tech Lead (http://groovy.codehaus.org)
http://blackdragsview.blogspot.com/

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

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

Re: Java <-> Groovy converters

Martin C. Martin-2
In reply to this post by Edward Povazan
Java to Groovy: Someone knows how to express something in Java, but not
in Groovy.  For example, turning anonymous inner classes into closures,
or named inner classes into separate classes in the same file.

Groovy to Java: Efficiency.  You have code that works in Groovy, but
it's slow.  It would be great to recode a few of the functions in Java.

- Martin

Edward Povazan wrote:

> Hello,
>
> Having used some of the Groovy AST classes of late, and also looking at
> the ASTNode hierarchy representing a Java class in Eclipse, I started to
> think about converters from Java to Groovy (or perhaps the other way
> around).
> It seems like a fun task. However, is there any practical use for
> something like this? Does anyone have any scenarios where this would be
> useful? Just curious.
>
> -Ed
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>    http://xircles.codehaus.org/manage_email
>

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

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

Re: Java <-> Groovy converters

tugwilson

On 22 Dec 2006, at 12:24, Martin C. Martin wrote:

> Java to Groovy: Someone knows how to express something in Java, but  
> not in Groovy.  For example, turning anonymous inner classes into  
> closures, or named inner classes into separate classes in the same  
> file.
>
> Groovy to Java: Efficiency.  You have code that works in Groovy,  
> but it's slow.  It would be great to recode a few of the functions  
> in Java.

You can recode functions in Java pretty easily. Just subclass your  
Groovy class with a Java class or have your Groovy class subclass a  
Java class. You end up with an object with a mixture of Java and  
Groovy methods.

If you translate Groovy into Java whilst preserving the semantics you  
will not a large performance improvement. This is because you will  
need to pass everything through the MetaClass.

John Wilson
The Wilson Partnership
web http://www.wilson.co.uk
blog http://eek.ook.org



---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

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

Re: Java <-> Groovy converters

Martin C. Martin-2


John Wilson wrote:

>
> On 22 Dec 2006, at 12:24, Martin C. Martin wrote:
>
>> Groovy to Java: Efficiency.  You have code that works in Groovy, but
>> it's slow.  It would be great to recode a few of the functions in Java.
>
> You can recode functions in Java pretty easily. Just subclass your
> Groovy class with a Java class or have your Groovy class subclass a Java
> class. You end up with an object with a mixture of Java and Groovy methods.
>
> If you translate Groovy into Java whilst preserving the semantics you
> will not a large performance improvement. This is because you will need
> to pass everything through the MetaClass.

True, I was thinking that the Groovy -> Java compiler would assume the
default MetaClass and do the "obvious" Java thing, which will be
incorrect in certain cases.  So as Jochen said, the resulting Java code
may not behave exactly like the Groovy code.  But it would be a start.

If you want Java code whose behaviour is the same as the Groovy code,
just call a Java decompiler on the .class files.  :)  This was suggested
in GinA, and I've been playing around with it.  It's not perfect, but
it's a lot of fun.  :)

Best,
Martin


---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

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

Re: Java <-> Groovy converters

tugwilson

On 22 Dec 2006, at 13:19, Martin C. Martin wrote:

> True, I was thinking that the Groovy -> Java compiler would assume  
> the default MetaClass and do the "obvious" Java thing, which will  
> be incorrect in certain cases.  So as Jochen said, the resulting  
> Java code may not behave exactly like the Groovy code.  But it  
> would be a start.


Yes but the issue is not really the behaviour of the MetaClass it's  
the type of the variables. My view is that using typed variables is  
normally bad Groovy style (There are exceptions to this, of course).  
If you have code continuing untyped variables and you can't infer the  
type then you have to use the MetaClass directly.

Writing Groovy as though it was Java is a phase that all of us have  
gone through when learning Groovy. I'd certainly not like to  
encourage people to persist in this habit for the sake of  
"efficiency" :)


John Wilson
The Wilson Partnership
web http://www.wilson.co.uk
blog http://eek.ook.org



---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

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

Re: Java <-> Groovy converters

Gavin Grover
In reply to this post by Edward Povazan
A Java -> Groovy converter would be handy.

Martin C. Martin wrote:
> For example, turning anonymous inner classes into closures,
> or named inner classes into separate classes in the same file.

Other common tasks / workarounds I do a lot of:
3. removing getters and setters
4. replacing method calls with operators, especially [ ] and <<
5. removing extra constructors / methods by using default parameters
6. changing the for-loop, and do-loop
7. add semi-colons for fewer but longer lines
8. change 10-line /** comment into 1-line // comment
9. remove 20-line copyright message at top of 10-lines-of-code file
10. combine all the shortish classes in a package into one file
11. add def in front of methods / fields inside classes
12. remove try-catch blocks, or throws clauses from methods
13. remove typenames, except int & char
14. replace arrays with lists, hashtables with maps
15. remove import java.util.*, etc
16. finally, refactor in more meaningful class, method, etc names

Cheers, Gavin Grover




__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

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

Re: Java <-> Groovy converters

tugwilson

On 22 Dec 2006, at 15:55, Gavin Grover wrote:

> Martin C. Martin wrote:
>> For example, turning anonymous inner classes into closures,
>> or named inner classes into separate classes in the same file.
>
> Other common tasks / workarounds I do a lot of:
> 3. removing getters and setters
> 4. replacing method calls with operators, especially [ ] and <<
> 5. removing extra constructors / methods by using default parameters
> 6. changing the for-loop, and do-loop
> 7. add semi-colons for fewer but longer lines
> 8. change 10-line /** comment into 1-line // comment
> 9. remove 20-line copyright message at top of 10-lines-of-code file
> 10. combine all the shortish classes in a package into one file
> 11. add def in front of methods / fields inside classes
> 12. remove try-catch blocks, or throws clauses from methods
> 13. remove typenames, except int & char
> 14. replace arrays with lists, hashtables with maps
> 15. remove import java.util.*, etc
> 16. finally, refactor in more meaningful class, method, etc names


That's a great list!


John Wilson
The Wilson Partnership
web http://www.wilson.co.uk
blog http://eek.ook.org



---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

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

Re: Java <-> Groovy converters

Guillaume Laforge
Administrator
That's a "Groovyfy" refactoring that could be added to the Eclipse plugin :-)

On 12/22/06, John Wilson <[hidden email]> wrote:

>
> On 22 Dec 2006, at 15:55, Gavin Grover wrote:
>
> > Martin C. Martin wrote:
> >> For example, turning anonymous inner classes into closures,
> >> or named inner classes into separate classes in the same file.
> >
> > Other common tasks / workarounds I do a lot of:
> > 3. removing getters and setters
> > 4. replacing method calls with operators, especially [ ] and <<
> > 5. removing extra constructors / methods by using default parameters
> > 6. changing the for-loop, and do-loop
> > 7. add semi-colons for fewer but longer lines
> > 8. change 10-line /** comment into 1-line // comment
> > 9. remove 20-line copyright message at top of 10-lines-of-code file
> > 10. combine all the shortish classes in a package into one file
> > 11. add def in front of methods / fields inside classes
> > 12. remove try-catch blocks, or throws clauses from methods
> > 13. remove typenames, except int & char
> > 14. replace arrays with lists, hashtables with maps
> > 15. remove import java.util.*, etc
> > 16. finally, refactor in more meaningful class, method, etc names
>
>
> That's a great list!
>
>
> John Wilson
> The Wilson Partnership
> web http://www.wilson.co.uk
> blog http://eek.ook.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--
Guillaume Laforge
Groovy Project Manager
http://glaforge.free.fr/blog/groovy

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

12
Loading...