SlideShare ist ein Scribd-Unternehmen logo
1 von 57
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)
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
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
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")
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, …
Summary
Groovy compiles to bytecodes
Groovy JDK
Native collections
Closures
Builders
Metaprogramming

Weitere ähnliche Inhalte

Was ist angesagt?

AST Transformations
AST TransformationsAST Transformations
AST TransformationsHamletDRC
 
Grooscript gr8conf
Grooscript gr8confGrooscript gr8conf
Grooscript gr8confGR8Conf
 
JRuby @ Boulder Ruby
JRuby @ Boulder RubyJRuby @ Boulder Ruby
JRuby @ Boulder RubyNick Sieger
 
Clojure for Java developers
Clojure for Java developersClojure for Java developers
Clojure for Java developersJohn Stevenson
 
Groovy presentation
Groovy presentationGroovy presentation
Groovy presentationManav Prasad
 
Better DSL Support for Groovy-Eclipse
Better DSL Support for Groovy-EclipseBetter DSL Support for Groovy-Eclipse
Better DSL Support for Groovy-EclipseAndrew Eisenberg
 
TclOO: Past Present Future
TclOO: Past Present FutureTclOO: Past Present Future
TclOO: Past Present FutureDonal Fellows
 
Ast transformations
Ast transformationsAst transformations
Ast transformationsHamletDRC
 
Building native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahBuilding native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahNick Plante
 
The TclQuadcode Compiler
The TclQuadcode CompilerThe TclQuadcode Compiler
The TclQuadcode CompilerDonal Fellows
 
GTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with GroovyGTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with GroovyAndres Almiray
 
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume LaforgeGroovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume LaforgeGuillaume Laforge
 
Javaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 GroovytestingJavaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 GroovytestingAndres Almiray
 
Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - StockholmJan Kronquist
 
Spock: A Highly Logical Way To Test
Spock: A Highly Logical Way To TestSpock: A Highly Logical Way To Test
Spock: A Highly Logical Way To TestHoward Lewis Ship
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojureAbbas Raza
 

Was ist angesagt? (20)

AST Transformations
AST TransformationsAST Transformations
AST Transformations
 
Grooscript gr8conf
Grooscript gr8confGrooscript gr8conf
Grooscript gr8conf
 
Adventures in TclOO
Adventures in TclOOAdventures in TclOO
Adventures in TclOO
 
Python lec4
Python lec4Python lec4
Python lec4
 
JRuby @ Boulder Ruby
JRuby @ Boulder RubyJRuby @ Boulder Ruby
JRuby @ Boulder Ruby
 
Clojure for Java developers
Clojure for Java developersClojure for Java developers
Clojure for Java developers
 
Groovy presentation
Groovy presentationGroovy presentation
Groovy presentation
 
Better DSL Support for Groovy-Eclipse
Better DSL Support for Groovy-EclipseBetter DSL Support for Groovy-Eclipse
Better DSL Support for Groovy-Eclipse
 
TclOO: Past Present Future
TclOO: Past Present FutureTclOO: Past Present Future
TclOO: Past Present Future
 
Ast transformations
Ast transformationsAst transformations
Ast transformations
 
Python lec5
Python lec5Python lec5
Python lec5
 
Building native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahBuilding native Android applications with Mirah and Pindah
Building native Android applications with Mirah and Pindah
 
The TclQuadcode Compiler
The TclQuadcode CompilerThe TclQuadcode Compiler
The TclQuadcode Compiler
 
GTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with GroovyGTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with Groovy
 
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume LaforgeGroovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
 
Ggug spock
Ggug spockGgug spock
Ggug spock
 
Javaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 GroovytestingJavaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 Groovytesting
 
Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - Stockholm
 
Spock: A Highly Logical Way To Test
Spock: A Highly Logical Way To TestSpock: A Highly Logical Way To Test
Spock: A Highly Logical Way To Test
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 

Andere mochten auch

Unit 2 3 1 Costs Of Production
Unit 2 3 1 Costs Of ProductionUnit 2 3 1 Costs Of Production
Unit 2 3 1 Costs Of ProductionCorey Topf
 
Week 34 Sponges
Week 34 SpongesWeek 34 Sponges
Week 34 SpongesCorey Topf
 
Uxd blog workshop
Uxd blog workshopUxd blog workshop
Uxd blog workshopHans Kemp
 
Legal aspects of data gathering and information exchange
Legal aspects of data gathering and information exchangeLegal aspects of data gathering and information exchange
Legal aspects of data gathering and information exchangeStevenSegaert
 
Week 6 Sponges
Week 6 SpongesWeek 6 Sponges
Week 6 SpongesCorey Topf
 
Vocabulary myp9
Vocabulary myp9Vocabulary myp9
Vocabulary myp9Corey Topf
 
