Groovy 1

download Groovy 1

of 30

Transcript of Groovy 1

  • 8/6/2019 Groovy 1

    1/30

    Basel Baden Bern Lausanne Zurich Dsseldorf Frankfurt/M. Freiburg i. Br. Hamburg Munich Stuttgart Vienna

    GROOVY

    Mischa Klliker

    Guido Schmutz

    Zrich, 24.06.2008

  • 8/6/2019 Groovy 1

    2/30

    Groovy - Java Lounge June/2008 2 2008

    Agenda

    Data are always

    part of the game.

    Groovy: Java on dope

    Builders

    Groovy on Windows

    What about performance?

    Wrap up

  • 8/6/2019 Groovy 1

    3/30

    Groovy - Java Lounge June/2008 3 2008

    Groovy.compareTo(Java)

    GroovyScript.groovyGroovyClass.groovy

    GroovyScript.groovyGroovyClass.groovy JavaClass.java

    JavaClass.java

    javac.exegroovyc.exe

    GroovyScript.classGroovyClass.classJavaClass.class

    GroovyScript.classGroovyClass.classJavaClass.class

    Java Virtual Machine

    Java Virtual Machine

  • 8/6/2019 Groovy 1

    4/30

    Groovy - Java Lounge June/2008 4 2008

    Groovy.compareTo(Java)

    Groovy

    compiled

    dynamic typed

    weak typed

    "execute" Source

    Java

    compiled

    static typed

    weak typed

    execute compiledclass only

  • 8/6/2019 Groovy 1

    5/30

    Groovy - Java Lounge June/2008 7 2008

    Implicit classes

    Java:

    Groovy basics

  • 8/6/2019 Groovy 1

    6/30

    Groovy - Java Lounge June/2008 11 2008

    Groovy Collections

  • 8/6/2019 Groovy 1

    7/30

    Groovy - Java Lounge June/2008 12 2008

    Groovy Maps

  • 8/6/2019 Groovy 1

    8/30

    Groovy - Java Lounge June/2008 14 2008

    GPath

    Java:

  • 8/6/2019 Groovy 1

    9/30

    Groovy - Java Lounge June/2008 15 2008

    GPath - Groovy

  • 8/6/2019 Groovy 1

    10/30

    Groovy - Java Lounge June/2008 16 2008

    Closures

    Methods without a name

    Internally implemented as an inner class with one method

    Often, implicit variables like it are available

    The last expression will be automatically returned

    This is valid for all methods

  • 8/6/2019 Groovy 1

    11/30

    Groovy - Java Lounge June/2008 17 2008

    Closures with iterators

    Work like the body of a for or while loop

    Beyond times and each, there is reverseEach, eachMatchfind, findAll, findIndexOf, any, every, grep, collect,collectAll, groupBy, inject, sum,min,max, upTo,downTo, step and more iterating methods which take a closure

    Look atorg.codehaus.groovy.runtime.DefaultGroovyMethods

    to find out more

  • 8/6/2019 Groovy 1

    12/30

    Groovy - Java Lounge June/2008 18 2008

    The Meta Object Protocol (MOP)

    Caller classCaller class MetaClassMetaClass

    "Real class", e.g,

    java.util.Date

    "Real class", e.g,

    java.util.Date

    e.g. java.util.Date

    Dynamicallyadded

    methods and

    properties

    Dynamicallyadded

    methods and

    properties

  • 8/6/2019 Groovy 1

    13/30

    Groovy - Java Lounge June/2008 19 2008

    The MOP: intercept, relay, pretend

    Meta ObjectProtocol

    The heart ofGroovy

    Simulateproperties:

  • 8/6/2019 Groovy 1

    14/30

    Groovy - Java Lounge June/2008 20 2008

    Pretend properties: usage

    There are also get() and set(), only for unknown properties

  • 8/6/2019 Groovy 1

    15/30

    Groovy - Java Lounge June/2008 27 2008

    Agenda

    Data are always

    part of the game.

    Groovy: Java on dope

    Builders

    Groovy on Windows

    What about performance?

    Wrap up

  • 8/6/2019 Groovy 1

    16/30

    Groovy - Java Lounge June/2008 28 2008

    Groovy Builders

  • 8/6/2019 Groovy 1

    17/30

    Groovy - Java Lounge June/2008 29 2008

    Groovy Builders

    Builders are a set of nested Closures which

    Pretend available methods

    Delegate method calls to the builder

    Builders are executable configuration

    Builders produce their ouput (if any) immediately

    Not like config files, which are first parsed and then queried

    Also not like a template which can be reused

  • 8/6/2019 Groovy 1

    18/30

    Groovy - Java Lounge June/2008 34 2008

    Agenda

    Data are always

    part of the game.

    Groovy: Java on dope

    Builders

    Groovy on Windows

    What about performance?

    Wrap up

  • 8/6/2019 Groovy 1

    19/30

    Groovy - Java Lounge June/2008 35 2008

    Scriptom: Groovy on Windows

    Requires a DLL in the path and a JAR in GROOVY_HOME\lib(uses Jacob, the Java COM Bridge)

  • 8/6/2019 Groovy 1

    20/30

    Groovy - Java Lounge June/2008 36 2008

    Agenda

    Data are always

    part of the game.

    Groovy: Java on dope

    Builders

    Groovy on Windows

    What about performance?

    Wrap up

  • 8/6/2019 Groovy 1

    21/30

    Groovy - Java Lounge June/2008 37 2008

    Groovy performance: The price of it

    Java 71-82ms

    Groovy 220-240ms, 3x slower

    The dynamic and reflective

    nature makes Groovyconsiderably slower than Java

  • 8/6/2019 Groovy 1

    22/30

    Groovy - Java Lounge June/2008 38 2008

    Groovy performance: how come?

    compiles to:

  • 8/6/2019 Groovy 1

    23/30

    Groovy - Java Lounge June/2008 39 2008

    Groovy performance: Good news

    Groovy 1.6 promises 100x speed-up in regard of dynamic calls

    JSR 292 adds a new 'invokedynamic' instruction to the JVM

    Scheduled for Java 7

  • 8/6/2019 Groovy 1

    24/30

    Groovy - Java Lounge June/2008 40 2008

    Agenda

    Data are always

    part of the game.

    Groovy: Java on dope

    Builders

    Groovy on Windows

    What about performance?

    Wrap up

  • 8/6/2019 Groovy 1

    25/30

    Groovy - Java Lounge June/2008 41 2008

    Groovy drawbacks (are there any?)

    2..10.times { slower } // than Java

    The dynamic nature lacks of compile time security

    No safe refactorings

    Less useful code completion in IDEs

    Scripts: No need to declare variables lead to hard-to-find errors

    Test, test, test

    The resolver behaviour is sometimes tricky to understand

    e.g.: this.class.methods.name // name is a prop of a single method

    e.g.:println map[key] // is key a literal string or a variable?

    No semicolons Problem with multiline expressions

    The last expression is automatically returned

    Maybe not what I really want

  • 8/6/2019 Groovy 1

    26/30

    Groovy - Java Lounge June/2008 43 2008

    Groovy advantages

    Very expressive

    Short syntax

    Easy SQL

    Easy trees with builders

    Easy integration (e.g. with windows)

    Extends Java classes with functionality we always wished to have

    Sometimes more compile time security when writing config fileswith builders instead of XML

  • 8/6/2019 Groovy 1

    27/30

    Groovy - Java Lounge June/2008 44 2008

    Things that didnt made it into this presentation

    Groovy XML

    Various goodies

    Spread operator *. Elvis operator ?: (this is not ? : )

    Method reference operator .&

    Spaceship operator

    File.withWriter { }

    RegEx-handling and improvments

    Reverse ranges, exclusive ranges (..>)

    switch statement and the isCase() method

  • 8/6/2019 Groovy 1

    28/30

    Groovy - Java Lounge June/2008 45 2008

    Things that didnt made it into this presentation II

    GORM: Groovy Object Relational Mapping

    Is developed under the hoods of Grails

    Dynamically creating and executing classes and scripts

    Working with the MetaClass

    Groovlets (Groovy Servlets)

    Groovy Templating

    Groovy WebServices SOAP-Support

    REST-Support

    XML-RPC

  • 8/6/2019 Groovy 1

    29/30

    Groovy - Java Lounge June/2008 46 2008

    Things that didnt made it into this presentation III

    Groovy and Ant:

    AntBuilder,

    Groovy-task for Ant, excuting AntScripts withinGroovy

    Groovy and Spring

    BeanBuilder

    groovy namespace in Spring XML

    Groovy and Java

    Just works as expected

    except some class loading issues with various server configurations

    Debugging

    Currently only if you have plenty of time

  • 8/6/2019 Groovy 1

    30/30

    Basel Baden Bern Lausanne Zurich Dsseldorf Frankfurt/M. Freiburg i. Br. Hamburg Munich Stuttgart Vienna

    Thank you!

    ?www.trivadis.com