<< November 2006 | Home | January 2007 >>

Groovy in GroovyEclipse

Groovy is not a scripting language!

There, I got that out of the way.

Often I see questions like Why use Groovy if it's like Java without types normally followed by responses showing examples of nice terse Groovy for various language constructs. I recently wrote some code for code completion of Default Groovy Methods which illustrates exactly why much of my plugin code is written in Groovy.

The completion idea is simple: Extract the methods from the DefaultGroovyMethods class, and create 2 lists of strings. One contains strings to display to the user in the completion popup, and the other contains strings that are inserted into the document after a completion is selected.

Often people look at Groovy snippets, and think all they are getting is Java with compact notation for lists, maps, strings and other types. The real fun is the compact way you can manipulate these commonly used types.

Back to the example. Place this Java code into your imagination:
Method[] methods = DefaultGroovyMethods.class.getMethods();
Now imagine all the iteration and string concatenation fun you will have.

Done?

Ok, now look at the Groovy code:
private static displayStrings = []
private static replaceStrings = []

static {
def methods = DefaultGroovyMethods.methods
methods = methods.findAll { java.lang.reflect.Modifier.isStatic(it.modifiers) }

// Get the display strings.
displayStrings = methods.collect { method ->
method.name + '(' +
method.parameterTypes.collect { it.simpleName } .join(',') +
") - ${method.returnType.simpleName}"
}

// Create replace strings - remember to convert from GStrings!
replaceStrings = methods.collect { "${it.name}()".toString() }
}
How easy was that?

It is important to note that this code is called from a Java class without any extra effort. A class file is a class file. And what magic do I need to do to export the plugin? Simply hit the magic 'export' button provided by Eclipse!

Groovy is NOT a scripting language

I just finished reading this article and related responses: Groovy or stuck in the groove?
It seems that people still don't get it: Groovy is not a scripting language. It is a first class replacement for Java when its features make it the appropriate language for the job.

Here is a little challenge:
class AProperClass {
void showMessage() { println "Don't call me scripting!" }
}
I compile this to a class file using groovyc or I just let the GroovyEclipse builder do it. And now I place the class in a Jar.

That is it, I simply add it to my Eclipse project library path (and of course, the Groovy runtime jar too), and use it in some Java code:
AProperClass instance = new AProperClass();
instance.showMessage();
So what is the challenge? Someone, anyone, create a jar that I can drop into Eclipse and use as above. Do it using either BeanShell, Scala, Kawa, Rhino, Jython, JRuby. And of course, no tricks allowed (e.g. funny Jython comments). Hmm, what about the Nice language? I haven't checked on that one.

I looked far and wide for an alternative to Java. So far I only found that alternative in Groovy. Please correct me if wrong - more tools, more fun.

And as far as Groovy losing its groove? There are days I think about the subscribers to the lists for some of the above listed languages and think how lucky they are not to be confronted with 50-100 daily messages that are divided over the Groovy and Grails lists.

A Groovy AST Viewer

Viewing an AST along side its Groovy source code is very useful for anyone making tools that extract information from AST nodes. Creating such a viewer for Eclipse is a lot more fun when mostly implemented in Groovy.

Read more...