Groovy a Scripting Language for Java

2,844 views

Published on

This was a talk I gave at the Salem Java Users Group on 1/6/09.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,844
On SlideShare
0
From Embeds
0
Number of Embeds
52
Actions
Shares
0
Downloads
64
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Groovy a Scripting Language for Java

  1. 1. Groovy: a Scripting Language for Java Charles Anderson Western Skies Consulting cander@westernskiesweb.com
  2. 2. Outline • Introduction and Context • Stupid Pet Tricks • Using Groovy in a Java project • Conclusion • Grails?
  3. 3. Introduction • There are now many dynamic and/or scripting languages that run on the Java Platform • Groovy is a scripting language specifically for the Java Platform - feature-rich, Java-friendly • Groovy compiles to Java classes & byte code - can use all of Java Platform
  4. 4. Introduction • Scripting - don’t have to explicitly compile • Typing - static or dynamic (optional/duck) • Mature - version 1.5, 1.6 Beta 2 • Books, websites, commercial backing, JSR • IDE Support
  5. 5. Code Comparison Groovy Groovy Java Java Syntax Functionality
  6. 6. Java or Groovy? class Book { private String title; Book (String theTitle) { title = theTitle; } String getTitle() { return title } }
  7. 7. Stupid Pet Tricks public class Main { public static void main(...) System.out.println(“Hello World”); }} println ‘Hello World’
  8. 8. More Powerful Switch switch (x) { case 0: // single int case 1..9: // range case [11,13,17]: // list case Float: // type check case ‘cat’: // string case ~/[A-Z][0-9]/: //regexp }
  9. 9. Groovy Beans public class MyBean implements Serializable { private String myProp; public String getMyProp()... public void setMyProp(...)... } class MyBean implements Serializable { String myProp; }
  10. 10. Terse NPE Safety if (x!=null && x.y!=null) { z = x.y.z; } z = x?.y?.z
  11. 11. Bob the Builder writer = new FileWriter(‘bob.html’) def page = new MarkupBuilder(writer) page.html{ head { title ‘An HTML Page’ } body { h1 ‘The big heading’ form (action:’submit’) { input(type:’checkbox’, id:’yes’) input(type:’checkbox’, id:’no’) }}}
  12. 12. Closures • A closure is a chunk of code that is a first- class entity/object in the language • From functional programming • Can accept parameters and return a value • Typical Java solution: inner classes, visitor pattern, template method pattern
  13. 13. Exorcising Inner Classes t = new Thread(new Runable() { public void run() { // some code }}); t.start(); Thread.start { //some code }
  14. 14. Java Iteration for (Iterator it=coll.iterator(); it.hasNext(); ) { Item item=(Item)it.next(); //some code } for (Item item : coll) { // some code }
  15. 15. Groovy Iteration coll.each(item-> // some code) coll.findAll(it % 2 == 0).each(...) new File(‘foo.txt’).eachLine { println it }
  16. 16. Embrace and Extend • Groovy “extends” Java classes - GDK • Object, GString, Collections, Files, Threads, Beans • Groovy enhances existing Java tools • JUnit, Ant, Swing • Reduce tedious boiler-plate - DRY
  17. 17. using Groovy • Little development tasks - automation • Can script COM and Windows • Experiments and spiking • Testing - unit tests, data loading, scaffolding
  18. 18. Conclusion • Groovy is a dynamic scripting language that is designed specifically for Java • Lots of “tricks” to make life easier, but they can be introduced gradually • Groovy is a playground for the future of Java - e.g., closures, properties • Questions, job leads, Grails?
  19. 19. Grails: Groovy on Rails • Accelerate and simplify web development on the Java Platform - but not Java Language • Deploys as a WAR file • MVC structure • Convention over configuration • Uses Spring and Hibernate under covers
  20. 20. Grails Commands Usage: grails command create-app run-app create-domain-class Entity generate-all Entity generate-webtest run-webtest war

×