Groovy a Scripting Language for Java

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.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Favorite

    Groovy a Scripting Language for Java - Presentation Transcript

    1. Groovy: a Scripting Language for Java Charles Anderson Western Skies Consulting cander@westernskiesweb.com
    2. Outline • Introduction and Context • Stupid Pet Tricks • Using Groovy in a Java project • Conclusion • Grails?
    3. Introduction • There are now many dynamic and/or scripting languages that run on the Java Platform • Groovy is a scripting language specifically for the Java Platform - feature-rich, Java-friendly • Groovy compiles to Java classes & byte code - can use all of Java Platform
    4. Introduction • Scripting - don’t have to explicitly compile • Typing - static or dynamic (optional/duck) • Mature - version 1.5, 1.6 Beta 2 • Books, websites, commercial backing, JSR • IDE Support
    5. Code Comparison Groovy Groovy Java Java Syntax Functionality
    6. Java or Groovy? class Book { private String title; Book (String theTitle) { title = theTitle; } String getTitle() { return title } }
    7. Stupid Pet Tricks public class Main { public static void main(...) System.out.println(“Hello World”); }} println ‘Hello World’
    8. More Powerful Switch switch (x) { case 0: // single int case 1..9: // range case [11,13,17]: // list case Float: // type check case ‘cat’: // string case ~/[A-Z][0-9]/: //regexp }
    9. Groovy Beans public class MyBean implements Serializable { private String myProp; public String getMyProp()... public void setMyProp(...)... } class MyBean implements Serializable { String myProp; }
    10. Terse NPE Safety if (x!=null && x.y!=null) { z = x.y.z; } z = x?.y?.z
    11. Bob the Builder writer = new FileWriter(‘bob.html’) def page = new MarkupBuilder(writer) page.html{ head { title ‘An HTML Page’ } body { h1 ‘The big heading’ form (action:’submit’) { input(type:’checkbox’, id:’yes’) input(type:’checkbox’, id:’no’) }}}
    12. Closures • A closure is a chunk of code that is a first- class entity/object in the language • From functional programming • Can accept parameters and return a value • Typical Java solution: inner classes, visitor pattern, template method pattern
    13. Exorcising Inner Classes t = new Thread(new Runable() { public void run() { // some code }}); t.start(); Thread.start { //some code }
    14. Java Iteration for (Iterator it=coll.iterator(); it.hasNext(); ) { Item item=(Item)it.next(); //some code } for (Item item : coll) { // some code }
    15. Groovy Iteration coll.each(item-> // some code) coll.findAll(it % 2 == 0).each(...) new File(‘foo.txt’).eachLine { println it }
    16. Embrace and Extend • Groovy “extends” Java classes - GDK • Object, GString, Collections, Files, Threads, Beans • Groovy enhances existing Java tools • JUnit, Ant, Swing • Reduce tedious boiler-plate - DRY
    17. using Groovy • Little development tasks - automation • Can script COM and Windows • Experiments and spiking • Testing - unit tests, data loading, scaffolding
    18. Conclusion • Groovy is a dynamic scripting language that is designed specifically for Java • Lots of “tricks” to make life easier, but they can be introduced gradually • Groovy is a playground for the future of Java - e.g., closures, properties • Questions, job leads, Grails?
    19. Grails: Groovy on Rails • Accelerate and simplify web development on the Java Platform - but not Java Language • Deploys as a WAR file • MVC structure • Convention over configuration • Uses Spring and Hibernate under covers
    20. Grails Commands Usage: grails command create-app run-app create-domain-class Entity generate-all Entity generate-webtest run-webtest war

    + Charles AndersonCharles Anderson, 10 months ago

    custom

    1204 views, 1 favs, 1 embeds more stats

    This was a talk I gave at the Salem Java Users Grou more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1204
      • 1200 on SlideShare
      • 4 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 35
    Most viewed embeds
    • 4 views on http://western-skies.blogspot.com

    more

    All embeds
    • 4 views on http://western-skies.blogspot.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories