Introduction to Groovy - Intertech

peruvianwageslaveInternet και Εφαρμογές Web

5 Φεβ 2013 (πριν από 4 χρόνια και 2 μήνες)

150 εμφανίσεις

An
Intertech
Course

Introduction to Groovy

Intertech

Open House

May 2012

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
2

Agenda


Java vs. Groovy (and yes <sigh> Hello World in Groovy)


What is Groovy?


Why Groovy?


Potential Issues with Groovy


Groovy Features (not found in Java)


What you need to do Groovy


Feature Overview (Manager warning


some code here!)


MOP


the secret sauce of Groovy


Groovy Ecosystem


How to Proceed with Groovy


Resource List and Q&A.

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
3

Who is this guy?


Jim White


jwhite@intertech.com


Intertech Instructor & Director of Training


Author
J2ME, Java in Small Things



2002, Manning


International Speaker


Including JavaOne


Contributor to many journals including:


JDJ


DevX.com


JavaPro


Consultant, engineer and architect with several companies


AND… a Groovy newbie!


Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
4

Audience check, warnings, caveats, etc.


How many of you are already Groovy developers?


Probably not much in this talk for you


sorry. But you can help keep me honest.


How many of you have experimented with Groovy?


May be a little in it for you.


It’s an hour long talk


that’s not much time to introduce a whole language.


How many of you have a Java background but are not familiar with Groovy
yet?


Perfect
!


If you know Java and are wondering what Groovy is


this talk is on the mark.


How many of you manage Java development teams?


Also
perfect
!


If you are a manager of Java development teams and want to know if Groovy can
add something to the team


this talk is for you (minus some of the coding).


How many of you don’t know Java?


Ouch!


this may hurt a bit, but hopefully you’ll get a little bit from it.


An
Intertech
Course

Examples of Groovy at a High Level

How does it compare to Java?

"Always program as if the person who will be maintaining your program is a
violent psychopath that knows where you live
.“



~Martin
Golding

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
6

Groovy v. Java
-

a first look


What if we had to complete a
small task.


How much code would it take
to…


Create
a list of people


Sort
the list by last, first name


Select
the people older than
50
from that list


Write these people to a file
(CSV format)



Java


LOC: 319


Time to develop: ~2 hours


Execution time: ~30ms


From somebody that has ~14
years of Java experience



Groovy


LOC: 115


Time to develop: ~1 hour


Execution time: ~700ms


From somebody that has < 1
month of Groovy experience

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
7

For those that really want Hello World in Groovy…

println

‘Hello World’





Satisfied!?

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
8

What is Groovy?


It was created in 2004.


by James Strachan and Bob
McWhirter

(
Codehaus
)


Groovy 1.0 released 2007


Current release: 1.8 (version 2.0 in the works)


It is an alternate language for the JVM.


But as a companion to Java vs. a replacement for Java.


Brings the power of languages like Ruby, Smalltalk, Python,... to the JVM.


Keep the elegance, robustness, support of the Java syntax and platform.


Groovy is the second language under standardization for the Java
platform.


What Java 2.0 would look like if we had the chance (??).


It is a dynamic programming language.


But what does dynamic mean? (more on that in a bit).


Optionally typed (not dynamically typed
-

more on this in a bit
).



Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
9

JVM

Groovy and Java are Symbiotic


Groovy runs in the JVM.


Groovy code can be compiled into a “normal” Java .class files.


Groovy code can call on Java classes.


Groovy classes can be called on by Java classes.


Every Groovy type is a subtype of
java.lang.Object
.


Groovy classes can extend Java classes & implement Java interfaces.


And vice versa!


Groovy is syntactically aligned with Java.


But can also be more compact and precise .


The
only language besides Java that fully


supports generics
and
annotations.





Groovy

Java

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
10

Cross Language Integration Ladder


A measure of languages
integration capability


Groovy goes all the way to the top











From
Groovy in Action


An
Intertech
Course

Why Groovy

A Case for Using Groovy

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
12

Why Groovy?


Productivity


More concise and meaningful code (vs. Java)


Feature set (see next slide)


Use as much or as little as you like with your Java applications


