Tom Corcoran - Introduction to Groovy and Grails - May 2009
1. Tom Corcoran – OSS Software - May 2009 1 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
2.
3. Tom Corcoran – OSS Software - May 2009 3 Genesis First begun in 2004 by James Strachan, lost momentum, project reenergized by Guillaume Laforgewith 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 Machine bytecode (.class file) As a result seamless 2 way integration with Java code and libraries Deployed as a jar (zip) file
4. Tom Corcoran – OSS Software - May 2009 4 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 expressiveness of 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
5. Tom Corcoran – OSS Software - May 2009 5 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 groovyMyCode.groovy Groovy Console To run Swing interactive console type GroovyConsole
10. Tom Corcoran – OSS Software - May 2009 8 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 with static language code
11. Tom Corcoran – OSS Software - May 2009 9 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 map A map is a mapping from unique unordered keys to values assert Provide expression that you are asserting will be true, otherwise throws AssertionError new reserved word
12. Tom Corcoran – OSS Software - May 2009 10 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
13. Tom Corcoran – OSS Software - May 2009 11 Groovy Math Groovy uses exact, or decimal math for default calculationsThis means that user computations like: 1.1 + 0.1 == 1.2 will 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
14. Tom Corcoran – OSS Software - May 2009 12 Goodies Groovy Truth Groovy provides parsers & slurpers for working with XML Use XmlSlurper to slurp up a xml file
15. Tom Corcoran – OSS Software - May 2009 13 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
16. Tom Corcoran – OSS Software - May 2009 14 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)
17. Tom Corcoran – OSS Software - May 2009 15 Closures Closures are pieces of code that can augment, refine or enhance another piece of code Closures can be passed to methods GDK Examples
18. Tom Corcoran – OSS Software - May 2009 16 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
19. Tom Corcoran – OSS Software - May 2009 17 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 times slower 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/
20. Tom Corcoran – OSS Software - May 2009 18 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
21. Tom Corcoran – OSS Software - May 2009 19 Resources Groovy http://groovy.codehaus.org Programming Groovy : VenkatSubramaniam Groovy Recipes: Scott Davis Grails http://www.grails.org The Definitive Guide to Grails: Graeme Rocher Grails in Action: Glen Smith & Peter Ledbrook
22. Tom Corcoran – OSS Software - May 2009 20 The End Comments http://joind.in/event/view/46 Local Groovy User Group? http://gugse.com