Upload
niklal
View
1.125
Download
0
Embed Size (px)
DESCRIPTION
Citation preview
Fly Through
Trust me?
Used dynamic langs on the JVM since 2000.
Groovy since 2004/2007
What?
A Dynamic Language for the JVM
A modern language
Inspired by best features:
JavaRuby
PythonSmalltalk
...
Java
The Premise
The Platform
Exciting?
Modern GC
JIT
Puts hardware to good use(mem, multi-core, I/O)
Java = Community
Well-established libraries (jdk, commons, ...).
Loads of opens source stuff.
Evolution
Groovy extends Java.
Really. 100% semantically, 90% syntactically.
CreativeTools
What does it look like?
class First {
static void main(String[] args) { println "You just gave me " + args[0] }
}
Shorter
println "You just gave me ${args[0]}"
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 }
}
Usages
Everywhere.
Groovy classes are real classes in Java.
Cherry-pick
● Small scripts● API experimentation● Tools for batch and analysis● Tests, Specs● Building
Entire applications
Seamlessly cross-compiles with Java.
Write any or all parts in Groovy, using any Java framework.
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; }
}
Pragmatic
class Item { String name String description}
Compact Properties
new Date().time // same as getTime()
Dynamic
def v = "string.."v = 1
assert v instanceof String
No cast needed
Just access methods and properties expected to be there.
But..
Types have significance..
Type Hybrid
Dynamic refs are declared with def.
Typed refs are auto-cast & converted:
String v = 123v.bytes[-1] == '3'
GroovyCastException
Date dd = "hi!"
Small Values of Cool
Optional Parens
client.sendMessage "Message: ${msg}"
client.quit()
Access else null
response?.entity
Use all
files*.name
Slice
def l = ['a', 'b', 'c', 'd']
assert l[1..-2] == ['b', 'c']
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
Closures
Code block literals.
Syntax:
def items = ["a", "b", "c", "def"]
items.eachWithIndex { item, i -> println "${i+1}: ${item}"}
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]
GDK
new File("urls.txt").eachLine {
def url = new URL(it)
def file = new File("./${url.path}")
file.parentFile.mkdirs()
file.bytes = url.bytes
}
Xml...
def slurper = new XmlSlurper()def html = slurper.parse(new File("/some.xhtml"))
html.body.'*'.each { println it.'@id'}
Spock
def "lists should be appendable"() { given: def a = []
when: a << 1
then: a == [1]}
Mocks
EventHandler handler = Mock()
handler.handle(someAllowedEvent) >> true
handler.handle(_) >> false
Data-driven
expect:a.toUpperCase() == bwhere: a | b"a" | "A""b" | "B""c" | "C""d" | "D"
PowerAsserts
assert a == [1]
ERROR org.codehaus.groovy.tr...rt.PowerAssertionError:assert a == [1] | | | false []
Environments
Scripts
$ groovy setup_db.groovy
REPL
$ groovyshgroovy:000> import org.apache.commons.io.*
Groovy Console
Building
Ant
Maven
Gradle
Grab with Grape
@Grab('org.mortbay.jetty:jetty-embedded:6.1.0')import org.mortbay.jetty.Server
def server = new Server(8080)server.start()
IDE:s
Amazing IntelliJ support.
Eclipse known to be good..
The Power of Prototyping
"You can use Groovy asan exploratory languagefor functionality spikes."
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
Thank you!
CC Images
Groovy-logo, "Groovy Duke"
"Tools""Shell""Tadpole"