All it takes is an additional JAR file in the
classpath


Dynamic nature (which adds to productivity)


Optional typing


Add operators


Methods as objects (“
Metaprogramming
”, Closures, …)


Community driven, corporate backing


JSR
-
241


SpringSource
/VMware



Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
13

Groovy Features (not found in Java)


Closures


Collection literals


Properties (
GroovyBeans

or POGO)


Metaprogramming


Multi
-
line strings


String interpolation


Mixins
/Categories


Named arguments


Default
arguments


Everything’s an Object (even numbers)


Operator overloading


GPath

expressions


Additional
operators


And much
much

more…

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
14

Falsities


Groovy is just for scripting.


It can be used for scripting (and it is powerful in that role) .


But it can also do a lot more.


Groovy is all about closures. “It’s a functional programming language.”


Groovy borrows from functional programming languages (like LISP or
Clojure
).


Groovy ≠ functional programming language


Groovy uses closures (a lot)


it’s
a

language feature not
the

language feature.


Is Java all about code blocks?


Groovy is good if you do TDD


While this statement is true, it wouldn’t be the only reason to use Groovy.


Groovy allows unit tests to be a first class citizens of your code.


But it no more requires them than does Java.


Use Groovy only if you want to use Grails.


Grails is a powerful Web development framework.


But Groovy has a lot to offer everyday Java developers.


Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
15

Groovy Issues


Groovy can be slow(
er
).


Groovy 1.8.x
is 100+ times faster than Groovy 1.0 at Fibonacci computing test.


Only 12% slower than Java at the same task.


See wiki.jvmlangsummit.com/images/0/04/Theodorou
-
Faster
-
Groovy
-
1.8.pdf


Better caching, optimizations, etc.


Groovy may need lots of memory.


MetaClass

in particular needs lots of memory to cache.


Groovy startup time needs improvement.


New syntax, default typing, closures, … takes sometime (a lot?) to get use to.


Use as much as you like


Remember most of regular Java works in Groovy


Documentation is thin but improving


And you have to have Java docs and Groovy docs.


Knowledge of Java is imperative
.


An
Intertech
Course

Doin
’ Groovy

Groovy Tools and Setup

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
17

What you need to do Groovy


Install Java 1.4 or better.


JAVA_HOME environment
variable set to the location of your JDK installation
.


Download latest Groovy (“GDK”)


From
groovy.codehaus.org/Download


Either in ZIP or installer form.


Optionally download the documentation.


Set
an environment variable GROOVY_HOME


To the
location where you unzipped the distribution zip
file.


Include
GROOVY_HOME/bin in your
PATH.


Optionally, a Groovy IDE is nice.


Get the Eclipse plugin (groovy.codehaus.org/
Eclipse+Plugin
)


Point your Eclipse IDE to
http://dist.springsource.org/release/GRECLIPSE/e3.7/



Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
18

Groovy Tools


groovysh


command
-
line shell


Used
to execute Groovy code
interactively.


Allows you to enter statements
or whole
scripts.


There are executed
“on the
fly.”


Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
19

Groovy Tools (continued)


groovyconsole


Swing
interface that acts as a minimal Groovy development
editor.


Execute
Groovy code
interactively.


It can load and run
Groovy script files.

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
20

Groovy Tools (continued)


groovy


The
processor that executes Groovy
programs and scripts.


It can even be used to test simple Groovy expressions

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
21

Groovy Tools (continued)


groovyc


Examples so far show Groovy running in “direct mode.”


Code is not interpreted, but no executable files are created.


The .class is generated and kept in memory before executing.


groovyc

produces “normal” JVM
bytecode

.class files.

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
22

Running Groovy in Java


Yes you can run a
compiled Groovy
class/script in Java.


Identical
to running a compiled Java
program.


Just add the
embeddable groovy
-
all
-
*.jar file
to your
JVM's
classpath
.


It provides all
of
Groovy's

third
-
party
dependencies.


An
Intertech
Course

Some Groovy Features

What I can show in 30 minutes

(or whatever time is left)

“Programming
is like sex. One mistake and you have to support it for the rest
of your life
.”



~
Michael
Sinz


Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
24

