CONTENTS INCLUDE:
n
Groovy/Java Integration
n
Language Elements
n
Operators
n
Collective Datatypes
n
Meta Programming
n
Hot Tips and more...
DZone, Inc.
|
www.dzone.com
Groovy is a dynamic language for the Java™ Virtual Machine
(JVM). It shines with full object-orientation, scriptability, optional
typing, operator customization, lexical declarations for the
most common data types, advanced concepts like closures and
ranges, compact property syntax and seamless Java™ integra-
tion. This reference card provides exactly the kind of information
you are likely to look up when programming Groovy.
Install Groovy from http://groovy.codehaus.org and you will
have the following commands available:
The groovy command comes with -h and --help options to
show all options and required arguments. Typical usages are:
Execute file MyScript.groovy
groovy MyScript
Evaluate (e) on the command line
groovy -e "print 12.5*Math.PI"
Print (p) for each line of input
echo 12.5 | groovy -pe
"line.toDouble() * Math.PI"
Inline edit (i) file data.txt by reversing each line and save a backup
groovy -i.bak –pe
"line.reverse()" data.txt
From Groovy, you can call any Java code like you would do from
Java. It’s identical.
From Java, you can call Groovy code in the following ways.
Note that you need to have the groovy-all.jar in your classpath.
Cross-compilation
Use groovyc, the <groovyc/> ant task or your IDE integration to
compile your groovy code together with your Java code. This
enables you to use your Groovy code as if it was written in Java.
Eval
Use class groovy.util.Eval for evaluating simple code that is
captured in a Java String: (int) Eval.xyz(1,2,3,"x+y+z");
Classes & Scripts
A Groovy class declaration looks like in Java. Default visibility
modifier is public
class MyClass {
void myMethod(String argument) {
}
}
GroovyShell
Use groovy.util.GroovyShell for more flexibility in the Binding
and optional pre-parsing:
GroovyShell shell= new GroovyShell();
Script scpt = shell.parse("y = x*x");
Binding binding = new Binding();
scpt.setBinding(binding);
binding.setVariable("x", 2);
scpt.run();
(int) binding.getVariable("y");
Chapter 11 of Groovy in Action has more details about
integration options. Here is an overview:
ABOUT GROOVY
STARTING GROOVY
GROOVY / JAVA INTEGRATION
LANGUAGE ELEMENTS
Groovy www.dzone.com Get More Refcardz! Visit refcardz.com
n
Authoritative content
n
Designed for developers
n
Written by top experts
n
Latest tools & technologies
n
Hot tips & examples
n
Bonus content online
n
New issue every 1-2 weeks
Subscribe Now for FREE!
Refcardz.com
Get More Refcardz
(
They’re free!
)
Command Purpose
groovy Execute Groovy code
groovyc Compile Groovy code
groovysh Open Groovy shell
groovyConsole Open Groovy UI console
java2groovy Migration helper
Integration option Features/properties
Eval/GroovyShell for small expressions + reloading, security
GroovyScriptEngine for dependent scripts + reloading
- classes, security
GroovyClassLoader the catch-all solution + reloading, security
Spring Beans integrates with Spring + reloading
JSR-223 easy language switch but limited in API
- reloading, security
requires Java 6
Groovy
By Dierk König
→
#15