groovy
TRANSCRIPT
Tom Corcoran – OSS Software - May 20091
Get excited by coding again!• Java History & the JVM• Groovy genesis & it’s relation to Java• Installing Groovy• Live demo to compare and contrast Groovy with Java code• Groovy
– Typing– Object & Looping– Regular Expressions– Groovy Math– Goodies+++– Closures– MOP– MLVM
• Grails & GAE
Tom Corcoran – OSS Software - May 20092
Java• Java developed by James Gosling released in 1995• Java applications compiled to bytecode (.class files) that can run on any Java
virtual machine (JVM) regardless of computer architecture• Version History
– 1 (1995), 2(1998), 3(2000), 4(2002), 5(2004), 6(2006)– Java 7 due Q1 2010: No closures
• JVM -> MLVM : the Java platform is changing – Groovy– JRuby (Ruby implementation)– Scala– Clojure (Lisp dialect)– Rhino (Javascript implementation)– JavaFX Script (Sun’s new baby)– Jython (Python implementation)
Tom Corcoran – OSS Software - May 20093
Genesis• First begun in 2004 by James Strachan, lost momentum, project
reenergized by Guillaume Laforge with v1 released in Jan 2007, currently at v1.6.3
• JSR 241: Groovy is an agile, dynamic programming language for the Java Virtual Machine. Groovy includes features found in Python, Ruby, and Smalltalk, but uses syntax similar to the Java programming language
• Like Java, dynamically compiled to Java Virtual Machinebytecode (.class file)– As a result seamless 2 way integration
with Java code and libraries• Deployed as a jar (zip) file
Tom Corcoran – OSS Software - May 20094
Java++• Java is a powerful language already,
the goal was to add a new Swiss army knife to the Java developer tool belt, to have the agility and expressivenessof a dynamic language
• Groovy is “quite literally Java, but without the pain”• Follows Java semantics, relaxed grammar, derived from java 5
– Any Java code will run as Groovy: Its superset-of-Java nature means that you can start with a Java file & change its suffix to “.groovy”, often Java developers end up with the gradual adoption route which is as designed
• GDK: extends the JDK (programming tools for Java)– Groovyc in addition to javac– GroovyDoc in addition to JavaDoc
Metaprogramming Can extend a program at runtime, including changing the structure of objects, types, and behaviour
Tom Corcoran – OSS Software - May 20095
Installation– Download zip/windows installer & unpack/install– Set GROOVY_HOME environment variable to unpacked location– Add GROOVY_HOME/bin to your PATH environment variable– Set your JAVA_HOME environment variable to point to your JDK– Groovy Shell
• In a command shell type groovysh– Run Groovy code
• groovy MyCode.groovy– Groovy Console
• To run Swing interactive console type GroovyConsole
Tom Corcoran – OSS Software - May 20096
GroovyBean
Tom Corcoran – OSS Software - May 20097
Increase readability– This GroovyBean gives the same result with less code noise
– This also illustrates a recurring theme in Groovy: Make common coding conventions simple
def• think of def as an alias of
"Object" • must have def / type name• def indicates that you don't care
about the typesemi colon optional
Tom Corcoran – OSS Software - May 20098
Typing• Groovy is optionally typed and the Groovy compiler,
groovyc, does not perform full type checking
– assigning a string to an integer compiles fine, when you try to run you will receive a GroovyCastException exception
– if you call a method that does not exist, you will not get any compilation error, you will get a MissingMethodException at runtime
• Rule of thumb: use static typing if interfacing withstatic language code
Tom Corcoran – OSS Software - May 20099
Objects & Looping• Everything is an object in Groovy
– Java mixes primitive • Boolean type and the numeric types - byte, short, int, long, and char, float and double
and reference (object) types • class types, interface types, and array types• String literals are represented by String objects
• Looping assertProvide expression that you are asserting will be true, otherwise throws AssertionError
new reserved word
mapA map is a mapping from unique unordered keys to values
Tom Corcoran – OSS Software - May 200910
Regular Expressions• Groovy uses the Java regular expression engine but adds some native
support sugar
~"pattern" expression, which creates a Java Pattern object
=~ expressions, which creates a Java Matcher object
Slashy strings, can help with escaping
Tom Corcoran – OSS Software - May 200911
Groovy Math• Groovy uses exact, or decimal math for default calculations
This means that user computations like:1.1 + 0.1 == 1.2will return true rather than false (using float or double types in Java returns a result of 1.2000000000000002)To achieve this groovy literals with decimal points are instantiated as java.math.BigDecimal types rather than binary floating point types (Float, Double)
• Some financial users are big fans of how it does Math and I have heard of one where the actuaries are able to code in it too and do as well
http://groovy.codehaus.org/Groovy+Math
Tom Corcoran – OSS Software - May 200912
Goodies• Groovy Truth
• Groovy provides parsers & slurpers for working with XML– Use XmlSlurper to slurp up a xml file
Tom Corcoran – OSS Software - May 200913
Goodies+• Optional parameters
– to achieve this in Java you have you have to overload method– Trailing array parameter is optional
• Implementing interfaces
Method parameters optional
Tom Corcoran – OSS Software - May 200914
Goodies++• Safe dereferencing
• Elvis operator is a shortening of Java's ternary operator
you will still notice the repetition of the name variable, which would violate the DRY principle (Don't Repeat Yourself)
Tom Corcoran – OSS Software - May 200915
Closures• Closures are pieces of code that can augment, refine or enhance
another piece of code
• Closures can be passed to methods
• GDK Examples
Tom Corcoran – OSS Software - May 200916
MOP• For each Java class and for each Groovy instance, there is an associated meta-
class which represents this runtime behavior of your objects
• a groovier kind of meta-class is available: the expando meta-class
• you can check that a certain method or property is available
Tom Corcoran – OSS Software - May 200917
MLVM• not a silver bullet...
– Doing at run-time what other languages do at compile time raise the concern of speed of dynamic languages
– Dynamic languages such as Groovy/JRuby are up to 10 timesslower on micro benchmarks due to heavy reflection use, this does not mean overall speed translates to that ratio
• the Da Vinci Machine Project– Sun project to prototype the extension of the JVM to add
support for dynamic languages– Specification process JSR-292:
Supporting Dynamically Typed Languages• add a new bytecode, invokedynamic, to bring dynamic
language speed closer to 2/3 times of Java• may be included in Java 7
http://openjdk.java.net/projects/mlvm/
Tom Corcoran – OSS Software - May 200918
Grails & GAE• As of 14 May 2009 Grails v1.1.1 now supports the Google
Application Engine (GAE)– GORM had to have wrapper for GAE’s datastore GQL
• Up to now to Hosting a grails war was about $20 a month • Now free hosting of application with 500 MB of persistent
storage and enough CPU and bandwidth for about 5 million page views a month
• http://myapplication.appspot.com
Tom Corcoran – OSS Software - May 200919
ResourcesGroovy http://groovy.codehaus.org
Programming Groovy : Venkat SubramaniamGroovy Recipes: Scott Davis
Grails http://www.grails.orgThe Definitive Guide to Grails: Graeme RocherGrails in Action: Glen Smith & Peter Ledbrook
Tom Corcoran – OSS Software - May 200920
The End
Comments http://joind.in/event/view/46Local Groovy User Group? http://gugse.com