SlideShare a Scribd company logo
1 of 24
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_20143DS.COM©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
Getting your Groovy On
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
3
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
What is Groovy?
4
• Groovy is a lightweight, "low-ceremony", dynamic, object
oriented language that runs on the Java Virtual Machine
• Compiles to Bytecode
• Can exercise all Java libraries
• Syntax is similar to Java with some similarities to Python/Ruby
• Requires only a single additional jar beyond what comes out of
box with JRE
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
Why Use Groovy?
5
Groovy is Java…
but better!
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
Why Use Groovy?
6
• It's both compiled and dynamic! (Discuss whether dynamic languages are good or bad)
• Groovy's dynamic "meta programming" api allows us to add functionality at runtime, which for better
or worse gives us the ability to extend core Java classes that haven't traditionally been extensible,
similar to extension methods in C#.
• Groovy supports nifty semantic sugar for looping
(see WaysToLoop.groovy )
• Groovy IS Java - Groovy extends and consumes any java code. i.e. all things Groovy ==
java.lang.Object
• Groovy makes dealing with collections so much easier than java
• Groovy supports dynamic typing via the def keyword
• Groovy is Functional! It supports a simplified closure syntax
• Groovy hates verbosity
• Simplified constructors
• No need to try/catch checked exceptions
• Semicolons not required
• “return” not required but inferred
• parens not required
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
Why Use Groovy?
7
• Groovy imports many common namespaces by default so you don't have to
clutter code with imports - java.lang, java.util.Calendar, java.util.io, java.net
• Simplified XML and JSoN Parsing
◦ see example XmlParsingTests
• Simplified Getter/Setters
◦ Like Properties in C#
◦ Groovy will auto-generate a getter and setter for any public field in a
class
◦ One can override the default getter and setter as necessary
• Groovy Console
◦ Allows you to play with groovy idiom without compiling
◦ Acts as an interpreter so groovy can be used for scripting
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
Where can I use Groovy?
• In build scripts
• In Java Apps
• In Groovy apps
• In a console window
• In a generic script
• On *nix, Windows, MacOS, Solaris, Anywhere a JVM
is available
• Mobile
• Desktop
• Embedded
8
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
Who uses Groovy?
• Web Developers
• Java Developers
• Script Developers
• Build Developers
• Lazy Developers
• NOT Max Guernsey!
• Anyone who works on the JVM!
9
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
How do I get my Groovy on
Environment and Installation
• Windows:
• Install the JDK 1.5 or newer
• http://groovy.codehaus.org/Download -> Windows Installer
• *Nix - Unix, Linux, MacOSX
• Install the JDK or OpenJDK 1.5 or later
• Groovy Environment Manager (GVM) - http://gvmtool.net
• curl -s get.vmtool.net | bash
• gvm install groovy
• done
10
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
Closures - Methods as First Class citizens
11
• Short, anonymous code blocks that can operate on values from the outer scope
• Can be referenced by a variable
• Can be passed into method as a parameter
Example:
10.times { println "Hello from position $it" } <-- closure
"it” = default name of the parameter when a single parameter closure
can be negated by putting a closure operator (->) in front of the closure body
- 10.times { -> println “Hello from position $it } —> throws exception
Syntax:
def closure = { <param1>, <param2>, <param3> ... ->
... do stuff ...
}
usage:
def foo = bar.execute(closure);
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
Closures
12
Closures enable C# “using {}” similar behavior to JVM to handle resource cleanup
See code. ExampleTests.groovy -> testSimpleClosure
• Best Practices:
• Keep closures small and cohesive
• Limit use of dynamic properties like determining the number of parameters at
run time
• Start with conventional methods and introduce closures during refactoring
• Use type specificity when overriding a method implementation using meta
programming
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
Groovy Collections: Lists
The “killer feature” of groovy is the Groovy Collection library
Built on top of the Java Collections, not re-inventing the wheel
Declaration:
Groovy -> exampleList = [1,2,3,4,5,6]
Java -> ArrayList exampleList = new ArrayList();
exampleList.Add(1);
…
exampleList.Add(6)
Both create java.util.ArrayList under the covers
Neat feature:
Range operator somelist[1..6] returns the values in the list from position 1 to position 6
13
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
Groovy Collections: Maps
Declaration:
def workers = [William: "Footman", Charlie: "Butler", John: "Lord's Valet", Thomas: "First Footman", Robert:
"Lord", Tom: “Chauffer"]
**Keys may be unquoted if they are “well-behaved”, meaning they don’t contain special characters
Iteration:
.each {} => takes a closure that can either be of a single parameter representing a single map entry object, or two
params when you want discrete key/value pairs
.collect {} - returns the result of a closures operations against a list/map, as a list/map. Similar to a map function in
other languages
Location:
Keys may be address like a property on the collection i.e. worker.William => returns “Footman”
.find - picks the first element that matches closure and returns
.findAll - similar to find but returns all occurrences that match the closure
.any - similar to LINQ’s Any
.every - similar to LINQ's All
Others:
.groupBy
14
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
Dynamism: Metaobject Protocol (MOP)
What is MOP?
The Metaobject Protocol is the feature that allows Groovy to introspect and manipulate program
code at runtime. It is what makes Groovy “dynamic”.
• Groovy Object interface:
• Used to add dynamic behavior to POJO’s
• MetaClass can be set at runtime to “override” behavior
• Groovy “Interceptable" interface: Extends Groovy Object to route all method calls throw the
invokeMethod() call e.g. Makes AOP like interception possible
• Property and Method Inspection
• MetaObjectProtocol.getMetaMethod() , .getStaticMetaMethod() , .getMetaProperty(), and
.getStaticMetaProperty(), .respondsTo(), .hasProperty()
• Method Interception and Method Injection
• ExpandoMetaClass
15
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
Dynamism: Groovy is dynamic
Metaobject Protocol (MOP)
• Property and Method Inspection
• Method Interception
• ExpandoMetaClass
• Method Injection
• Categories -
• Most “controlled’ way of method injection
• Syntax similar to .net Extension methods.
• Requires static method where first parameter is “self”
• require use() { } syntax in order to invoke
• @Category annotation - simplifies syntax of category definition such that “self” reference is
not required
• To much overhead - Use ExpandoMetaClass for simplicity
• ExpandoMetaClass - see ExampleTests.groovy
• Mixins - allows composition of many classes into a single execution space (@Mixin
annotation) or method injection via the metaClass.mixin() method
16
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
Dynamism: Best Practices
Best practices:
• Use def for method and property definition but not for public api. While a nice convenience, its
nice to give your api consumers a specific type so they don’t have to deep dive into your code.
• Use Category method injection for infrequent method injection where isolation of the added
methods is important
• Unit testing is imperative!!!
• Conformance checking is a good idea
• someObject.metaClass.respondsTo(someObject,”someMethodName”)
• Use @TypeChecked annotation to enable compile time type checking. This disables run time
meta programming (method injection) but does not prevent use of GDK added method or
customer extension methods
• Use @CompileStatic to force compiler to generated more efficient byte-code. This disables all
the dynamic
17
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
C# Similarities
• Named method arguments
• Optional method arguments
• Default values for method arguments
• “as” Keyword for casting and namespace aliasing
• dynamic typing
• Type inference - def keyword similar to var keyword
• “with{}” keyword works with Java Beans similar to Object
Initializers
18
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
Things I don’t like about Groovy
:-(• Groovy makes all things public by default. Requires explicit work to
hide methods and members. Groovy does not respect "private"
keyword on member variables. In order to make a member truly
private, one must overload the setter to prevent external setting and
use the "final" keyword to make it readonly.
• Groovy allows a programmer to selectively implement interfaces, i.e.
you don't have to implement all members.
• Dynamic typing performance can be up to 10% less efficient than
statically typed Java code
19
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
Discussion Point
Design by Capability vs Design by Interface - DuckTyping
The idea that rather than using strict interfaces, we can assume an object
implements a capability that we want to use and just use it.
Requires intimate knowledge of codebase, less reliance on compiler
20
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
Gradle
• A groovy based build framework
• Integrates with ant, ivy and maven
• Manages 3rd party dependencies using maven infrastructure
• Easy to learn syntax. No ugly XML.
• Plug-Ins for many compilers and build execution engines.
21
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
Spock
• A specification driven testing framework
• JUnit integration allows Specifications to be run in any IDE that can execute
JUnit tests.
• Expressive, test names
• Expressive test output
• Doesn’t enforce any particular testing strategy
• Easy to learn groovy syntax
22
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014
Resources
• Groovy - http://codehaus.org
• GDK - beta.groovy-lang.org/docs/latest/html/groovy-jdk
• http://groovy.codehaus.org/Groovy+style+and+language+feature+guidelines+for+
Java+developers
• http://www.gradle.org/
• https://grails.org/
• https://code.google.com/p/spock/
23
3DS.COM/BIOVIA©DassaultSystèmes|ConfidentialInformation|8/25/2014|ref.:3DS_Document_2014

More Related Content

What's hot

NodeJS guide for beginners
NodeJS guide for beginnersNodeJS guide for beginners
NodeJS guide for beginnersEnoch Joshua
 
Metaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And GrailsMetaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And GrailszenMonkey
 
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
 
eXo EC - Groovy Programming Language
eXo EC - Groovy Programming LanguageeXo EC - Groovy Programming Language
eXo EC - Groovy Programming LanguageHoat Le
 
Spring-batch Groovy y Gradle
Spring-batch Groovy y GradleSpring-batch Groovy y Gradle
Spring-batch Groovy y GradleAntonio Mas
 
Testing cloud and kubernetes applications - ElasTest
Testing cloud and kubernetes applications - ElasTestTesting cloud and kubernetes applications - ElasTest
Testing cloud and kubernetes applications - ElasTestMicael Gallego
 
OWF12/PAUG Conf Days Dart a new html5 technology, nicolas geoffray, softwar...
OWF12/PAUG Conf Days Dart   a new html5 technology, nicolas geoffray, softwar...OWF12/PAUG Conf Days Dart   a new html5 technology, nicolas geoffray, softwar...
OWF12/PAUG Conf Days Dart a new html5 technology, nicolas geoffray, softwar...Paris Open Source Summit
 
SFScon 2020 - Juri Strumpflohner - Beyond Basics - Scaling Development acros...
 SFScon 2020 - Juri Strumpflohner - Beyond Basics - Scaling Development acros... SFScon 2020 - Juri Strumpflohner - Beyond Basics - Scaling Development acros...
SFScon 2020 - Juri Strumpflohner - Beyond Basics - Scaling Development acros...South Tyrol Free Software Conference
 
What Makes Objective C Dynamic?
What Makes Objective C Dynamic?What Makes Objective C Dynamic?
What Makes Objective C Dynamic?Kyle Oba
 
really really really awesome php application with bdd behat and iterfaces
really really really awesome php application with bdd behat and iterfacesreally really really awesome php application with bdd behat and iterfaces
really really really awesome php application with bdd behat and iterfacesGiulio De Donato
 
SyScan 2016 - Remote code execution via Java native deserialization
SyScan 2016 - Remote code execution via Java native deserializationSyScan 2016 - Remote code execution via Java native deserialization
SyScan 2016 - Remote code execution via Java native deserializationDavid Jorm
 
Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020
Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020
Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020Johnny Sung
 
Objective c runtime
Objective c runtimeObjective c runtime
Objective c runtimeInferis
 

What's hot (20)

NodeJS guide for beginners
NodeJS guide for beginnersNodeJS guide for beginners
NodeJS guide for beginners
 
NodeJS: an Introduction
NodeJS: an IntroductionNodeJS: an Introduction
NodeJS: an Introduction
 
Metaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And GrailsMetaprogramming Techniques In Groovy And Grails
Metaprogramming Techniques In Groovy And Grails
 
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 android
Groovy androidGroovy android
Groovy android
 
Groovy & Grails
Groovy & GrailsGroovy & Grails
Groovy & Grails
 
eXo EC - Groovy Programming Language
eXo EC - Groovy Programming LanguageeXo EC - Groovy Programming Language
eXo EC - Groovy Programming Language
 
Whats New In Groovy 1.6?
Whats New In Groovy 1.6?Whats New In Groovy 1.6?
Whats New In Groovy 1.6?
 
Spring-batch Groovy y Gradle
Spring-batch Groovy y GradleSpring-batch Groovy y Gradle
Spring-batch Groovy y Gradle
 
Introduction to NodeJS
Introduction to NodeJSIntroduction to NodeJS
Introduction to NodeJS
 
Testing cloud and kubernetes applications - ElasTest
Testing cloud and kubernetes applications - ElasTestTesting cloud and kubernetes applications - ElasTest
Testing cloud and kubernetes applications - ElasTest
 
OWF12/PAUG Conf Days Dart a new html5 technology, nicolas geoffray, softwar...
OWF12/PAUG Conf Days Dart   a new html5 technology, nicolas geoffray, softwar...OWF12/PAUG Conf Days Dart   a new html5 technology, nicolas geoffray, softwar...
OWF12/PAUG Conf Days Dart a new html5 technology, nicolas geoffray, softwar...
 
SFScon 2020 - Juri Strumpflohner - Beyond Basics - Scaling Development acros...
 SFScon 2020 - Juri Strumpflohner - Beyond Basics - Scaling Development acros... SFScon 2020 - Juri Strumpflohner - Beyond Basics - Scaling Development acros...
SFScon 2020 - Juri Strumpflohner - Beyond Basics - Scaling Development acros...
 
Golang
GolangGolang
Golang
 
What Makes Objective C Dynamic?
What Makes Objective C Dynamic?What Makes Objective C Dynamic?
What Makes Objective C Dynamic?
 
really really really awesome php application with bdd behat and iterfaces
really really really awesome php application with bdd behat and iterfacesreally really really awesome php application with bdd behat and iterfaces
really really really awesome php application with bdd behat and iterfaces
 
Practical Groovy DSL
Practical Groovy DSLPractical Groovy DSL
Practical Groovy DSL
 
SyScan 2016 - Remote code execution via Java native deserialization
SyScan 2016 - Remote code execution via Java native deserializationSyScan 2016 - Remote code execution via Java native deserialization
SyScan 2016 - Remote code execution via Java native deserialization
 
Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020
Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020
Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020
 
Objective c runtime
Objective c runtimeObjective c runtime
Objective c runtime
 

Similar to getting-your-groovy-on

Groovy In the Cloud
Groovy In the CloudGroovy In the Cloud
Groovy In the CloudJim Driscoll
 
ASP.NET MVC Workshop for Women in Technology
ASP.NET MVC Workshop for Women in TechnologyASP.NET MVC Workshop for Women in Technology
ASP.NET MVC Workshop for Women in TechnologyMałgorzata Borzęcka
 
MvvmCross Introduction
MvvmCross IntroductionMvvmCross Introduction
MvvmCross IntroductionStuart Lodge
 
MvvmCross Seminar
MvvmCross SeminarMvvmCross Seminar
MvvmCross SeminarXamarin
 
Dropwizard and Groovy
Dropwizard and GroovyDropwizard and Groovy
Dropwizard and Groovytomaslin
 
Gradle enabled android project
Gradle enabled android projectGradle enabled android project
Gradle enabled android projectShaka Huang
 
Introduction to Grails 2013
Introduction to Grails 2013Introduction to Grails 2013
Introduction to Grails 2013Gavin Hogan
 
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx PluginGr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx PluginYasuharu Nakano
 
SOLID Programming with Portable Class Libraries
SOLID Programming with Portable Class LibrariesSOLID Programming with Portable Class Libraries
SOLID Programming with Portable Class LibrariesVagif Abilov
 
intoduction to Grails Framework
intoduction to Grails Frameworkintoduction to Grails Framework
intoduction to Grails FrameworkHarshdeep Kaur
 
Difference between java and c#
Difference between java and c#Difference between java and c#
Difference between java and c#TECOS
 
Using Javascript in today's world
Using Javascript in today's worldUsing Javascript in today's world
Using Javascript in today's worldSudar Muthu
 
Write microservice in golang
Write microservice in golangWrite microservice in golang
Write microservice in golangBo-Yi Wu
 
#1 - HTML5 Overview
#1 - HTML5 Overview#1 - HTML5 Overview
#1 - HTML5 Overviewiloveigloo
 
TypeScript - Javascript done right
TypeScript - Javascript done rightTypeScript - Javascript done right
TypeScript - Javascript done rightWekoslav Stefanovski
 
BYOP: Custom Processor Development with Apache NiFi
BYOP: Custom Processor Development with Apache NiFiBYOP: Custom Processor Development with Apache NiFi
BYOP: Custom Processor Development with Apache NiFiDataWorks Summit
 
Apache Drill (ver. 0.1, check ver. 0.2)
Apache Drill (ver. 0.1, check ver. 0.2)Apache Drill (ver. 0.1, check ver. 0.2)
Apache Drill (ver. 0.1, check ver. 0.2)Camuel Gilyadov
 

Similar to getting-your-groovy-on (20)

Groovy In the Cloud
Groovy In the CloudGroovy In the Cloud
Groovy In the Cloud
 
ASP.NET MVC Workshop for Women in Technology
ASP.NET MVC Workshop for Women in TechnologyASP.NET MVC Workshop for Women in Technology
ASP.NET MVC Workshop for Women in Technology
 
DevOps-Roadmap
DevOps-RoadmapDevOps-Roadmap
DevOps-Roadmap
 
MvvmCross Introduction
MvvmCross IntroductionMvvmCross Introduction
MvvmCross Introduction
 
MvvmCross Seminar
MvvmCross SeminarMvvmCross Seminar
MvvmCross Seminar
 
Dropwizard and Groovy
Dropwizard and GroovyDropwizard and Groovy
Dropwizard and Groovy
 
Gradle enabled android project
Gradle enabled android projectGradle enabled android project
Gradle enabled android project
 
Introduction to Grails 2013
Introduction to Grails 2013Introduction to Grails 2013
Introduction to Grails 2013
 
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx PluginGr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
Gr8conf EU 2013 Speed up your development: GroovyServ and Grails Improx Plugin
 
Gitlab ci-cd
Gitlab ci-cdGitlab ci-cd
Gitlab ci-cd
 
SOLID Programming with Portable Class Libraries
SOLID Programming with Portable Class LibrariesSOLID Programming with Portable Class Libraries
SOLID Programming with Portable Class Libraries
 
intoduction to Grails Framework
intoduction to Grails Frameworkintoduction to Grails Framework
intoduction to Grails Framework
 
Difference between java and c#
Difference between java and c#Difference between java and c#
Difference between java and c#
 
Using Javascript in today's world
Using Javascript in today's worldUsing Javascript in today's world
Using Javascript in today's world
 
Grooscript greach
Grooscript greachGrooscript greach
Grooscript greach
 
Write microservice in golang
Write microservice in golangWrite microservice in golang
Write microservice in golang
 
#1 - HTML5 Overview
#1 - HTML5 Overview#1 - HTML5 Overview
#1 - HTML5 Overview
 
TypeScript - Javascript done right
TypeScript - Javascript done rightTypeScript - Javascript done right
TypeScript - Javascript done right
 
BYOP: Custom Processor Development with Apache NiFi
BYOP: Custom Processor Development with Apache NiFiBYOP: Custom Processor Development with Apache NiFi
BYOP: Custom Processor Development with Apache NiFi
 
Apache Drill (ver. 0.1, check ver. 0.2)
Apache Drill (ver. 0.1, check ver. 0.2)Apache Drill (ver. 0.1, check ver. 0.2)
Apache Drill (ver. 0.1, check ver. 0.2)
 

getting-your-groovy-on