More Related Content
Similar to getting-your-groovy-on
Similar to getting-your-groovy-on (20)
getting-your-groovy-on
- 6. 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
- 7. 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
- 11. 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);
- 14. 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
- 15. 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
- 16. 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
- 17. 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
- 19. 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