groovy a scripting language for java

20
Groovy: a Scripting Language for Java Charles Anderson Western Skies Consulting [email protected]

Upload: charles-anderson

Post on 05-Dec-2014

4.381 views

Category:

Technology


6 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Groovy a Scripting Language for Java

Groovy: a Scripting Language for Java

Charles AndersonWestern Skies Consulting

[email protected]

Page 2: Groovy a Scripting Language for Java

Outline

• Introduction and Context

• Stupid Pet Tricks

• Using Groovy in a Java project

• Conclusion

• Grails?

Page 3: Groovy a Scripting Language for Java

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

Page 4: Groovy a Scripting Language for Java

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

Page 5: Groovy a Scripting Language for Java

Code Comparison

Java

Groovy

SyntaxSyntax

Groovy

Java

Groovy

Functionality

Page 6: Groovy a Scripting Language for Java

Java or Groovy?class Book {private String title;Book (String theTitle) {title = theTitle;

}String getTitle() {return title

}}

Page 7: Groovy a Scripting Language for Java

Stupid Pet Tricks

public class Main {public static void main(...) System.out.println(“Hello World”);

}}

println ‘Hello World’

Page 8: Groovy a Scripting Language for Java

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

}

Page 9: Groovy a Scripting Language for Java

Groovy Beanspublic class MyBean implements Serializable {

private String myProp;public String getMyProp()...public void setMyProp(...)...

}class MyBean implements Serializable {

String myProp;}

Page 10: Groovy a Scripting Language for Java

Terse NPE Safety

if (x!=null && x.y!=null) {z = x.y.z;

}

z = x?.y?.z

Page 11: Groovy a Scripting Language for Java

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’)

}}}

Page 12: Groovy a Scripting Language for Java

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

Page 13: Groovy a Scripting Language for Java

Exorcising Inner Classes

t = new Thread(new Runable() {public void run() {// some code

}});t.start();

Thread.start { //some code }

Page 14: Groovy a Scripting Language for Java

Java Iteration

for (Iterator it=coll.iterator(); it.hasNext(); ) {

Item item=(Item)it.next();//some code

}

for (Item item : coll) {// some code

}

Page 15: Groovy a Scripting Language for Java

Groovy Iteration

coll.each(item-> // some code)

coll.findAll(it % 2 == 0).each(...)

new File(‘foo.txt’).eachLine { println it }

Page 16: Groovy a Scripting Language for Java

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

Page 17: Groovy a Scripting Language for Java

using Groovy

• Little development tasks - automation

• Can script COM and Windows

• Experiments and spiking

• Testing - unit tests, data loading, scaffolding

Page 18: Groovy a Scripting Language for Java

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?

Page 19: Groovy a Scripting Language for Java

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

Page 20: Groovy a Scripting Language for Java

Grails Commands

Usage: grails commandcreate-apprun-appcreate-domain-class Entitygenerate-all Entitygenerate-webtestrun-webtestwar