groovy fly through
DESCRIPTION
TRANSCRIPT
![Page 1: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/1.jpg)
Fly Through
![Page 3: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/3.jpg)
Trust me?
Used dynamic langs on the JVM since 2000.
Groovy since 2004/2007
![Page 4: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/4.jpg)
What?
A Dynamic Language for the JVM
![Page 5: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/5.jpg)
A modern language
Inspired by best features:
JavaRuby
PythonSmalltalk
...
![Page 6: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/6.jpg)
Java
The Premise
The Platform
![Page 7: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/7.jpg)
Exciting?
Modern GC
JIT
Puts hardware to good use(mem, multi-core, I/O)
![Page 8: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/8.jpg)
Java = Community
Well-established libraries (jdk, commons, ...).
Loads of opens source stuff.
![Page 9: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/9.jpg)
Evolution
Groovy extends Java.
Really. 100% semantically, 90% syntactically.
![Page 10: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/10.jpg)
CreativeTools
![Page 11: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/11.jpg)
What does it look like?
class First {
static void main(String[] args) { println "You just gave me " + args[0] }
}
![Page 12: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/12.jpg)
Shorter
println "You just gave me ${args[0]}"
![Page 13: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/13.jpg)
Many Variants
class Think {
String value
def getMeaning() { "You just gave me ${value}." }
static main(args) { def first = args? args[0] : "nothing" println new Think(value:first).meaning }
}
![Page 14: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/14.jpg)
Usages
Everywhere.
Groovy classes are real classes in Java.
![Page 15: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/15.jpg)
Cherry-pick
● Small scripts● API experimentation● Tools for batch and analysis● Tests, Specs● Building
![Page 16: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/16.jpg)
Entire applications
Seamlessly cross-compiles with Java.
Write any or all parts in Groovy, using any Java framework.
![Page 17: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/17.jpg)
Ceremonypublic class Item {
private String name; private String description;
public Item() {}
public String getName() { return name; }
public String setName(String name) { this.name = name; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
}
![Page 18: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/18.jpg)
Pragmatic
class Item { String name String description}
![Page 19: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/19.jpg)
Compact Properties
new Date().time // same as getTime()
![Page 20: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/20.jpg)
Dynamic
def v = "string.."v = 1
assert v instanceof String
![Page 21: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/21.jpg)
No cast needed
Just access methods and properties expected to be there.
![Page 22: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/22.jpg)
But..
Types have significance..
![Page 23: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/23.jpg)
Type Hybrid
Dynamic refs are declared with def.
Typed refs are auto-cast & converted:
String v = 123v.bytes[-1] == '3'
![Page 24: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/24.jpg)
GroovyCastException
Date dd = "hi!"
![Page 25: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/25.jpg)
Small Values of Cool
![Page 26: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/26.jpg)
Optional Parens
client.sendMessage "Message: ${msg}"
client.quit()
![Page 27: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/27.jpg)
Access else null
response?.entity
![Page 28: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/28.jpg)
Use all
files*.name
![Page 29: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/29.jpg)
Slice
def l = ['a', 'b', 'c', 'd']
assert l[1..-2] == ['b', 'c']
![Page 30: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/30.jpg)
Map Expandos
Map map = ['a': 1, b: 2]
assert map['a'] == 1assert map.b == 2
map.c = 3map['d'] = 3
assert map['c'] == map.d
![Page 31: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/31.jpg)
Closures
Code block literals.
Syntax:
def items = ["a", "b", "c", "def"]
items.eachWithIndex { item, i -> println "${i+1}: ${item}"}
![Page 32: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/32.jpg)
Sift and Transform
def values = ["1", "2", "12", "123"]
def some = values.findAll { it.size() > 1 }
def ints = some.collect { (it as int) * 2 }
assert ints == [24, 246]
![Page 33: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/33.jpg)
GDK
new File("urls.txt").eachLine {
def url = new URL(it)
def file = new File("./${url.path}")
file.parentFile.mkdirs()
file.bytes = url.bytes
}
![Page 34: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/34.jpg)
Xml...
def slurper = new XmlSlurper()def html = slurper.parse(new File("/some.xhtml"))
html.body.'*'.each { println it.'@id'}
![Page 35: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/35.jpg)
Spock
def "lists should be appendable"() { given: def a = []
when: a << 1
then: a == [1]}
![Page 36: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/36.jpg)
Mocks
EventHandler handler = Mock()
handler.handle(someAllowedEvent) >> true
handler.handle(_) >> false
![Page 37: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/37.jpg)
Data-driven
expect:a.toUpperCase() == bwhere: a | b"a" | "A""b" | "B""c" | "C""d" | "D"
![Page 38: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/38.jpg)
PowerAsserts
assert a == [1]
ERROR org.codehaus.groovy.tr...rt.PowerAssertionError:assert a == [1] | | | false []
![Page 39: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/39.jpg)
Environments
![Page 40: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/40.jpg)
Scripts
$ groovy setup_db.groovy
![Page 41: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/41.jpg)
REPL
$ groovyshgroovy:000> import org.apache.commons.io.*
![Page 42: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/42.jpg)
Groovy Console
![Page 43: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/43.jpg)
Building
Ant
Maven
Gradle
![Page 44: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/44.jpg)
Grab with Grape
@Grab('org.mortbay.jetty:jetty-embedded:6.1.0')import org.mortbay.jetty.Server
def server = new Server(8080)server.start()
![Page 45: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/45.jpg)
IDE:s
Amazing IntelliJ support.
Eclipse known to be good..
![Page 46: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/46.jpg)
The Power of Prototyping
"You can use Groovy asan exploratory languagefor functionality spikes."
![Page 47: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/47.jpg)
More Info
<http://groovy.codehaus.org/>
<http://spockframework.org/>
.. the web is full of groovy:
<http://groovyconsole.appspot.com/>
Groovy Style For the Java Guy
![Page 48: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/48.jpg)
Thank you!
![Page 49: Groovy Fly Through](https://reader033.vdocuments.site/reader033/viewer/2022051817/5484ce635806b5a4588b46a0/html5/thumbnails/49.jpg)
CC Images
Groovy-logo, "Groovy Duke"
"Tools""Shell""Tadpole"