Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Making Java Groovy (JavaOne 2013)

2,659 views

Published on

"Making Java Groovy" presentation at JavaOne 2013

Making Java Groovy (JavaOne 2013)

  1. 1. Making Java Groovy Simplify Your Java Development With Groovy JavaOne 2013
  2. 2. Contact Info Ken Kousen President, Kousen IT, Inc. www.kousenit.com ken.kousen@kousenit.com @kenkousen Making Java Groovy http://manning.com/kousen
  3. 3. Groovy JVM language
  4. 4. Groovy JVM language compiles to bytecodes
  5. 5. Groovy JVM language compiles to bytecodes optional semicolons
  6. 6. Groovy JVM language compiles to bytecodes optional semicolons optional parentheses
  7. 7. Groovy JVM language compiles to bytecodes optional semicolons optional parentheses everything is an object
  8. 8. Adding Groovy to Java
  9. 9. Adding Groovy to Java Actually, Boromir, One Does
  10. 10. Adding Groovy to Java Actually, Boromir, One Does (and simply)
  11. 11. Adding Groovy to Java Actually, Boromir, One Does (and simply) (and btw, Lord Stark, you might want to stay out of King’s Landing…)
  12. 12. Adding Groovy to Java Demo: Execute compiled Groovy with java command
  13. 13. Operator Overloading All operators in Groovy invoke methods
  14. 14. Operator Overloading All operators in Groovy invoke methods a + b a.plus(b) a – b a.minus(b) a * b a.multiply(b) a[b] a.getAt(b) a[b] = c a.putAt(b,c) a == b a.equals(b) or a.compareTo(b)
  15. 15. Operator Overloading All operators in Groovy invoke methods http://groovy.codehaus.org/Operator+Overloading a + b a.plus(b) a – b a.minus(b) a * b a.multiply(b) a[b] a.getAt(b) a[b] = c a.putAt(b,c) a == b a.equals(b) or a.compareTo(b)
  16. 16. POGOs Actually, YES
  17. 17. POGOs Actually, YES Plain Old Groovy Objects
  18. 18. POGOs Generated getters / setters
  19. 19. POGOs Generated getters / setters Map-based constructor
  20. 20. POGOs Generated getters / setters Map-based constructor AST transformations @ToString @EqualsAndHashCode @TupleConstructor @Canonical
  21. 21. Adding Groovy to Java Demo: Account class in Groovy NumberFormat from Java Test in Java
  22. 22. Collections Okay, make it so…
  23. 23. Collections Okay, make it so… Native collections
  24. 24. Collections Okay, make it so… Native collections Ranges
  25. 25. Collections Okay, make it so… Native collections Ranges Lists and Sets
  26. 26. Collections Okay, make it so… Native collections Ranges Lists and Sets Maps
  27. 27. Ranges Implement Comparable next() previous() Number, Date, String, …
  28. 28. Lists [3, 1, 4, 1, 5, 9, …] ['this', 'is', 'a', 'list’]
  29. 29. Sets [3, 1, 4, 1, 5, 9, …] as Set Set strings
  30. 30. Maps def map = [a:1, b:2, c:3] map.d = 4 map['e'] = 5 map.put('f', 6)
  31. 31. Closures { … it … } { x, y -> x + y }
  32. 32. Closures { … it … } { x, y -> x + y } anonymous functions as first-class objects
  33. 33. Closures { … it … } { x, y -> x + y } anonymous functions as first-class objects (oversimplified, but it’s a start…)
  34. 34. Closures Methods on collections: each collect findAll inject
  35. 35. Functional Groovy Closures  lambdas (if pure) collect  map findAll  filter inject  reduce
  36. 36. Groovy JDK Java classes enhanced by Groovy through metaprogramming Number String Date Collection URL …
  37. 37. RESTful Web Service Google v3 Geocoder Convert addresses to latitude / longitude
  38. 38. Geocoder Base URL URLEncoder.encode(it, 'UTF-8').join(',’)
  39. 39. Geocoder Base URL URLEncoder.encode(it, 'UTF-8').join(',’) params.collect { k,v -> "$k=$v" }.join('&')
  40. 40. Geocoder Base URL URLEncoder.encode(it, 'UTF-8').join(',’) params.collect { k,v -> "$k=$v" }.join('&') XmlSlurper.parse("$base$qs")
  41. 41. JSON ICNDB  the Internet Chuck Norris Database http://api.icndb.com/jokes/random? limitTo=[nerdy]& firstName=…& lastName=…
  42. 42. ICNDB JsonSlurper Metaprogramming
  43. 43. Spock Testing
  44. 44. Spock Specification + Mock
  45. 45. Spock Specification + Mock Includes JUnit runner
  46. 46. Spock Specification + Mock Includes JUnit runner Data-driven tests
  47. 47. Spock Specification + Mock Includes JUnit runner Data-driven tests Database-driven tests
  48. 48. Gradle Build tool
  49. 49. Gradle Build tool DSL for builds
  50. 50. Gradle Build tool DSL for builds Build script in Groovy rather than XML
  51. 51. Gradle Build tool DSL for builds Build script in Groovy rather than XML Used by: Hibernate, Spring, Android, …
  52. 52. Summary Groovy compiles to bytecodes Groovy JDK Native collections Closures Builders Metaprogramming

×