49
Fly Through

Groovy Fly Through

  • Upload
    niklal

  • View
    1.125

  • Download
    0

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Groovy Fly Through

Fly Through

Page 3: Groovy Fly Through

Trust me?

Used dynamic langs on the JVM since 2000.

Groovy since 2004/2007

Page 4: Groovy Fly Through

What?

A Dynamic Language for the JVM

Page 5: Groovy Fly Through

A modern language

Inspired by best features:

JavaRuby

PythonSmalltalk

...

Page 6: Groovy Fly Through

Java

The Premise

The Platform

Page 7: Groovy Fly Through

Exciting?

Modern GC

JIT

Puts hardware to good use(mem, multi-core, I/O)

Page 8: Groovy Fly Through

Java = Community

Well-established libraries (jdk, commons, ...).

Loads of opens source stuff.

Page 9: Groovy Fly Through

Evolution

Groovy extends Java.

Really. 100% semantically, 90% syntactically.

Page 10: Groovy Fly Through

CreativeTools

Page 11: Groovy Fly Through

What does it look like?

class First {

static void main(String[] args) { println "You just gave me " + args[0] }

}

Page 12: Groovy Fly Through

Shorter

println "You just gave me ${args[0]}"

Page 13: Groovy Fly Through

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

Usages

Everywhere.

Groovy classes are real classes in Java.

Page 15: Groovy Fly Through

Cherry-pick

● Small scripts● API experimentation● Tools for batch and analysis● Tests, Specs● Building

Page 16: Groovy Fly Through

Entire applications

Seamlessly cross-compiles with Java.

Write any or all parts in Groovy, using any Java framework.

Page 17: Groovy Fly Through

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

Pragmatic

class Item { String name String description}

Page 19: Groovy Fly Through

Compact Properties

new Date().time // same as getTime()

Page 20: Groovy Fly Through

Dynamic

def v = "string.."v = 1

assert v instanceof String

Page 21: Groovy Fly Through

No cast needed

Just access methods and properties expected to be there.

Page 22: Groovy Fly Through

But..

Types have significance..

Page 23: Groovy Fly Through

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

GroovyCastException

Date dd = "hi!"

Page 25: Groovy Fly Through

Small Values of Cool

Page 26: Groovy Fly Through

Optional Parens

client.sendMessage "Message: ${msg}"

client.quit()

Page 27: Groovy Fly Through

Access else null

response?.entity

Page 28: Groovy Fly Through

Use all

files*.name

Page 29: Groovy Fly Through

Slice

def l = ['a', 'b', 'c', 'd']

assert l[1..-2] == ['b', 'c']

Page 30: Groovy Fly Through

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

Closures

Code block literals.

Syntax:

def items = ["a", "b", "c", "def"]

items.eachWithIndex { item, i -> println "${i+1}: ${item}"}

Page 32: Groovy Fly Through

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

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

Xml...

def slurper = new XmlSlurper()def html = slurper.parse(new File("/some.xhtml"))

html.body.'*'.each { println it.'@id'}

Page 35: Groovy Fly Through

Spock

def "lists should be appendable"() { given: def a = []

when: a << 1

then: a == [1]}

Page 36: Groovy Fly Through

Mocks

EventHandler handler = Mock()

handler.handle(someAllowedEvent) >> true

handler.handle(_) >> false

Page 37: Groovy Fly Through

Data-driven

expect:a.toUpperCase() == bwhere: a | b"a" | "A""b" | "B""c" | "C""d" | "D"

Page 38: Groovy Fly Through

PowerAsserts

assert a == [1]

ERROR org.codehaus.groovy.tr...rt.PowerAssertionError:assert a == [1] | | | false []

Page 39: Groovy Fly Through

Environments

Page 40: Groovy Fly Through

Scripts

$ groovy setup_db.groovy

Page 41: Groovy Fly Through

REPL

$ groovyshgroovy:000> import org.apache.commons.io.*

Page 42: Groovy Fly Through

Groovy Console

Page 43: Groovy Fly Through

Building

Ant

Maven

Gradle

Page 44: Groovy Fly Through

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

IDE:s

Amazing IntelliJ support.

Eclipse known to be good..

Page 46: Groovy Fly Through

The Power of Prototyping

"You can use Groovy asan exploratory languagefor functionality spikes."

Page 47: Groovy Fly Through

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

Thank you!