Groovy Intro

  • 624 views
Uploaded on

 

  • 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
624
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
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
  • Groovy is a dynamic, dynamically-typed, object-oriented scripting programing language, which takes inspiration from Ruby, Smalltalk or Python, and combines all the powerful features with Java-like syntax.
  • Re-define accessors Read-only properties Combinations of parameters in constructors Access without calling accessors
  • Methods become first-class citizens Represent a class with a sole method
  • Further simplification for closures with single parameters
  • Over collections you can iterate using methods like e.g. each(), upto() or step(), which take closures as parameters and invoke the closure on each element in the collection. The closures can of course interact with the rest of the code. Don’t get confused by missing parentheses around the closures, that’s just a syntax simplification. You could include them.
  • Groovy is also well suited for testing. You can test both Java and Groovy code, run the tests from Ant, Maven or your IDE and Groovy has integrated quiete a few nice features that help writing tests.
  • Groovy is ready to help you extend your Java applications into several new domains.
  • With scripting you can add code to your application at runtime. For example, you can supply a Runnable implementation in a file or a string and your application will parse it, compile and use it as if it was part of your program from the beginning.
  • Build object hierarchies or hierarchical documents The code resembles the generated document
  • It is valid Groovy code You can embed control logic
  • Which aproach is more object-oriented?
  • DSL are special purpose programming languages targeted at a particular domain or a framework to ease communication between domain experts. The languages don’t need to do more that what they have been designed for. Think of SQL or HTML as good examples. They define their own grammar, they need a parser and they nicely focus on their primary task and nothing else. These were example of external DSLs, as they have their own parser. For more ad-hoc DSLs internal DSLs are more practical. They are embedded into a host language and reuse its grammar and parser. The definition and integration of such languages is much faster then for external ones. However, you’re limited by the host language, which is why there are not many DSLs in Java. But in Groovy the situation is much better.
  • We are all domain experts for dates.
  • Some of you are domain experts for Hibernate query criteria.
  • Here’s my own DSL for my accounting application. Look at the difference in writing code.
  • You saw the powerful syntax enhancements that Groovy has, we looked into builders, scripting and DSL creation. You should be able to start writing your first Groovy applications now.

Transcript

  • 1. Groovy for Java experts V á clav Pech Software Developer and Product Evangelist JetBrains, Inc.
  • 2. Properties
  • 3. Closures
  • 4. Closures – implicit parameter
  • 5. Iterations
  • 6. GDK = JDK + FUN
    • java.util.Collection
      • each(), find(), join(), min(), max() …
    • java.lang.Object
      • any(), every(), print(), invokeMethod(), …
    • java.lang.Number
      • plus(), minus(), power(), upto(), times(), …
  • 7. Some operators
    • [‘ Java ’, ‘ Groovy ’]*.toUpperCase()
    • customer ? .shippingAddress ? .street
    • return user.locale ?: defaultLocale
  • 8. Syntax enhancements
    • Dynamic (duck) typing – optional!
    • GDK
    • Syntax enhancements
      • Properties
      • Closures
      • Named parameters
      • Collections and maps
      • Operator overloading
  • 9. Enjoy writing tests
    • For both Java and Groovy
    • Run with Ant, Maven, IDE, …
    • Integrated JUnit support
        • assert…(), shouldFail(), …
    • Relaxed typing
    • Easy mocking
  • 10. Big Boy Toys
    • Scripting
    • Builders
    • Domain Specific Languages
    • Meta-programming
  • 11. Scripting
    • Evaluate custom Groovy code
        • At run-time!!!
  • 12. Builders
    • Construct hierarchies
  • 13. Builders - GAnt
  • 14. Builders – Spring config
  • 15. Categories
  • 16. DSL
    • Limited purpose language
    • Targeted to a particular domain
    • Friendlier API to a framework
      • External
        • SQL, HTML, CSS, …
      • Internal
  • 17. DSL – Date manipulation
  • 18. DSL – Hibernate criteria
  • 19. DSL – Account manipulation
  • 20. Dynamic method invocation GroovyObject invokeMethod() getProperty() setProperty() methodMissing() propertyMissing() MetaClass invokeMethod() properties methods
  • 21. Summary
    • Groovy
      • Powerful Java extension
      • Tests, Builders, Scripting, DSLs
      • Contact me: vaclav@jetbrains.com
  • 22. Questions ?