Groovy Basics


Groovy classes and/or scripts are usually stored in .groovy files.


They don’t have to be, but this is the convention.


It’s a convention that provides some conveniences.


Not everything has to be a class (or in a class).


Scripts
contain Groovy statements without an enclosing class declaration
.


Scripts
can even contain method definitions outside of class definitions to better
structure the code
.


Access modifiers are optional.


Everything is public by default.


Imports are optional.


Exception handling optional.



Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
25

Groovy Basics (continued)


Many “normal” Java syntax structures are often optional; such as…


Semicolons ending lines of code.


Parenthesis for method calls.


Return statements.


Usually, you only add
them for
clarity/readability.


Everything is an object (descending from
java.lang.Object
).


Groovy determines the type by default if you aren’t explicit.


Example:

def

i

= 4


i

will be of type
java.lang.Integer


There are 3 types of “strings” in Groovy


Makes dealing with regex and backslash strings (URL, files, etc.) much easier


Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
26

Groovy Script


Class structure not required


Put this code in a .groovy file
just the same.


Execute it with Groovy tools or
JVM directly

def

fileName

=
/c:
\
groovytests
\
4score.txt/

def

address =
new

File(
"$
fileName
"
)

if

(
address.exists
()){


address.
eachLine

{line
-
>


println

line.toUpperCase
()


}

}
else


println

'File does not exist'

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
27

Java Class

public

class

Address {


private

int

houseNumber
;


private

String
street
;


private

String
city
;


private

String
state
;


private

int

zip
;


public

int

getHouseNumber
() {


return

houseNumber
;


}


public

void

setHouseNumber
(
int

houseNumber
) {


this
.
houseNumber

=
houseNumber
;


}


public

String
getStreet
() {


return

street
;


}


public

void

setStreet
(String street) {


this
.
street

= street;


}


public

String
getCity
() {


return

city
;


}



public

void

setCity
(String city) {


this
.
city

= city;


}


public

String
getState
() {


return

state
;


}


public

void

setState
(String state) {


this
.
state

= state;


}


public

int

getZip
() {


return

zip
;


}


public

void

setZip
(
int

zip) {


this
.
zip

= zip;


}


public

String
toString
() {


return

houseNumber

+
" "

+
street

+
"
\
n"

+



city

+
", "

+
state

+
"

+
zip
;


}

}


Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
28

Groovy Class

public

class

Address {


private

int

houseNumber
;


private

String
street
;


private

String
city
;


private

String
state
;


private

int

zip
;


public

int

getHouseNumber
() {


return

houseNumber
;


}


public

void

setHouseNumber
(
int

houseNumber
) {


this
.
houseNumber

=
houseNumber
;


}


public

String
getStreet
() {


return

street
;


}


public

void

setStreet
(String street) {


this
.
street

= street;


}


public

String
getCity
() {


return

city
;


}



public

void

setCity
(String city) {


this
.
city

= city;


}


public

String
getState
() {


return

state
;


}


public

void

setState
(String state) {


this
.
state

= state;


}


public

int

getZip
() {


return

zip
;


}


public

void

setZip
(
int

zip) {


this
.
zip

= zip;


}


public

String
toString
() {


return

houseNumber

+
" "

+
street

+
"
\
n"

+


city

+
", "

+
state

+
" “
+
zip
;


}

}


Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
29

A Better Groovy Class

class

Address {


private

int

houseNumber


private

String
street


private

String
city


private

String
state


private

int

zip


int

getHouseNumber
() {


houseNumber


}


void

setHouseNumber
(
int

houseNumber
) {


this
.
houseNumber

=
houseNumber


}


String
getStreet
() {


street


}


void

setStreet
(String street) {


this
.
street

=
street


}


String
getCity
() {


city


}



void

setCity
(String city) {


this
.
city

=
city


}


String
getState
() {


state


}


void

setState
(String state) {


this
.
state

=
state


}


int

getZip
() {


zip


}


void

setZip
(
int

zip) {


this
.
zip

=
zip


}


String
toString
() {


houseNumber

+
" "

+
street

+
"
\
n"

+


city

+
", "

+
state

+
" “
+
zip


}

}


Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
30

