Groovy Fly Through

1,076 views
996 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,076
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Groovy Fly Through

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

×