Durftevragen
DurftevragenDurftevragen
Durftevragenroemen
 
Quick Quiz Week1
Quick Quiz Week1Quick Quiz Week1
Quick Quiz Week1Corey Topf
 
Beyond The Advertising 
Into The Unknown - The Future Business and Communicat...
Beyond The Advertising 
Into The Unknown - The Future Business and Communicat...Beyond The Advertising 
Into The Unknown - The Future Business and Communicat...
Beyond The Advertising 
Into The Unknown - The Future Business and Communicat...Johan Ronnestam
 
Iad2 Cmd2 C Kwartaalopdracht 0910 Q1
Iad2 Cmd2 C Kwartaalopdracht 0910 Q1Iad2 Cmd2 C Kwartaalopdracht 0910 Q1
Iad2 Cmd2 C Kwartaalopdracht 0910 Q1Hans Kemp
 
Iad1 0708Q2 Hoorcollege 1 Structuur, Flow En Navigatie
Iad1 0708Q2 Hoorcollege 1   Structuur, Flow En NavigatieIad1 0708Q2 Hoorcollege 1   Structuur, Flow En Navigatie
Iad1 0708Q2 Hoorcollege 1 Structuur, Flow En NavigatieHans Kemp
 
Fordonsindustrin — Kunskap och kompetens i framtiden
Fordonsindustrin — Kunskap och kompetens i framtidenFordonsindustrin — Kunskap och kompetens i framtiden
Fordonsindustrin — Kunskap och kompetens i framtidenJohan Ronnestam
 
The bambino tour
The bambino tourThe bambino tour
The bambino tourelenargy
 
Interactieve datavisualisaties
Interactieve datavisualisatiesInteractieve datavisualisaties
Interactieve datavisualisatiesHans Kemp
 
Week 13 Sponges
Week 13 SpongesWeek 13 Sponges
Week 13 SpongesCorey Topf
 

Andere mochten auch (20)

Unit 2 3 1 Costs Of Production
Unit 2 3 1 Costs Of ProductionUnit 2 3 1 Costs Of Production
Unit 2 3 1 Costs Of Production
 
Techo june6th
Techo june6thTecho june6th
Techo june6th
 
Week 34 Sponges
Week 34 SpongesWeek 34 Sponges
Week 34 Sponges
 
Uxd blog workshop
Uxd blog workshopUxd blog workshop
Uxd blog workshop
 
Legal aspects of data gathering and information exchange
Legal aspects of data gathering and information exchangeLegal aspects of data gathering and information exchange
Legal aspects of data gathering and information exchange
 
Week 6 Sponges
Week 6 SpongesWeek 6 Sponges
Week 6 Sponges
 
Incroyable
IncroyableIncroyable
Incroyable
 
Vocabulary myp9
Vocabulary myp9Vocabulary myp9
Vocabulary myp9
 
Durftevragen
DurftevragenDurftevragen
Durftevragen
 
Techo april18
Techo april18Techo april18
Techo april18
 
Quick Quiz Week1
Quick Quiz Week1Quick Quiz Week1
Quick Quiz Week1
 
Freshmen
FreshmenFreshmen
Freshmen
 
Beyond The Advertising 
Into The Unknown - The Future Business and Communicat...
Beyond The Advertising 
Into The Unknown - The Future Business and Communicat...Beyond The Advertising 
Into The Unknown - The Future Business and Communicat...
Beyond The Advertising 
Into The Unknown - The Future Business and Communicat...
 
Iad2 Cmd2 C Kwartaalopdracht 0910 Q1
Iad2 Cmd2 C Kwartaalopdracht 0910 Q1Iad2 Cmd2 C Kwartaalopdracht 0910 Q1
Iad2 Cmd2 C Kwartaalopdracht 0910 Q1
 
Iad1 0708Q2 Hoorcollege 1 Structuur, Flow En Navigatie
Iad1 0708Q2 Hoorcollege 1   Structuur, Flow En NavigatieIad1 0708Q2 Hoorcollege 1   Structuur, Flow En Navigatie
Iad1 0708Q2 Hoorcollege 1 Structuur, Flow En Navigatie
 
Fordonsindustrin — Kunskap och kompetens i framtiden
Fordonsindustrin — Kunskap och kompetens i framtidenFordonsindustrin — Kunskap och kompetens i framtiden
Fordonsindustrin — Kunskap och kompetens i framtiden
 
The bambino tour
The bambino tourThe bambino tour
The bambino tour
 
Interactieve datavisualisaties
Interactieve datavisualisatiesInteractieve datavisualisaties
Interactieve datavisualisaties
 
Week 13 Sponges
Week 13 SpongesWeek 13 Sponges
Week 13 Sponges
 
Intro.
Intro.Intro.
Intro.
 

Ähnlich wie Making Java Groovy (JavaOne 2013)