A Real Groovy Class

import

groovy.transform.ToString
;


@
ToString

class

Address {


int

houseNumber


String
street


String
city


String
state


int

zip

}

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
31

GroovyBeans


JavaBean defined in Groovy.


Ordinary JavaBeans with exposed
properties.


Similar to .NET properties


The getters/setters are
autogenerated
.


For public/protected fields.


Purpose: simplify and make code
more concise.



import

groovy.transform.ToString

@
ToString

class

Customer {


String
first


String
last


Date
dob


int

children

}


def

c =
new

Customer()

c.first

=
'Jim'

c.last

=
'White'

c.dob

=
new

Date()

println

c


Customer(Jim
, White, Fri May 25
15:14:07 CDT 2012, 0)

A more Groovy way


def

c =
new

Customer(
first
:
'Jim
'
,
last
:
'White'
,
dob
:
new

Date())

println

c


Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
32

Optional Typing (not dynamic)


It’s not dynamic typing!


Groovy types are NOT
DYNAMIC


They
NEVER
CHANGE


Groovy is type
-
safe at runtime


Groovy offers
explicit typing.


Just
as you do in Java
.



def
” is used to indicate no type is
specified.


“Groovy, you figure it out at runtime.”


Groovy
infers
type
by
the value.


Where it can’t infer, it uses
java.lang.Object
.

int

x =
5
;
// explicit
typing

def

y =
5
;
// implicit typing to
java.lang.Integer


Integer z =
5
;

z =
new

Object();
//
GroovyCastException

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
33

Ranges


Ranges are objects in Groovy.


They represent a sequence of
values with upper and lower
bound.


Use .. between bounds


Optional < for right bound (half
exclusive range)

class

Person {


int

age


Person(
int

age) {


this
.
age

= age


}

}


def

ageRange

=
0
..
120

def

p =
new

Person(
30
)

assert

p.
age

in

ageRange


def

today =
new

Date();

def

nextWeek

= today +
7

def

thisWeek

= today..
nextWeek

assert

(today
-

1

in

thisWeek
) ==
false

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
34

List/Maps


The array syntax can be used to
define Lists and Maps.


Maps are expressed as List with “:”
separating keys/values

def

myMap

= [1:'Jim', 2:'Kelly']


Allows indexing out of current
bounds.


All sorts of new methods and
operators.

def

mishmash = [
'a'
,
42
,
new

Date(),
5.5f
,
true
]


System.
out
.println
(
mishmash.
getClass
())

for

(Object
obj

: mishmash) {


System.
out
.println
(
obj.getClass
())

}


assert

5.5f

in

mishmash


class
java.util.ArrayList

class
java.lang.String

class
java.lang.Integer

class
java.util.Date

class
java.lang.Float

class
java.lang.Boolean


More Groovy way


def

mishmash = [
'a'
,
42
,
new

Date(),
5.5f
,
true
]

println

mishmash.
class

mishmash.
each

{
println

it
.class
}


assert

5.5f

in

mishmash


Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
35

Closures


A piece of code wrapped as a object.


Acts like a method.


Takes parameters.


Returns a value.


You can pass a reference to it around, just like an object.


Recognized
as a list of statements within curly
braces


Like a Java code block.


It
optionally has a list of identifiers in order to name the parameters
passed to
it.


An arrow (
-
>) marking
the end of the
parameter list.


If the
closure needs
a
single
parameter, “it” is the default parameter name


So you
don't need to declare it specifically
before
-
>


BTW: closures are coming to Java


Java 8

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
36

Closures (continued)


Closure is a type in Groovy.


Syntax

{ <
params
>
-
>



//code block

}

def

myClosure

=

{a, b, c
-
>


println

a


println

b


println

c


def

x = a + b + c


println

x

}


myClosure
(
3
,
6
,
8
)



3

6

8

17

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
37

Closures (continued)


There are many closures already
built in.


Remember, “it” refers to the single
parameter by default.


Some methods and closures take
closures as parameters!


Like
eachLine

here.

new

File(
'myfile.txt'
).
eachLine

{
println

it

}

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
38

