Groovy support - tooltwist/documentation GitHub Wiki

Groovy is a Java-like language with the syntax relaxed, making it more like Ruby and other scripting languages. It declares types implicitly, and contains many shortcuts.

Java is actually a subset of Groovy, so you can switch to native Java at any time. Groovy compiles down to Java .class files, and you can jump seamlessly from a Groovy class to a Java class and visa versa. Groovy is fully supported in Eclipse, with auto-complete and debugging.

One huge pain in Java is creating JSON and HTML. Look how easy it is in Groovy….

	def json = """\
		{
			"a": [ 123, 456 ],
			"total": "${total}",
			"result": "This is all very nice",
			"person": [
					"Fred Smith",
					"Madge Smith",
					"Harry Jones"
			]
		}
	"""

Notice it looks like JSON, as opposed to lines like this:

StringBuffer buf  = new StringBuffer();
buf.append("{\n");
buf.append("\t\"a\": [ 123, 456 ],\n");
buf.append("\t\"total\": \"" + total + "\",\n");
etc

The nice thing is that .groovy files can be completely intermingled with .java files, and all the normal auto-complete, etc works. In other words, it's easy to jump to Groovy when doing string manipulation, and back again for normal stuff. Valid Java is a subset of Groovy, so it's possible to rename a .java file to .groovy without change. Where Groovy falls short, you just insert Java code on any or every line.

Groovy also works nicely with XData:

	def data = new XD(json)
	def cnt = 1
	for (XSelector s: data.select("person")) {
		String name = s.getString(".")
		println "${cnt++} name=${name}"
	}

Learn Groovy in 20 minutes

A quick tutorial can be found at http://www.ibm.com/developerworks/java/tutorials/j-groovy/j-groovy-pdf.pdf. Read chapter 2, then skip to page 16. Don't follow the installation instructions in the tutorial, as they are out of date.

Installing Groovy

In Eclipse, go to Help->Eclipse Marketplace... and search for Groovy. Select "Groovy-Eclipse for Juno" (or whatever your version is) and press Finish.

When you create a new project, you might get an error:

The container 'Groovy DSL Support' references non existing library '/tooltwist/tooltwist_osx_8.0-beta/eclipse/plugins/org.codehaus.groovy_2.0.6.xx-RELEASE-20121219-0800-e42/plugin_dsld_support/'

To fix this, open the project properties and go to Java Build Path->Libraries, and delete Groovy DSL Support. If you want to use DSL (Domain Specific Language), you'll have to work out the problem (then update this page).

Other information

http://groovy.codehaus.org/For+those+new+to+both+Java+and+Groovy

--