Download - Groovy by Chetan Gosavi
![Page 1: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/1.jpg)
Introduction To GroovyFor Java Programmers
Jeff BrownPrincipal Software Engineer
Object Computing Inc.http://www.ociweb.com/[email protected]
![Page 2: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/2.jpg)
Our Sponsors
● Books To Give Away● Thanks To Manning And Apress
– http://manning.com/– http://apress.com/
![Page 3: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/3.jpg)
About Me
● Jeff Brown● Principal Engineer Object Computing Inc.● Software Engineering For 15 Years● Mostly Java For 10 Years● Java/OO Instructor For 7 Years● Grails Core Development Team Member● St. Louis Java SIG Steering Committee
Member
![Page 4: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/4.jpg)
What Is Groovy?
● Open Source● Agile Dynamic Language
● Others...● JavaScript● Ruby● Python
● Integrates Very Well With Java● Runs On The JVM● Call Groovy From Java● Call Java From Groovy● Leverage Powerful Existing Java Libraries
![Page 5: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/5.jpg)
Why Groovy?
● Familiar Syntax For Java Programmers● Leverage The Wealth Of Java Libraries● Easy Integration With Your Existing
Infrastructure● App Servers● Servlet Containers● Loads Of Databases With JDBC Drivers● All Your Homegrown Java Infrastructure
![Page 6: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/6.jpg)
Momentum
● In Recent Weeks...– Manning published Groovy In Action– Apress published The Definitive Guide To Grails– AboutGroovy.com went live– Groovy 1.0RC01 was released
![Page 7: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/7.jpg)
Installing Groovy
●Download Release– http://groovy.codehaus.org/
●Extract The Archive●Set GROOVY_HOME●Add $GROOVY_HOME/bin to PATH
![Page 8: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/8.jpg)
Hello Groovy
● Give It A Spin...
$ groovy versionGroovy Version: 1.0RC01 JVM: 1.4.2_13b06
$ groovy e " println 'Hello From Groovy' "Hello From Groovy
$ groovy e "a=10;b=4;c=a*b;println c"40
![Page 9: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/9.jpg)
Groovy Shell
$ groovyshLet's get Groovy!================Version: 1.0RC01 JVM: 1.4.2_13b06Type 'exit' to terminate the shellType 'help' for command helpType 'go' to execute the statements
groovy> width = 5groovy> height = 12groovy> area = width * heightgroovy> println areagroovy> go60
![Page 10: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/10.jpg)
Groovy Console
$ groovyConsole
![Page 11: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/11.jpg)
Groovy Scripts
// mygroovyscript.groovyprintln 'Hello From My Groovy Script'
groovy mygroovyscript.groovyHello From My Groovy Script
![Page 12: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/12.jpg)
Groovy Classes
// MyGroovyTest.groovyclass MyGroovyTest { def sayHello() {
println 'Hello From MyGroovyTest' } static void main(args) { def mgt = new MyGroovyTest() mgt.sayHello() }}
groovy MyGroovyTest.groovyHello From MyGroovyTest
![Page 13: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/13.jpg)
groovyc
● groovyc Compiles Groovy To Bytecode● Compiled Code Runs As Normal Java Code● CLASSPATH
– groovyall[version].jar– in $GROOVY_HOME/embeddable/
groovyc MyGroovyTest.groovyjava MyGroovyTestHello From MyGroovyTest
![Page 14: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/14.jpg)
Some Language Basics
● Everything Is An Object● GString● Closures● Collections● Categories● Ranges● Groovy Beans● Builders● Meta Programming
The following slides are a bunch of code snippets run in groovysh and groovyConsole to accompany live demo and discussion.
![Page 15: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/15.jpg)
Everything Is An Object
![Page 16: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/16.jpg)
Groovy Strings
● Known As GStrings● GStrings Are Surrounded By Double Quotes
– single quotes are used for regular strings● May Contain Groovy Expressions
– expressions surrounded by ${ }– evaluated and substitution takes place at runtime
● Square Bracket Syntax My Be Applied– like charAt(i)– more complicated range related example later
![Page 17: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/17.jpg)
Groovy Strings
![Page 18: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/18.jpg)
Groovy Strings
![Page 19: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/19.jpg)
Closures
● A Block Of Code● May Be Passed Around● May Accept Arguments● Always Return Something
– not necessarily explicitly● More Flexible Than Anonymous Inner Class
![Page 20: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/20.jpg)
Closures
● Groovy Adds A times Method to Number● The times Method Accepts A Closure
groovy> 3.times { println 'Hello' }groovy> goHelloHelloHello
![Page 21: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/21.jpg)
Closures
● Closures Are First Class Objects● References May Point To Closures
groovy> cl = { println 'Closures Are Cool' }groovy> 3.times clgroovy> goClosures Are CoolClosures Are CoolClosures Are Cool
![Page 22: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/22.jpg)
Closures
● Closures May Accept Arguments● The times Method Passes An Argument To
The Closure
groovy> 3.times { index > println "index is ${index}" }groovy> goindex is 0index is 1index is 2
![Page 23: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/23.jpg)
Closures
● Closures Have An Implicit “it” Argument
groovy> 3.times { println "index is ${it}" }groovy> goindex is 0index is 1index is 2
![Page 24: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/24.jpg)
Closures
● Multiple Arguments
groovy> myMap = [name:'Jeff', location:'St. Louis']groovy> myMap.each { key, value > println "${key} is ${value}" }groovy> golocation is St. Louisname is Jeff
![Page 25: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/25.jpg)
Collections
● Lists Are Simple To Declare
groovy> kids = ['Zack', 'Jake']groovy> println kids.classgroovy> goclass java.util.ArrayList
![Page 26: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/26.jpg)
Collections
● Adding To A List
groovy> albums = ['Rush']groovy> albums << 'Fly By Night'groovy> albums += 'Caress Of Steel'groovy> albums.add '2112'groovy> println albumsgroovy> go["Rush", "Fly By Night", "Caress Of Steel", "2112"]
![Page 27: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/27.jpg)
Collections
● Removing From A List
groovy> instruments = ['drums', 'keyboards', 'guitars', 'lutes']groovy> instruments.remove 'keyboards'groovy> instruments = 'lutes'groovy> println instrumentsgroovy> go["drums", "guitars"]
![Page 28: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/28.jpg)
Collections
● Closure To Iterate Over A List
groovy> tracks = ['Custard Pie', 'The Rover', 'In My Time Of Dying']groovy> tracks.each { println it }groovy> goCustard PieThe RoverIn My Time Of Dying
![Page 29: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/29.jpg)
Collections
● Iterating With An Index
groovy> tracks = ['Custard Pie', 'The Rover', 'In My Time Of Dying']groovy> tracks.eachWithIndex { track, index > println "Track ${index + 1}: ${track}" }groovy> goTrack 1: Custard PieTrack 2: The RoverTrack 3: In My Time Of Dying
![Page 30: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/30.jpg)
Collections
● Map Manipulation
groovy> myMap = [bass:'Geezer', drums:'Bill']groovy> myMap['vocals'] = 'Ozzy'groovy> myMap.guitar = 'Tony'groovy> println myMapgroovy> go["drums":"Bill", "vocals":"Ozzy", "bass":"Geezer", "guitar":"Tony"]
![Page 31: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/31.jpg)
Groovy Categories
![Page 32: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/32.jpg)
Groovy Categories
![Page 33: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/33.jpg)
Groovy Categories
![Page 34: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/34.jpg)
Ranges
![Page 35: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/35.jpg)
Ranges
![Page 36: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/36.jpg)
Ranges
![Page 37: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/37.jpg)
Groovy Beans
● Groovy Beans Are POGOs● Similar To POJOs● Boilerplate Code Is Eliminated
![Page 38: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/38.jpg)
POJO
// Person.javapublic class Person {
private String firstName;private String lastName;
public Person() {}
public Person(String firstName, String lastName) {this.firstName = firstName;this.lastName = lastName;
}
![Page 39: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/39.jpg)
POJO
public String getFirstName() {return firstName;
}
public void setFirstName(String firstName) {this.firstName = firstName;
}
public String getLastName() {return lastName;
}
public void setLastName(String lastName) {this.lastName = lastName;
}}
![Page 40: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/40.jpg)
POJO
● I Wrote Code To Declare The Fields● I Let Eclipse Generate Constructors● I Let Eclipse Generate Getters/Setters
If all of that code can be generated by the IDE, why can't it be generated by the compiler or the runtime environment?
![Page 41: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/41.jpg)
Groovy Beans
● No Need To Write Constructors● No Need To Write Getters/Setters● May Declare Types – Don't Need To
// BaseballTeam.groovyclass BaseballTeam {
def cityNamedef teamName
}
![Page 42: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/42.jpg)
Groovy Beans
cardinals = new BaseballTeam(cityName:'St. Louis', teamName:'Cardinals')
println "City Name: ${cardinals.cityName}"println "Team Name: ${cardinals.teamName}"
City Name: St. LouisTeam Name: Cardinals
![Page 43: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/43.jpg)
Groovy Beans
● Property Access Looks Like Field Access– name = cardinals.teamName– name = cardinals.getTeamName()
● Assignment Works The Same Way– cardinals.teamName = 'Saint Louis'– cardinals.setTeamName('Saint Louis')
![Page 44: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/44.jpg)
Groovy Beans
● Properties Aren't Necessarily Declared As Fields
class BaseballTeam {
def cityNamedef teamName
def getDisplayName () {"${cityName} ${teamName}"
}
}
![Page 45: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/45.jpg)
Groovy Beans
cardinals = new BaseballTeam(cityName:'St. Louis', teamName:'Cardinals')println cardinals.displayName
St. Louis Cardinals
![Page 46: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/46.jpg)
Groovy Beans
● Properties Are Public By Default– private field– public getter/setter
● Properties May Be Private Or Protected● No 'package' Level
![Page 47: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/47.jpg)
MarkupBuilder
builder = new groovy.xml.MarkupBuilder()builder.baseball { league(name:"National") { team("Cardinals") team("Cubs") team("Mets") } league(name:"American") { team("Angels") team("Yankees") team("Royals") }}
![Page 48: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/48.jpg)
MarkupBuilder
<baseball> <league name='National'> <team>Cardinals</team> <team>Cubs</team> <team>Mets</team> </league> <league name='American'> <team>Angels</team> <team>Yankees</team> <team>Royals</team> </league></baseball>
![Page 49: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/49.jpg)
MarkupBuilder
builder = new groovy.xml.MarkupBuilder()builder.html() { head() { title('Markup Builder Demo') } body { h1('Bands') ul { li('Rush') li('King Crimson') li('Opeth') } }}
![Page 50: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/50.jpg)
MarkupBuilder
<html> <head> <title>Markup Builder Demo</title> </head> <body> <h1>Bands</h1> <ul> <li>Rush</li> <li>King Crimson</li> <li>Opeth</li> </ul> </body></html>
![Page 51: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/51.jpg)
Meta Programming
class MyGroovyThing { Object invokeMethod(String name, Object args) { // do something cool }}
Live Meta Programming Demo...
![Page 52: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/52.jpg)
Calling Groovy From Java
● GroovyShell● GroovyClassLoader● Bean Scripting Framework● Java 6 (and beyond...)
Live Demo...
![Page 53: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/53.jpg)
Links
● Main Groovy Site– http://groovy.codehaus.org/
● Main Grails Site– http://grails.org/
● Groovy Portal– http://aboutgroovy.com/
● My Blog– http://javajeff.blogspot.com/
● Java News Brief– http://www.ociweb.com/jnb/
![Page 54: Groovy by Chetan Gosavi](https://reader033.vdocument.in/reader033/viewer/2022051019/5695d2651a28ab9b029a456c/html5/thumbnails/54.jpg)
The End
Thank You For Coming!
Q&A