The Dynamic Languages Day, which is
part of the JAX
08, starts with a session about
Groovy:
Guillaume Laforge:
Groovy,
a successful dynamic language for the JVM
- What's Groovy
- Syntax
- Groovy APIs
- What's new in Groovy 1.5
- Domain-Specific Languages
- Integration of Groovy in custom apps
Groovy is Java-like and intregrates
seamless with Java; you can mix both, same strings, regex, APIs, OO, threads,
security etc.
It's fully oo, Closures, Properties,
Operator overloading, Multimethods, GPath, BigDecimal, etc. Lots of interesting
language features missing in Java. Ideal for DSLs
Already integrated in a lot of projects,
LOTS of projects, like Grails, JBoss Seam, ServiceMix, SoapUI, Turbine,
Oracle, Mule, Spring, IntelliJ IDEA, Ant, Maven, ...
Used in mission-critical apps:
e. g. Mutual of Omaha, Fortune 500 US
insurance company,
45,000 lines of Groovy (half business
code, half test code), risk calculation engine, ideal for business rules
expression
Reasons for Groovy: exact arithmetic
support, perfect Java integration, closures
Groovy Syntax
Groovy is a Java superset
Valid Java code is also valid Groovy
code, but using Groovy's language constructs, you get shorter, "groovier"
programs
- no semicolons necessary,
- public is default,
- {$varName} in String literals will be replaced by variable value,
- Properties: get rid of simple getters and setters,
- def is type-unsafe general type,
- var.name = X instead of var.setName(X),
- println instead of System.out.println,
- no types necessary for arguments,
- code out-side of classes
- Operator overloading
- new properties on numbers like 10.dollars or 30.seconds
Literals for lists, maps, ranges, regexes,
strings
Multiline strings: """
first line of string
last line of string """
Gstrings: println "My name is $(name)"
Closures
especially useful with each, eachLine,
findAll iterators; "it" means current value
Groovy APIs
Simplified APIs for common tasks, like
- mocking, stubbing support,
- XML parsing,
- SQL support with exception & resource handling,
- COM scripting on Windows,
- consuming/providing Web services
Swing Builder
easy, concise way to generate UI, e.
g. frame with panel with labels, text fields and buttons, even with listeners
using closures the code is really short
Automating Office applications
ActiveXProxy is a wrapper for COM objects
because Groovy is a dynamic language
(double dispatch), you can simply call the usual methods on your COM objects
(like in VB)
Domain-Specific Languages
Why create a DSL?
- more expressive language
- common metaphore
- domain experts can help writing the business rules
- seperate business code and logic from technical/application code
New in Groovy 1.5
Java 5 Features, like Annotations, generics,
...
Groovy switch statement works on a lot
more things, like lists, enums, etc.
ExpandoMetaClass from Grails: expand
existing classes
IDE support
- IntelliJ IDEA plugin
- Eclipse plugin (not so good by now)
- Oracle is working on JDeveloper plugin
Integrating Groovy in your apps
- Java 6's scripting API (JSR-223)
- Drop engine and libraries in your class path and create engine
- GroovyShell
- GroovyClassLoader: most powerful integration mechanism
- Spring 2 Groovy integration
http://groovy.codehaus.org