Upload
charles-anderson
View
4.383
Download
6
Embed Size (px)
DESCRIPTION
This was a talk I gave at the Salem Java Users Group on 1/6/09.
Citation preview
Outline
• Introduction and Context
• Stupid Pet Tricks
• Using Groovy in a Java project
• Conclusion
• Grails?
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
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
Code Comparison
Java
Groovy
SyntaxSyntax
Groovy
Java
Groovy
Functionality
Java or Groovy?class Book {private String title;Book (String theTitle) {title = theTitle;
}String getTitle() {return title
}}
Stupid Pet Tricks
public class Main {public static void main(...) System.out.println(“Hello World”);
}}
println ‘Hello World’
More Powerful Switch
switch (x) {case 0: // single intcase 1..9: // rangecase [11,13,17]: // listcase Float: // type checkcase ‘cat’: // stringcase ~/[A-Z][0-9]/: //regexp
}
Groovy Beanspublic class MyBean implements Serializable {
private String myProp;public String getMyProp()...public void setMyProp(...)...
}class MyBean implements Serializable {
String myProp;}
Terse NPE Safety
if (x!=null && x.y!=null) {z = x.y.z;
}
z = x?.y?.z
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’)
}}}
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
Exorcising Inner Classes
t = new Thread(new Runable() {public void run() {// some code
}});t.start();
Thread.start { //some code }
Java Iteration
for (Iterator it=coll.iterator(); it.hasNext(); ) {
Item item=(Item)it.next();//some code
}
for (Item item : coll) {// some code
}
Groovy Iteration
coll.each(item-> // some code)
coll.findAll(it % 2 == 0).each(...)
new File(‘foo.txt’).eachLine { println it }
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
using Groovy
• Little development tasks - automation
• Can script COM and Windows
• Experiments and spiking
• Testing - unit tests, data loading, scaffolding
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?
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
Grails Commands
Usage: grails commandcreate-apprun-appcreate-domain-class Entitygenerate-all Entitygenerate-webtestrun-webtestwar