Making Java Groovy

1,385 views
1,193 views

Published on

Speaker: Kenneth Kousen
Groovy isn't designed to replace Java -- it just makes Java cleaner and easier to develop. This presentation will look at various tasks Java developers need to do and demonstrate ways Groovy can help.
Topics will include building and testing applications, accessing both relational and NoSQL databases, working with web services, and more.

Published in: Technology, News & Politics
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,385
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
26
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Making Java Groovy

  1. 1. Making Java Groovy Easier development with Groovy NFJS
  2. 2. Contact Info Ken Kousen ken.kousen@kousenit.com @kenkousen Making Java Groovy http://manning.com/kousen
  3. 3. Groovy JVM language compiler generates bytecodes
  4. 4. Groovy Groovy Is Java OO language with similar syntax Everything is an object Strings and Groovy Strings
  5. 5. Optional Typing Type is optional String, double, Employee, ... def x can be assigned to anything
  6. 6. Duck Typing def x x.method() if method() exists at runtime, no problem
  7. 7. Strings Single quotes == Java strings Double quotes == Groovy strings interpolation "Hello, ${name}!"
  8. 8. POGOs Plain Old Groovy Objects getters / setters map-based constructor public / private
  9. 9. Example Convert a POJO to a POGO JUnit test case (Java) POGO (Groovy)
  10. 10. Groovy + Java Adding Groovy to Java is easy
  11. 11. Groovy + Java Adding Java to Groovy is inevitable
  12. 12. Operator Overloading Used extensively in Groovy JDK All operators correspond to methods
  13. 13. Example java.math.BigDecimal Complex numbers (later)
  14. 14. Collections Ranges 5..10 'a'..'z' now..then
  15. 15. Collections Lists [3, 1, 4, 1, 5, 9] ['this', 'is', 'a', 'list']
  16. 16. Collections Sets [3, 1, 4, 1, 5, 9] as Set Set strings
  17. 17. Collections Maps def map = [a:1, b:2, c:3] map.d = 4 map['e'] = 5 map.put('f') = 6
  18. 18. Groovy Groovy Is Not Java closures, builders, metaprogramming
  19. 19. Closures { ... it ... } { x, y -> ... } function bodies as first class objects (oversimplified, but not a bad start...)
  20. 20. Closures list.each { ... } iterator list.collect { ... } transform list by applying closure
  21. 21. Example Closure Coercion { ... } as FilenameFilter No more anonymous inner classes
  22. 22. Example RowMapper from Spring JdbcTemplate (Java) closure for mapRow (Groovy)
  23. 23. Builders groovy.xml.MarkupBuilder groovy.json.JsonBuilder SwingBuilder, AntBuilder, ...
  24. 24. Parsers and Slurpers XmlParser / XmlSlurper JsonSlurper
  25. 25. Examples Google Geocoder lists, maps Groovy JDK collect groovy.util.XmlSlurper
  26. 26. Examples ICNDB Groovy JDK JSON
  27. 27. Metaprogramming Runtime metaprogramming Expando Expando MetaClass (EMC)
  28. 28. Example Complex numbers metaprogramming Spock
  29. 29. AST Transformations Compile time metaprogramming @Delegate @EqualsAndHashCode @Immutable
  30. 30. Example SmartPhone @Delegate Phone @Delegate Camera
  31. 31. Testing assert keyword returns nothing if true lots of debugging info if false
  32. 32. Testing GroovyTestCase extends JUnit TestCase shouldFail(...) { ... }
  33. 33. Testing Spock Framework
  34. 34. Build Tool Gradle DSL for your build
  35. 35. Only one
  36. 36. Two (or more)
  37. 37. Examples Groovy Vampires Groovy JDK REST (GET only) JSON Mongo DB
  38. 38. Examples People service Full REST (w/o hypermedia) Spock POGOs AST Transformations groovy.sql.Sql
  39. 39. Summary Groovy is Java Collections, Groovy JDK, Streamlined, simplified code Groovy is not Java Closures, Builders, Metaprogramming
  40. 40. Thanks for coming! Please complete your session evals All code is (or will be) on GitHub https://github.com/kousen

×