Operator Overloading/Overriding


In Groovy, operators are assigned
to methods.


Examples








To overload the operator for your
object…


just provide the appropriate
method in your class.


@
ToString

class

Address {


int

houseNumber


String
street


String
city


String
state


int

zip



void

plus(
int

i
){


houseNumber

=
houseNumber

+
i


}

}


def

a =
new

Address(
city
:
'Portville
'
,

state
:
'NY'
,
zip
:
14770
,
street
:
'Elm'
,
houseNumber
:
17
)

a+
2

println

a



Address(19
, Elm, Portville, NY, 14770)

Operator

Method

a+b

a.plus
(b)

a
-
b

a.minus
(b)

a<==>b

a.compareTo
(b)

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
39

For Loops


Did Java’s for each loop ever make
sense?

ArrayList

list =
new

ArrayList
();

for

(Object
obj

: list) {


// body

}



Groovy attempts to simplify.

for

(
variable

in

iterable

)

{




//

body


}


// in Java

List<String
> list =
new

ArrayList
<String>();

list.add
(
Jim
"
);

list.add
(
"Kelly"
);

list.add
(
"Seamus"
);

StringBuffer

buffer;

for

(String
str

: list) {


buffer
=
new

StringBuffer
(
str
);


System.
out
.println
(
buffer.reverse
());

}


// in Groovy

def

list =[
'
Jim'
,
'Kelly'
,
'Seamus
'
]

for

(
ea

in

list){


println

ea.
reverse
();

}


miJ

ylleK

sumaeS

Actually a more Groovy way wouldn’t use for

(with Closure)


def

list =[
'
Jim'
,
'Kelly'
,
'Seamus
'
]

list.
each

{
println

it
.reverse
()}


Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
40

Multiple String Literals


Groovy has many string literal types.


Single quoted
: same as Java string
literals.


Double quoted
: a
GString


Unescaped

$ placeholders are replaced.


Triple quoted
: multiline string literal.


Whitespace is preserved.


Placeholders are replaced.



Slashy


strings: allows strings with
backslashes.


Placeholders are replaced.


def

name =
'White'

String w =
'Jim $name'

String x =
"Jim $name"

String y =
"""
Jim

'is cool'

$name"""

String z =
/Jim
\
the man
\

$
name
/

println

w

println

x

println

y

println

z


Jim $name

Jim White

Jim

'is cool'

White

Jim
\
the man
\

White

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
41

Regex


Regular expressions are usually prominent in scripting languages.


And so they are too in Groovy.


Groovy relies on Java’s regex support.


Groovy has 3 operators to work with regex’s.


=~ find


==~ match


~
String
pattern


Usually used with
Slashy

strings because of ”
\
” in regex’s.

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
42

4score.txt

1.
Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated
to
the
proposition that all men are created equal
.


2.
Now we are engaged in a great civil war, testing whether that nation, or any nation, so conceived and so dedicated, can long
end
ure.


3.
We are met on a great battle
-
field of that war.


4.
We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nati
on
might live.


5.
It is altogether fitting and proper that we should do this
.


6.
But, in a larger sense, we can not dedicate, we can not consecrate, we can not hallow this ground.


7.
The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract.


8.
The world will little note, nor long remember what we say here, but it can never forget what they did here.


9.
It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobl
y a
dvanced.


10.
It is rather for us to be here dedicated to the great task remaining before us

that from these honored dead we take increased de
votion
to that cause for which they gave the last full measure of devotion

that we here highly resolve that these dead shall not have d
ied in
vain

that this nation, under God, shall have a new birth of freedom

and that government of the people, by the people, for the pe
ople,
shall not perish from the earth.


Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
43

Regex

def

fileName

=
/c:
\
groovytests
\
4score.txt/

def

address =
new

File(
"$
fileName
"
)

// count the number of "we" in each
line in GA

address.
eachLine

{line
-
>


java.util.regex.Matcher

matcher = line =~
/we/


println

matcher.
count

}



// fine lines containing the word "
nation“ in GA

address.
eachLine

{line
-
>



println

line ==~
/^.*nation.*$/


}



// find four letter word
in the food list

def

