SlideShare a Scribd company logo
Making Java Groovy
Simplify Your Java Development
With Groovy
JavaOne 2013
Contact Info
Ken Kousen
President, Kousen IT, Inc.
www.kousenit.com
ken.kousen@kousenit.com
@kenkousen
Making Java Groovy
http://manning.com/kousen
Groovy
JVM language
Groovy
JVM language
compiles to bytecodes
Groovy
JVM language
compiles to bytecodes
optional semicolons
Groovy
JVM language
compiles to bytecodes
optional semicolons
optional parentheses
Groovy
JVM language
compiles to bytecodes
optional semicolons
optional parentheses
everything is an object
Adding Groovy to Java
Adding Groovy to Java
Actually, Boromir, One Does
Adding Groovy to Java
Actually, Boromir, One Does
(and simply)
Adding Groovy to Java
Actually, Boromir, One Does
(and simply)
(and btw, Lord Stark, you might want
to stay out
of King’s Landing…)
Adding Groovy to Java
Demo: Execute compiled Groovy
with java command
Operator Overloading
All operators in Groovy invoke methods
Operator Overloading
All operators in Groovy invoke methods
a + b a.plus(b)
a – b a.minus(b)
a * b a.multiply(b)
a[b] a.getAt(b)
a[b] = c a.putAt(b,c)
a == b a.equals(b) or
a.compareTo(b)
Operator Overloading
All operators in Groovy invoke methods
http://groovy.codehaus.org/Operator+Overloading
a + b a.plus(b)
a – b a.minus(b)
a * b a.multiply(b)
a[b] a.getAt(b)
a[b] = c a.putAt(b,c)
a == b a.equals(b) or
a.compareTo(b)
Making Java Groovy (JavaOne 2013)
POGOs
Actually, YES
POGOs
Actually, YES
Plain Old Groovy Objects
POGOs
Generated getters / setters
POGOs
Generated getters / setters
Map-based constructor
POGOs
Generated getters / setters
Map-based constructor
AST transformations
@ToString
@EqualsAndHashCode
@TupleConstructor
@Canonical
Adding Groovy to Java
Demo:
Account class in Groovy
NumberFormat from Java
Test in Java
Making Java Groovy (JavaOne 2013)
Collections
Okay, make it so…
Collections
Okay, make it so…
Native collections
Collections
Okay, make it so…
Native collections
Ranges
Collections
Okay, make it so…
Native collections
Ranges
Lists and Sets
Collections
Okay, make it so…
Native collections
Ranges
Lists and Sets
Maps
Ranges
Implement Comparable
next()
previous()
Number, Date, String, …
Lists
[3, 1, 4, 1, 5, 9, …]
['this', 'is', 'a', 'list’]
Sets
[3, 1, 4, 1, 5, 9, …] as Set
Set strings
Maps
def map = [a:1, b:2, c:3]
map.d = 4
map['e'] = 5
map.put('f', 6)
Closures
{ … it … }
{ x, y -> x + y }
Closures
{ … it … }
{ x, y -> x + y }
anonymous functions as first-class objects
Closures
{ … it … }
{ x, y -> x + y }
anonymous functions as first-class objects
(oversimplified, but it’s a start…)
Closures
Methods on collections:
each
collect
findAll
inject
Functional Groovy
Closures  lambdas (if pure)
collect  map
findAll  filter
inject  reduce
Making Java Groovy (JavaOne 2013)
Groovy JDK
Java classes enhanced by Groovy
through metaprogramming
Number
String
Date
Collection
URL
…
RESTful Web Service
Google v3 Geocoder
Convert addresses to latitude / longitude
Geocoder
Base URL
URLEncoder.encode(it, 'UTF-8').join(',’)
Geocoder
Base URL
URLEncoder.encode(it, 'UTF-8').join(',’)
params.collect { k,v -> "$k=$v"
}.join('&')
Geocoder
Base URL
URLEncoder.encode(it, 'UTF-8').join(',’)
params.collect { k,v -> "$k=$v"
}.join('&')
XmlSlurper.parse("$base$qs")
Making Java Groovy (JavaOne 2013)
JSON
ICNDB  the Internet Chuck Norris Database
http://api.icndb.com/jokes/random?
limitTo=[nerdy]&
firstName=…&
lastName=…
ICNDB
JsonSlurper
Metaprogramming
Spock Testing
Spock
Specification + Mock
Spock
Specification + Mock
Includes JUnit runner
Spock
Specification + Mock
Includes JUnit runner
Data-driven tests
Spock
Specification + Mock
Includes JUnit runner
Data-driven tests
Database-driven tests
Gradle
Build tool
Gradle
Build tool
DSL for builds
Gradle
Build tool
DSL for builds
Build script in Groovy rather than XML
Gradle
Build tool
DSL for builds
Build script in Groovy rather than XML
Used by:
Hibernate, Spring, Android, …
Making Java Groovy (JavaOne 2013)
Summary
Groovy compiles to bytecodes
Groovy JDK
Native collections
Closures
Builders
Metaprogramming

More Related Content

PPT
2007 09 10 Fzi Training Groovy Grails V Ws
ODP
AST Transformations at JFokus
PDF
Infinum android talks_10_getting groovy on android
PDF
Oscon Java Testing on the Fast Lane
PDF
GR8Conf 2009: What's New in Groovy 1.6? by Guillaume Laforge
PDF
Spock: Test Well and Prosper
PDF
ConFess Vienna 2015 - Metaprogramming with Groovy
ODP
Groovy Ast Transformations (greach)
2007 09 10 Fzi Training Groovy Grails V Ws
AST Transformations at JFokus
Infinum android talks_10_getting groovy on android
Oscon Java Testing on the Fast Lane
GR8Conf 2009: What's New in Groovy 1.6? by Guillaume Laforge
Spock: Test Well and Prosper
ConFess Vienna 2015 - Metaprogramming with Groovy
Groovy Ast Transformations (greach)

What's hot (20)

ODP
AST Transformations
PDF
Grooscript gr8conf
PPTX
Adventures in TclOO
PPTX
Python lec4
PDF
JRuby @ Boulder Ruby
PDF
Clojure for Java developers
PPT
Groovy presentation
PDF
Better DSL Support for Groovy-Eclipse
PPTX
TclOO: Past Present Future
ODP
Ast transformations
PPTX
Python lec5
PPTX
Building native Android applications with Mirah and Pindah
PDF
The TclQuadcode Compiler
PPT
GTAC Boosting your Testing Productivity with Groovy
ZIP
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
PPT
Ggug spock
PPT
Javaone2008 Bof 5101 Groovytesting
PDF
Clojure for Java developers - Stockholm
PDF
Spock: A Highly Logical Way To Test
PDF
Introduction to clojure
AST Transformations
Grooscript gr8conf
Adventures in TclOO
Python lec4
JRuby @ Boulder Ruby
Clojure for Java developers
Groovy presentation
Better DSL Support for Groovy-Eclipse
TclOO: Past Present Future
Ast transformations
Python lec5
Building native Android applications with Mirah and Pindah
The TclQuadcode Compiler
GTAC Boosting your Testing Productivity with Groovy
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Ggug spock
Javaone2008 Bof 5101 Groovytesting
Clojure for Java developers - Stockholm
Spock: A Highly Logical Way To Test
Introduction to clojure
Ad

Viewers also liked (20)

PPTX
Unit 2 3 1 Costs Of Production
PDF
Techo june6th
PDF
Week 34 Sponges
PPTX
Uxd blog workshop
PPT
Legal aspects of data gathering and information exchange
PDF
Week 6 Sponges
PPS
Incroyable
PDF
Vocabulary myp9
KEY
Durftevragen
PDF
Techo april18
PPTX
Quick Quiz Week1
PPTX
Freshmen
PDF
Beyond The Advertising 
Into The Unknown - The Future Business and Communicat...
PPTX
Iad2 Cmd2 C Kwartaalopdracht 0910 Q1
PPT
Iad1 0708Q2 Hoorcollege 1 Structuur, Flow En Navigatie
PDF
Fordonsindustrin — Kunskap och kompetens i framtiden
PPTX
The bambino tour
PPTX
Interactieve datavisualisaties
PDF
Week 13 Sponges
PDF
Intro.
Unit 2 3 1 Costs Of Production
Techo june6th
Week 34 Sponges
Uxd blog workshop
Legal aspects of data gathering and information exchange
Week 6 Sponges
Incroyable
Vocabulary myp9
Durftevragen
Techo april18
Quick Quiz Week1
Freshmen
Beyond The Advertising 
Into The Unknown - The Future Business and Communicat...
Iad2 Cmd2 C Kwartaalopdracht 0910 Q1
Iad1 0708Q2 Hoorcollege 1 Structuur, Flow En Navigatie
Fordonsindustrin — Kunskap och kompetens i framtiden
The bambino tour
Interactieve datavisualisaties
Week 13 Sponges
Intro.
Ad

Similar to Making Java Groovy (JavaOne 2013) (20)

PDF
Learning groovy -EU workshop
PDF
Introduction to Groovy (Serbian Developer Conference 2013)
ODP
Learning groovy 1: half day workshop
PPTX
Groovy And Grails Introduction
PDF
Groovy.pptx
PDF
An Introduction to Groovy for Java Developers
PDF
Introductionto fp with groovy
PPT
Groovy Update - JavaPolis 2007
PDF
Apache Groovy: the language and the ecosystem
PPT
Groovy unleashed
PPTX
getting-your-groovy-on
ODP
KEY
Introduction to Groovy
PDF
Groovy for java developers
PPT
Groovy Introduction - JAX Germany - 2008
PPTX
Groovy Programming Language
KEY
Startup groovysession1
PDF
Groovy in the Enterprise - Case Studies - TSSJS Prague 2008 - Guillaume Laforge
PPTX
Introduction to Grails
Learning groovy -EU workshop
Introduction to Groovy (Serbian Developer Conference 2013)
Learning groovy 1: half day workshop
Groovy And Grails Introduction
Groovy.pptx
An Introduction to Groovy for Java Developers
Introductionto fp with groovy
Groovy Update - JavaPolis 2007
Apache Groovy: the language and the ecosystem
Groovy unleashed
getting-your-groovy-on
Introduction to Groovy
Groovy for java developers
Groovy Introduction - JAX Germany - 2008
Groovy Programming Language
Startup groovysession1
Groovy in the Enterprise - Case Studies - TSSJS Prague 2008 - Guillaume Laforge
Introduction to Grails

Making Java Groovy (JavaOne 2013)