18
Advanced Groovy Tips & Tricks

Advanced Groovy Tips and Tricks

Embed Size (px)

DESCRIPTION

Speaker: Kenneth Kousen Groovy has a very easy learning curve for Java developers, so many people become Groovy users without realizing all it can do. This presentation will examine features of Groovy that can make your life easier once you're past the initial adoption stage. Examples will include closure coercion, mixins, simple runtime metaprogramming, operator overloading, drop and take, a tour through some of the overlooked methods in the Groovy JDK, and more.

Citation preview

Page 1: Advanced Groovy Tips and Tricks

Advanced GroovyTips & Tricks

Page 3: Advanced Groovy Tips and Tricks

Use closures to implement interfaces

Replaces anonymous inner classes

Coerced Closures

Page 4: Advanced Groovy Tips and Tricks

Coerced Closures

Examples:Filename FilterICNDB

Page 5: Advanced Groovy Tips and Tricks

Coerced Closures

Use single closure

or

Map of method names to closures

Page 6: Advanced Groovy Tips and Tricks

Coerced Closures

ExampleUtility Methods

(also shows GroovyTestCase additions)

Page 7: Advanced Groovy Tips and Tricks

Runtime metaprogramming

Use the metaClass

Make Java classes easier to use

Used in Grails dynamic finders

Page 8: Advanced Groovy Tips and Tricks

Runtime metaprogramming

Examples:ComplexAdditional Logging methods

Page 9: Advanced Groovy Tips and Tricks

Operator Overloading

Adding operator methods

plus, minus, leftShiftequals

Page 10: Advanced Groovy Tips and Tricks

Operator Overloading

Example:Human resources

Page 11: Advanced Groovy Tips and Tricks

Creating a Range

Implement Comparable interface

Overridenextprevious

Page 12: Advanced Groovy Tips and Tricks

Creating a Range

Example:Train Stations

Page 13: Advanced Groovy Tips and Tricks

Groovy JDK methods

Functional methodscollect and inject

Collectionfind, findAllcombinationseachPermutation(Closure)max, min, sum, count, join, ...

Page 14: Advanced Groovy Tips and Tricks

Groovy JDK methods

Examples:sorting stringssumming numberspermutationscombinations... others also fall in this category ...

Page 15: Advanced Groovy Tips and Tricks

Groovy JDK Methods

"...url...".toURL().text

Use groovy.sql.Sql call stored procedures

Easy java.io.File processing

Page 16: Advanced Groovy Tips and Tricks

Groovy API

Examplessumming numbers, revisitedcalling a stored procedurebasic authentication

(also shows multiple returns)

Page 17: Advanced Groovy Tips and Tricks

AST transformations

@Immutable@Delegate@Singleton@TupleConstructor@EqualsAndHashCode@ToString@Canonical

Page 18: Advanced Groovy Tips and Tricks

Thanks for coming!

Source code located in GitHub at:https://github.com/kousen/AdvancedGroovy.git

Please complete the session evals