fourLetterWordPattern

= ~
/
\
w{4}/

def

myWords

=
[
'
beer'
,
'cheese'
,
'Coke'
,
'Pepsi
'
,

'pizza'
,
'meat'
,
'corn'
,
'
brocolli
'
]

println

myWords.
grep
(
fourLetterWordPattern
)

0

1

0

0

1

3

1

1

0

2

true

true

false

true

false

false

false

false

false

true

[beer, Coke, meat, corn]

An
Intertech
Course

Behind the Scenes

Groovy Architecture

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
45

The Meta Object Protocol (MOP)


MOP is the secret sauce of
Groovy’s

dynamic behavior.


Put simply,
whenever Groovy
calls a
method
it doesn't call it
directly.


An intermediate
layer
calls the method on its behalf.


The intermediate
layer provides hooks that allow
other behavior.


InvokerHelper.invokeMethod
(this, “
aMethod
”, {
parameterA
,
parameterB
,
…});


InvokerHelper.invokeMethod
(this,

println
”, {“Hello World”});


Groovy maintains a meta class (of type
MetaClass
) for every class loaded.


This meta class maintains the collection of all methods/properties in the
class.


Allows adding additional methods/props that Groovy knows about.


Allowing the invoker to call on either Java or Groovy methods/props.



Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
46

JVM

aMethod
(
‘Hello

World’,43)

Invoker

Helper

Meta
Class

Your Bean

An
Intertech
Course

Miscellaneous

Other Stuff to Know When Starting

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
48

Groovy Ecosystem


Grails


Web application framework (based on Rails)


Gradle


Advanced build automation based on Ant, Maven


Griffon


Inspired
by Grails,
an application
framework for developing desktop applications


Spock


Inspired by
JUnit
,
jMock
,
a testing
and specification framework


Gaelyk


A
lightweight Groovy toolkit for Google App Engine
Java


And many more


See http://groovy.codehaus.org/Modules

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
49

How to proceed with Groovy?


Get it! Explore it!


I think you’ll find it hard to deny the potential power.


Try it out for small needs


especially scripting, unit testing, etc.


Since it can be added on top of Java, allow developers to adopt slowly.


It can be an option, not a requirement.


Allow confidence in it to build/spread.


Work it into modules that make most sense.


I/O processing


XML processing


List/Map processing


Watch for potential performance issues.


If you like it…


Try it on a RAD prototype project


see if the productivity gains are real.


Expand to Grails, Griffon, etc. as needed.

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
50

Resources


Books


many out there, but you want one that covers 1.5 or better.


Prefer 1.8


Groovy in Action (2
nd

edition), Manning


in MEAP


Programming Groovy: Dynamic Productivity for the Java
Developer, Pragmatic
Programmers



good but a bit dated (2008)


Web sites


groovy.codehaus.org/Documentation


www.sauria.com/presentations/Groovy%20SeaJUG%202004
-
03.ppt.pdf


www.ibm.com/developerworks/java/tutorials/j
-
groovy/j
-
groovy
-
pdf.pdf


wiki.jvmlangsummit.com/images/0/04/Theodorou
-
Faster
-
Groovy
-
1.8.pdf

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
51

Summary


Groovy is a dynamic language that is symbiotic with Java.


Groovy improves developer productivity.


Groovy offers many features not found in Java to include: closures,
optional typing, operator overloading, syntax simplicity, …


To do Groovy you need Java, the GDK, and optionally a Groovy
-
aware
IDE.


The Meta Object Protocol provides Groovy its dynamic behavior.


Groovy has a healthy ecosystem. Frameworks for web development,
builds, testing, etc. exist.


There are potential issues
with
Groovy to include potential
performance issues.


You can begin slowly with Groovy. Add it where it makes sense,
provides assistance, and builds confidence.


Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
52

Thank You!

Jim White

Intertech, Inc.

651
-
288
-
7000

jwhite@intertech.com


Slides and code will be available on
our web site (also emailed to those
that signed up)

Course Name

Copyright © Intertech, Inc. • www.Intertech.com • 800
-
866
-
9884

Slide
53

Questions


What questions do you have about Groovy?