• Like

Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Groovy Fly Through

  • 835 views
Uploaded on

 

More in: Technology , Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
835
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
5
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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