Apache Groovy: the language and the ecosystem
Apache Groovy: the language and the ecosystemApache Groovy: the language and the ecosystem
Apache Groovy: the language and the ecosystemKostas Saidis
 
Groovy
GroovyGroovy
Groovyatonse
 
An Introduction to Groovy for Java Developers
An Introduction to Groovy for Java DevelopersAn Introduction to Groovy for Java Developers
An Introduction to Groovy for Java DevelopersKostas Saidis
 
Polyglot Programming @ Jax.de 2010
Polyglot Programming @ Jax.de 2010Polyglot Programming @ Jax.de 2010
Polyglot Programming @ Jax.de 2010Andres Almiray
 
Eclipsecon08 Introduction To Groovy
Eclipsecon08 Introduction To GroovyEclipsecon08 Introduction To Groovy
Eclipsecon08 Introduction To GroovyAndres Almiray
 
Groovy Fly Through
Groovy Fly ThroughGroovy Fly Through
Groovy Fly Throughniklal
 
Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)Jonathan Felch
 
Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Guillaume Laforge
 
Introduction To Groovy 2005
Introduction To Groovy 2005Introduction To Groovy 2005
Introduction To Groovy 2005Tugdual Grall
 
Groovy for Java Developers
Groovy for Java DevelopersGroovy for Java Developers
Groovy for Java DevelopersAndres Almiray
 
Groovy And Grails Introduction
Groovy And Grails IntroductionGroovy And Grails Introduction
Groovy And Grails IntroductionEric Weimer
 
The curious Life of JavaScript - Talk at SI-SE 2015
The curious Life of JavaScript - Talk at SI-SE 2015The curious Life of JavaScript - Talk at SI-SE 2015
The curious Life of JavaScript - Talk at SI-SE 2015jbandi
 
Polyglot Programming in the JVM
Polyglot Programming in the JVMPolyglot Programming in the JVM
Polyglot Programming in the JVMAndres Almiray
 
Introduction To Groovy
Introduction To GroovyIntroduction To Groovy
Introduction To Groovymanishkp84
 
Groovy a Scripting Language for Java
Groovy a Scripting Language for JavaGroovy a Scripting Language for Java
Groovy a Scripting Language for JavaCharles Anderson
 

Ähnlich wie Making Java Groovy (JavaOne 2013) (20)

Apache Groovy: the language and the ecosystem
Apache Groovy: the language and the ecosystemApache Groovy: the language and the ecosystem
Apache Groovy: the language and the ecosystem
 
Groovy
GroovyGroovy
Groovy
 
An Introduction to Groovy for Java Developers
An Introduction to Groovy for Java DevelopersAn Introduction to Groovy for Java Developers
An Introduction to Groovy for Java Developers
 
Polyglot Programming @ Jax.de 2010
Polyglot Programming @ Jax.de 2010Polyglot Programming @ Jax.de 2010
Polyglot Programming @ Jax.de 2010
 
Groovy
GroovyGroovy
Groovy
 
Groovy!
Groovy!Groovy!
Groovy!
 
Clojure And Swing
Clojure And SwingClojure And Swing
Clojure And Swing
 
What's New in Groovy 1.6?
What's New in Groovy 1.6?What's New in Groovy 1.6?
What's New in Groovy 1.6?
 
Eclipsecon08 Introduction To Groovy
Eclipsecon08 Introduction To GroovyEclipsecon08 Introduction To Groovy
Eclipsecon08 Introduction To Groovy
 
Groovy Fly Through
Groovy Fly ThroughGroovy Fly Through
Groovy Fly Through
 
Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)
 
Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007
 
Groovy.pptx
Groovy.pptxGroovy.pptx
Groovy.pptx
 
Introduction To Groovy 2005
Introduction To Groovy 2005Introduction To Groovy 2005
Introduction To Groovy 2005
 
Groovy for Java Developers
Groovy for Java DevelopersGroovy for Java Developers
Groovy for Java Developers
 
Groovy And Grails Introduction
Groovy And Grails IntroductionGroovy And Grails Introduction
Groovy And Grails Introduction
 
The curious Life of JavaScript - Talk at SI-SE 2015
The curious Life of JavaScript - Talk at SI-SE 2015The curious Life of JavaScript - Talk at SI-SE 2015
The curious Life of JavaScript - Talk at SI-SE 2015
 
Polyglot Programming in the JVM
Polyglot Programming in the JVMPolyglot Programming in the JVM
Polyglot Programming in the JVM
 
Introduction To Groovy
Introduction To GroovyIntroduction To Groovy
Introduction To Groovy
 
Groovy a Scripting Language for Java
Groovy a Scripting Language for JavaGroovy a Scripting Language for Java
Groovy a Scripting Language for Java
 

Making Java Groovy (JavaOne 2013)