$30 off During Our Annual Pro Sale. View Details »

Groovy in 2014 and beyond -- JavaOne 2014

Groovy in 2014 and beyond -- JavaOne 2014

The latest developments in the Groovy programming language proejct

Guillaume Laforge

October 02, 2014
Tweet

More Decks by Guillaume Laforge

Other Decks in Programming

Transcript

  1. Groovy
    in 2014
    and beyond
    Guillaume Laforge
    Groovy project lead
    Pivotal
    @glaforge

    View Slide

  2. Stay up-to-date
    Groovy Weekly Newsletter (Every Tuesday)
    http://beta.groovy-lang.org/groovy-weekly.html
    2

    View Slide

  3. The Groovy roadmap
    3
    2015
    2014
    2013
    Groovy 2.3
    Groovy 2.4
    Groovy 2.2
    Groovy 2.5 ?
    Groovy 3.0 ?

    View Slide

  4. @glaforge — Groovy in 2014 and beyond
    Groovy 2.3

    View Slide

  5. @glaforge — Groovy in 2014 and beyond
    JDK 8 support

    View Slide

  6. JDK 8 support — closures vs lambdas
    6
    IntStream.range(1,  100).forEach(s  -­‐>      
                                             System.out.println(s));  
    !
    Files.lines(Paths.get('README.adoc'))  
             .map(it  -­‐>  it.toUpperCase())  
             .forEach(it  -­‐>  System.out.println(it));

    View Slide

  7. JDK 8 support — closures vs lambdas
    6
    IntStream.range(1,  100).forEach(s  -­‐>      
                                             System.out.println(s));  
    !
    Files.lines(Paths.get('README.adoc'))  
             .map(it  -­‐>  it.toUpperCase())  
             .forEach(it  -­‐>  System.out.println(it));
    IntStream.range(1,  100).forEach  {  println  it  }  
    !
    Files.lines(Paths.get('README.adoc'))  
             .map  {  it.toUpperCase()  }  
             .forEach  {  println  it  }

    View Slide

  8. JDK 8 support — closures vs lambdas
    6
    IntStream.range(1,  100).forEach(s  -­‐>      
                                             System.out.println(s));  
    !
    Files.lines(Paths.get('README.adoc'))  
             .map(it  -­‐>  it.toUpperCase())  
             .forEach(it  -­‐>  System.out.println(it));
    IntStream.range(1,  100).forEach  {  println  it  }  
    !
    Files.lines(Paths.get('README.adoc'))  
             .map  {  it.toUpperCase()  }  
             .forEach  {  println  it  }
    Use Groovy closures
    wherever you pass
    lambdas in Java 8

    View Slide

  9. @glaforge — Groovy in 2014 and beyond
    Traits

    View Slide

  10. Traits
    • Like interfaces, but with method bodies
    • similar to Java 8 interface default methods

    • Elegant way to compose behavior

    • multiple inheritance without the « diamond » problem

    • Traits can also be stateful

    • traits can have properties like normal classes

    • Compatible with static typing and static compilation
    • class methods from traits also visible from Java classes

    • Also possible to implement traits at runtime 8

    View Slide

  11. Traits: a simple example
    9
    trait  FlyingAbility  {  
           String  fly()  {  "I'm  flying!"  }  
    }  
    !
    class  Bird  implements  FlyingAbility  {}  
    def  b  =  new  Bird()  
    !
    assert  b.fly()  ==  "I'm  flying!"

    View Slide

  12. Traits: a simple example
    9
    trait  FlyingAbility  {  
           String  fly()  {  "I'm  flying!"  }  
    }  
    !
    class  Bird  implements  FlyingAbility  {}  
    def  b  =  new  Bird()  
    !
    assert  b.fly()  ==  "I'm  flying!"
    « trait », a new keyword
    for a new concept

    View Slide

  13. Traits: a simple example
    9
    trait  FlyingAbility  {  
           String  fly()  {  "I'm  flying!"  }  
    }  
    !
    class  Bird  implements  FlyingAbility  {}  
    def  b  =  new  Bird()  
    !
    assert  b.fly()  ==  "I'm  flying!"
    a class 

    « implements »

    a trait

    View Slide

  14. Traits: a simple example
    9
    trait  FlyingAbility  {  
           String  fly()  {  "I'm  flying!"  }  
    }  
    !
    class  Bird  implements  FlyingAbility  {}  
    def  b  =  new  Bird()  
    !
    assert  b.fly()  ==  "I'm  flying!"
    the fly() method from
    the trait is available

    View Slide

  15. Traits: a simple example
    9
    trait  FlyingAbility  {  
           String  fly()  {  "I'm  flying!"  }  
    }  
    !
    class  Bird  implements  FlyingAbility  {}  
    def  b  =  new  Bird()  
    !
    assert  b.fly()  ==  "I'm  flying!"

    View Slide

  16. Traits: stateful
    10
    trait  Named  {  
           String  name  
    }  
    !
    class  Bird  implements  Named  {}  
    def  b  =  new  Bird(name:  'Colibri')  
    !
    assert  b.name  ==  'Colibri'

    View Slide

  17. Traits: stateful
    10
    trait  Named  {  
           String  name  
    }  
    !
    class  Bird  implements  Named  {}  
    def  b  =  new  Bird(name:  'Colibri')  
    !
    assert  b.name  ==  'Colibri'
    a Groovy property

    View Slide

  18. Traits: stateful
    10
    trait  Named  {  
           String  name  
    }  
    !
    class  Bird  implements  Named  {}  
    def  b  =  new  Bird(name:  'Colibri')  
    !
    assert  b.name  ==  'Colibri'
    implement the trait

    View Slide

  19. Traits: stateful
    10
    trait  Named  {  
           String  name  
    }  
    !
    class  Bird  implements  Named  {}  
    def  b  =  new  Bird(name:  'Colibri')  
    !
    assert  b.name  ==  'Colibri'
    Groovy named
    argument constructor

    View Slide

  20. Traits: stateful
    10
    trait  Named  {  
           String  name  
    }  
    !
    class  Bird  implements  Named  {}  
    def  b  =  new  Bird(name:  'Colibri')  
    !
    assert  b.name  ==  'Colibri'
    access the property

    View Slide

  21. Traits: stateful
    10
    trait  Named  {  
           String  name  
    }  
    !
    class  Bird  implements  Named  {}  
    def  b  =  new  Bird(name:  'Colibri')  
    !
    assert  b.name  ==  'Colibri'

    View Slide

  22. Traits: inheritance
    11
    trait  Named  {  String  name  }  
    !
    trait  FlyingAbility  extends  Named  {  
           String  fly()  {  "I'm  a  flying  ${name}!"  }  
    }  
    !
    class  Bird  implements  FlyingAbility  {}  
    def  b  =  new  Bird(name:  'Colibri')  
    !
    assert  b.name  ==  'Colibri'  
    assert  b.fly()  ==  "I'm  a  flying  Colibri!"

    View Slide

  23. Traits: inheritance
    11
    trait  Named  {  String  name  }  
    !
    trait  FlyingAbility  extends  Named  {  
           String  fly()  {  "I'm  a  flying  ${name}!"  }  
    }  
    !
    class  Bird  implements  FlyingAbility  {}  
    def  b  =  new  Bird(name:  'Colibri')  
    !
    assert  b.name  ==  'Colibri'  
    assert  b.fly()  ==  "I'm  a  flying  Colibri!"
    extend the Named trait

    View Slide

  24. Traits: inheritance
    11
    trait  Named  {  String  name  }  
    !
    trait  FlyingAbility  extends  Named  {  
           String  fly()  {  "I'm  a  flying  ${name}!"  }  
    }  
    !
    class  Bird  implements  FlyingAbility  {}  
    def  b  =  new  Bird(name:  'Colibri')  
    !
    assert  b.name  ==  'Colibri'  
    assert  b.fly()  ==  "I'm  a  flying  Colibri!"
    access the name property

    View Slide

  25. Traits: inheritance
    11
    trait  Named  {  String  name  }  
    !
    trait  FlyingAbility  extends  Named  {  
           String  fly()  {  "I'm  a  flying  ${name}!"  }  
    }  
    !
    class  Bird  implements  FlyingAbility  {}  
    def  b  =  new  Bird(name:  'Colibri')  
    !
    assert  b.name  ==  'Colibri'  
    assert  b.fly()  ==  "I'm  a  flying  Colibri!"
    implement the
    composite trait

    View Slide

  26. Traits: inheritance
    11
    trait  Named  {  String  name  }  
    !
    trait  FlyingAbility  extends  Named  {  
           String  fly()  {  "I'm  a  flying  ${name}!"  }  
    }  
    !
    class  Bird  implements  FlyingAbility  {}  
    def  b  =  new  Bird(name:  'Colibri')  
    !
    assert  b.name  ==  'Colibri'  
    assert  b.fly()  ==  "I'm  a  flying  Colibri!"

    View Slide

  27. Traits: what about conflicts?
    12
    trait  KiteSurfer  {  String  surf()  {  'kite'  }  }  
    !
    trait  WebSurfer    {  String  surf()  {    'web'  }  }  
    !
    class  Person  {  String  name  }  
    !
    class  Hipster  extends  Person  
                         implements  KiteSurfer,  WebSurfer  {}  
    !
    def  h  =  new  Hipster()  
    assert  h.surf()  ==  'web'

    View Slide

  28. Traits: what about conflicts?
    12
    trait  KiteSurfer  {  String  surf()  {  'kite'  }  }  
    !
    trait  WebSurfer    {  String  surf()  {    'web'  }  }  
    !
    class  Person  {  String  name  }  
    !
    class  Hipster  extends  Person  
                         implements  KiteSurfer,  WebSurfer  {}  
    !
    def  h  =  new  Hipster()  
    assert  h.surf()  ==  'web'
    two surf() methods

    View Slide

  29. Traits: what about conflicts?
    12
    trait  KiteSurfer  {  String  surf()  {  'kite'  }  }  
    !
    trait  WebSurfer    {  String  surf()  {    'web'  }  }  
    !
    class  Person  {  String  name  }  
    !
    class  Hipster  extends  Person  
                         implements  KiteSurfer,  WebSurfer  {}  
    !
    def  h  =  new  Hipster()  
    assert  h.surf()  ==  'web'

    View Slide

  30. Traits: what about conflicts?
    12
    trait  KiteSurfer  {  String  surf()  {  'kite'  }  }  
    !
    trait  WebSurfer    {  String  surf()  {    'web'  }  }  
    !
    class  Person  {  String  name  }  
    !
    class  Hipster  extends  Person  
                         implements  KiteSurfer,  WebSurfer  {}  
    !
    def  h  =  new  Hipster()  
    assert  h.surf()  ==  'web'
    extending a class and
    implementing the two traits

    View Slide

  31. Traits: what about conflicts?
    12
    trait  KiteSurfer  {  String  surf()  {  'kite'  }  }  
    !
    trait  WebSurfer    {  String  surf()  {    'web'  }  }  
    !
    class  Person  {  String  name  }  
    !
    class  Hipster  extends  Person  
                         implements  KiteSurfer,  WebSurfer  {}  
    !
    def  h  =  new  Hipster()  
    assert  h.surf()  ==  'web'

    View Slide

  32. Traits: what about conflicts?
    12
    trait  KiteSurfer  {  String  surf()  {  'kite'  }  }  
    !
    trait  WebSurfer    {  String  surf()  {    'web'  }  }  
    !
    class  Person  {  String  name  }  
    !
    class  Hipster  extends  Person  
                         implements  KiteSurfer,  WebSurfer  {}  
    !
    def  h  =  new  Hipster()  
    assert  h.surf()  ==  'web'
    last declared trait wins!

    View Slide

  33. Traits: what about conflicts?
    12
    trait  KiteSurfer  {  String  surf()  {  'kite'  }  }  
    !
    trait  WebSurfer    {  String  surf()  {    'web'  }  }  
    !
    class  Person  {  String  name  }  
    !
    class  Hipster  extends  Person  
                         implements  KiteSurfer,  WebSurfer  {}  
    !
    def  h  =  new  Hipster()  
    assert  h.surf()  ==  'web'

    View Slide

  34. Traits: what about conflicts?
    13
    trait  KiteSurfer  {  String  surf()  {  'kite'  }  }  
    !
    trait  WebSurfer    {  String  surf()  {    'web'  }  }  
    !
    class  Person  {  String  name  }  
    !
    class  Hipster  extends  Person  
                         implements  WebSurfer,  KiteSurfer  {}  
    !
    def  h  =  new  Hipster()  
    assert  h.surf()  ==  'kite'

    View Slide

  35. Traits: what about conflicts?
    13
    trait  KiteSurfer  {  String  surf()  {  'kite'  }  }  
    !
    trait  WebSurfer    {  String  surf()  {    'web'  }  }  
    !
    class  Person  {  String  name  }  
    !
    class  Hipster  extends  Person  
                         implements  WebSurfer,  KiteSurfer  {}  
    !
    def  h  =  new  Hipster()  
    assert  h.surf()  ==  'kite'
    reverse the order!

    View Slide

  36. Traits: what about conflicts?
    13
    trait  KiteSurfer  {  String  surf()  {  'kite'  }  }  
    !
    trait  WebSurfer    {  String  surf()  {    'web'  }  }  
    !
    class  Person  {  String  name  }  
    !
    class  Hipster  extends  Person  
                         implements  WebSurfer,  KiteSurfer  {}  
    !
    def  h  =  new  Hipster()  
    assert  h.surf()  ==  'kite'

    View Slide

  37. Traits: what about conflicts?
    14
    trait  KiteSurfer  {  String  surf()  {  'kite'  }  }  
    !
    trait  WebSurfer    {  String  surf()  {    'web'  }  }  
    !
    class  Person  {  String  name  }  
    !
    class  Hipster  extends  Person  
                         implements  WebSurfer,  KiteSurfer  {  
           String  surf()  {  KiteSurfer.super.surf()  }  
    }  
    !
    def  h  =  new  Hipster()  
    assert  h.surf()  ==  'kite'

    View Slide

  38. Traits: what about conflicts?
    14
    trait  KiteSurfer  {  String  surf()  {  'kite'  }  }  
    !
    trait  WebSurfer    {  String  surf()  {    'web'  }  }  
    !
    class  Person  {  String  name  }  
    !
    class  Hipster  extends  Person  
                         implements  WebSurfer,  KiteSurfer  {  
           String  surf()  {  KiteSurfer.super.surf()  }  
    }  
    !
    def  h  =  new  Hipster()  
    assert  h.surf()  ==  'kite'
    Be explicit!
    Override surf() 

    & use ‘super’

    View Slide

  39. Traits: what about conflicts?
    14
    trait  KiteSurfer  {  String  surf()  {  'kite'  }  }  
    !
    trait  WebSurfer    {  String  surf()  {    'web'  }  }  
    !
    class  Person  {  String  name  }  
    !
    class  Hipster  extends  Person  
                         implements  WebSurfer,  KiteSurfer  {  
           String  surf()  {  KiteSurfer.super.surf()  }  
    }  
    !
    def  h  =  new  Hipster()  
    assert  h.surf()  ==  'kite'
    Your class method takes
    precedence over the traits

    View Slide

  40. Traits: what about conflicts?
    14
    trait  KiteSurfer  {  String  surf()  {  'kite'  }  }  
    !
    trait  WebSurfer    {  String  surf()  {    'web'  }  }  
    !
    class  Person  {  String  name  }  
    !
    class  Hipster  extends  Person  
                         implements  WebSurfer,  KiteSurfer  {  
           String  surf()  {  KiteSurfer.super.surf()  }  
    }  
    !
    def  h  =  new  Hipster()  
    assert  h.surf()  ==  'kite'

    View Slide

  41. trait  Named  {  
           String  name  
    }  
    !
    class  Animal  {}  
    class  NamedAnimal  implements  Named  {}  
    !
    def  na  =  new  NamedAnimal(name:  'Felix')  
    !
    assert  na.name  ==  'Felix'
    Traits: runtime implementation
    15

    View Slide

  42. trait  Named  {  
           String  name  
    }  
    !
    class  Animal  {}  
    class  NamedAnimal  implements  Named  {}  
    !
    def  na  =  new  NamedAnimal(name:  'Felix')  
    !
    assert  na.name  ==  'Felix'
    Traits: runtime implementation
    15
    Somewhat artificial to have to
    create an intermediary class to
    get named animals

    View Slide

  43. trait  Named  {  
           String  name  
    }  
    !
    class  Animal  {}  
    class  NamedAnimal  implements  Named  {}  
    !
    def  na  =  new  NamedAnimal(name:  'Felix')  
    !
    assert  na.name  ==  'Felix'
    Traits: runtime implementation
    15

    View Slide

  44. trait  Named  {  
           String  name  
    }  
    !
    class  Animal  {}  
    !
    !
    def  na  =  new  Animal()  as  Named  
    na.name  =  'Felix'  
    assert  na.name  ==  'Felix'
    Traits: runtime implementation
    16

    View Slide

  45. trait  Named  {  
           String  name  
    }  
    !
    class  Animal  {}  
    !
    !
    def  na  =  new  Animal()  as  Named  
    na.name  =  'Felix'  
    assert  na.name  ==  'Felix'
    Traits: runtime implementation
    16
    Runtime trait, 

    with Groovy’s usual
    coercion mechanism

    View Slide

  46. trait  Named  {  
           String  name  
    }  
    !
    class  Animal  {}  
    !
    !
    def  na  =  new  Animal()  as  Named  
    na.name  =  'Felix'  
    assert  na.name  ==  'Felix'
    Traits: runtime implementation
    16

    View Slide

  47. Traits: runtime implementation
    17
    trait  Named  {  String  name  }  
    !
    trait  Quacks  {  
           String  quack()  {  'Quack!'  }  
    }  
    !
    class  Animal  {}  
    !
    def  na  =  new  Animal().withTraits  Named,  Quacks  
    na.name  =  'Daffy'  
    assert  na.name  ==  'Daffy'  
    assert  na.quack()  ==  'Quack!'

    View Slide

  48. Traits: runtime implementation
    17
    trait  Named  {  String  name  }  
    !
    trait  Quacks  {  
           String  quack()  {  'Quack!'  }  
    }  
    !
    class  Animal  {}  
    !
    def  na  =  new  Animal().withTraits  Named,  Quacks  
    na.name  =  'Daffy'  
    assert  na.name  ==  'Daffy'  
    assert  na.quack()  ==  'Quack!'
    Implement several traits at
    once, at runtime

    View Slide

  49. Traits: runtime implementation
    17
    trait  Named  {  String  name  }  
    !
    trait  Quacks  {  
           String  quack()  {  'Quack!'  }  
    }  
    !
    class  Animal  {}  
    !
    def  na  =  new  Animal().withTraits  Named,  Quacks  
    na.name  =  'Daffy'  
    assert  na.name  ==  'Daffy'  
    assert  na.quack()  ==  'Quack!'

    View Slide

  50. @glaforge — Groovy in 2014 and beyond
    AST transforms

    View Slide

  51. New: @TailRecursive
    19
    import  groovy.transform.TailRecursive  
    !
    @TailRecursive  
    def  fact(BigInteger  n,  accu  =  1G)  {  
           if  (n  <  2)  accu  
           else  fact(n  -­‐  1,  n  *  accu)  
    }  
    !
    assert  fact(1000)  >  10e2566

    View Slide

  52. New: @TailRecursive
    19
    import  groovy.transform.TailRecursive  
    !
    @TailRecursive  
    def  fact(BigInteger  n,  accu  =  1G)  {  
           if  (n  <  2)  accu  
           else  fact(n  -­‐  1,  n  *  accu)  
    }  
    !
    assert  fact(1000)  >  10e2566
    Rewrites tail recursive
    friendly function serially

    View Slide

  53. New: @TailRecursive
    19
    import  groovy.transform.TailRecursive  
    !
    @TailRecursive  
    def  fact(BigInteger  n,  accu  =  1G)  {  
           if  (n  <  2)  accu  
           else  fact(n  -­‐  1,  n  *  accu)  
    }  
    !
    assert  fact(1000)  >  10e2566
    Doesn’t blow up with a
    stack overflow error

    View Slide

  54. New: @TailRecursive
    19
    import  groovy.transform.TailRecursive  
    !
    @TailRecursive  
    def  fact(BigInteger  n,  accu  =  1G)  {  
           if  (n  <  2)  accu  
           else  fact(n  -­‐  1,  n  *  accu)  
    }  
    !
    assert  fact(1000)  >  10e2566
    Downside of tail recursion is 

    you might have to rewrite 

    your algo to be tailrec friendly

    View Slide

  55. New: @TailRecursive
    19
    import  groovy.transform.TailRecursive  
    !
    @TailRecursive  
    def  fact(BigInteger  n,  accu  =  1G)  {  
           if  (n  <  2)  accu  
           else  fact(n  -­‐  1,  n  *  accu)  
    }  
    !
    assert  fact(1000)  >  10e2566

    View Slide

  56. New: @Sortable
    20
    import  groovy.transform.*  
    !
    @Sortable  
    class  Person  {  
           String  lastName  
           String  firstName  
           int  age  
    }

    View Slide

  57. New: @Sortable
    20
    import  groovy.transform.*  
    !
    @Sortable  
    class  Person  {  
           String  lastName  
           String  firstName  
           int  age  
    }
    Makes the class Comparable
    by multiple Comparators

    View Slide

  58. New: @Sortable
    20
    import  groovy.transform.*  
    !
    @Sortable  
    class  Person  {  
           String  lastName  
           String  firstName  
           int  age  
    }
    First compare by lastName,
    then by firstName, etc.

    View Slide

  59. New: @Sortable
    20
    import  groovy.transform.*  
    !
    @Sortable  
    class  Person  {  
           String  lastName  
           String  firstName  
           int  age  
    }
    You can also specify
    ‘includes’ / ‘excludes’
    properties

    View Slide

  60. New: @Sortable
    20
    import  groovy.transform.*  
    !
    @Sortable  
    class  Person  {  
           String  lastName  
           String  firstName  
           int  age  
    }

    View Slide

  61. @BaseScript improvements
    21
    abstract  class  CustomBase  extends  Script  {  
           int  meaningOfLife  =  42  
    }
    @BaseScript(CustomBase)  
    import  groovy.transform.BaseScript  
    !
    assert  meaningOfLife  ==  42

    View Slide

  62. @BaseScript improvements
    21
    abstract  class  CustomBase  extends  Script  {  
           int  meaningOfLife  =  42  
    }
    @BaseScript(CustomBase)  
    import  groovy.transform.BaseScript  
    !
    assert  meaningOfLife  ==  42
    You can add your own base methods and
    properties to all compiled scripts

    View Slide

  63. @BaseScript improvements
    21
    abstract  class  CustomBase  extends  Script  {  
           int  meaningOfLife  =  42  
    }
    @BaseScript(CustomBase)  
    import  groovy.transform.BaseScript  
    !
    assert  meaningOfLife  ==  42
    Define the base script
    class for this script

    View Slide

  64. @BaseScript improvements
    21
    abstract  class  CustomBase  extends  Script  {  
           int  meaningOfLife  =  42  
    }
    @BaseScript(CustomBase)  
    import  groovy.transform.BaseScript  
    !
    assert  meaningOfLife  ==  42
    Ability to put the annotation on
    imports & package

    View Slide

  65. @BaseScript improvements
    21
    abstract  class  CustomBase  extends  Script  {  
           int  meaningOfLife  =  42  
    }
    @BaseScript(CustomBase)  
    import  groovy.transform.BaseScript  
    !
    assert  meaningOfLife  ==  42

    View Slide

  66. @BaseScript custom abstract method
    22
    abstract  class  CustomBase  extends  Script  {  
       def  run()  {  
           before()  
           internalRun()  
           after()  
       }  
    !
       abstract  internalRun()  
    !
       def  before()  {  println  'before'  }  
       def  after()    {  println  'after'    }  
    }

    View Slide

  67. @BaseScript custom abstract method
    22
    abstract  class  CustomBase  extends  Script  {  
       def  run()  {  
           before()  
           internalRun()  
           after()  
       }  
    !
       abstract  internalRun()  
    !
       def  before()  {  println  'before'  }  
       def  after()    {  println  'after'    }  
    }
    import  groovy.transform.BaseScript  
    @BaseScript  CustomBase  script  
    !
    println  'Hello'

    View Slide

  68. @BaseScript custom abstract method
    22
    abstract  class  CustomBase  extends  Script  {  
       def  run()  {  
           before()  
           internalRun()  
           after()  
       }  
    !
       abstract  internalRun()  
    !
       def  before()  {  println  'before'  }  
       def  after()    {  println  'after'    }  
    }
    import  groovy.transform.BaseScript  
    @BaseScript  CustomBase  script  
    !
    println  'Hello'
    You can define your own abstract
    method for script bodies

    View Slide

  69. @BaseScript custom abstract method
    22
    abstract  class  CustomBase  extends  Script  {  
       def  run()  {  
           before()  
           internalRun()  
           after()  
       }  
    !
       abstract  internalRun()  
    !
       def  before()  {  println  'before'  }  
       def  after()    {  println  'after'    }  
    }
    import  groovy.transform.BaseScript  
    @BaseScript  CustomBase  script  
    !
    println  'Hello'

    View Slide

  70. @glaforge — Groovy in 2014 and beyond
    NIO2 module

    View Slide

  71. JDK 7+ NIO2 module
    • All the familiar methods on File 

    retrofitted on Path as well
    24
    path.withReader  {  Reader  r  -­‐>  ...  }  
    path.eachLine  {  String  line  -­‐>  ...  }  
    path.eachFileRecurse  {  Path  p  -­‐>  ...  }  
    path  <<  'some  content'  
    path  <<  bytes  
    path.readLines()  

    View Slide

  72. JDK 7+ NIO2 module
    • All the familiar methods on File 

    retrofitted on Path as well
    24
    path.withReader  {  Reader  r  -­‐>  ...  }  
    path.eachLine  {  String  line  -­‐>  ...  }  
    path.eachFileRecurse  {  Path  p  -­‐>  ...  }  
    path  <<  'some  content'  
    path  <<  bytes  
    path.readLines()  

    Feature request to add all the
    java.nio.file.Files static utility
    methods as GDK

    View Slide

  73. JDK 7+ NIO2 module
    • All the familiar methods on File 

    retrofitted on Path as well
    24
    path.withReader  {  Reader  r  -­‐>  ...  }  
    path.eachLine  {  String  line  -­‐>  ...  }  
    path.eachFileRecurse  {  Path  p  -­‐>  ...  }  
    path  <<  'some  content'  
    path  <<  bytes  
    path.readLines()  

    View Slide

  74. @glaforge — Groovy in 2014 and beyond
    JSON

    View Slide

  75. JSON parser / builder perf. increase
    • Re-implementation of JSON support for speed & efficiency

    • parser forked off the Boon JSON project

    • serializer carefully fine-tuned

    !
    • Article on the parsing speed improvements

    • http://rick-hightower.blogspot.fr/2014/04/groovy-and-boon-provide-fastest-json.html
    26

    View Slide

  76. JSON parser / builder perf. increase
    • Re-implementation of JSON support for speed & efficiency

    • parser forked off the Boon JSON project

    • serializer carefully fine-tuned

    !
    • Article on the parsing speed improvements

    • http://rick-hightower.blogspot.fr/2014/04/groovy-and-boon-provide-fastest-json.html
    26

    View Slide

  77. JSON parser / builder perf. increase
    • Re-implementation of JSON support for speed & efficiency

    • parser forked off the Boon JSON project

    • serializer carefully fine-tuned

    !
    • Article on the parsing speed improvements

    • http://rick-hightower.blogspot.fr/2014/04/groovy-and-boon-provide-fastest-json.html
    26
    Benchmark gives 3x to 4x
    performance factor 

    over Jackson and GSON

    View Slide

  78. JSON parser / builder perf. increase
    • Re-implementation of JSON support for speed & efficiency

    • parser forked off the Boon JSON project

    • serializer carefully fine-tuned

    !
    • Article on the parsing speed improvements

    • http://rick-hightower.blogspot.fr/2014/04/groovy-and-boon-provide-fastest-json.html
    26

    View Slide

  79. New modes for parsing
    • Original JsonSlurper renamed to JsonSlurperClassic

    !
    • Additional parsing modes:

    • INDEX_OVERLAY: super fast for <2MB payloads

    o using a « parsing overlay » technique

    • CHARACTER_SOURCE: for >2MB payloads

    o implemented with sliding windows over readers

    • LAX: beyond the JSON spec, nice for configuration files

    o support single quotes, / and # comments

    • CHAR_BUFFER: general purpose
    27

    View Slide

  80. JsonSlurper for configuration files
    28
    import  groovy.json.*  
    import  static  groovy.json.JsonParserType.*  
    !
    def  parser  =  new  JsonSlurper().setType(LAX)  
    !
    def  conf  =  parser.parseText  '''  
           //  configuration  file  
           {  
                   //  no  quote  for  key,  single  quoted  value  
                   environment:  'production'  
                   #  pound-­‐style  comment  
                   'server':  5  
           }  
    '''  
    !
    assert  conf.environment  ==  'production'  
    assert  conf.server  ==  5

    View Slide

  81. JsonSlurper for configuration files
    28
    import  groovy.json.*  
    import  static  groovy.json.JsonParserType.*  
    !
    def  parser  =  new  JsonSlurper().setType(LAX)  
    !
    def  conf  =  parser.parseText  '''  
           //  configuration  file  
           {  
                   //  no  quote  for  key,  single  quoted  value  
                   environment:  'production'  
                   #  pound-­‐style  comment  
                   'server':  5  
           }  
    '''  
    !
    assert  conf.environment  ==  'production'  
    assert  conf.server  ==  5
    More tolerant parser: 

    single quotes, 

    non-quoted keys, 

    // and # comments,
    missing comas

    View Slide

  82. JsonSlurper for configuration files
    28
    import  groovy.json.*  
    import  static  groovy.json.JsonParserType.*  
    !
    def  parser  =  new  JsonSlurper().setType(LAX)  
    !
    def  conf  =  parser.parseText  '''  
           //  configuration  file  
           {  
                   //  no  quote  for  key,  single  quoted  value  
                   environment:  'production'  
                   #  pound-­‐style  comment  
                   'server':  5  
           }  
    '''  
    !
    assert  conf.environment  ==  'production'  
    assert  conf.server  ==  5

    View Slide

  83. @glaforge — Groovy in 2014 and beyond
    Markup template engine
    >

    View Slide

  84. Markup template engine
    • Based on the principles of Groovy’s « builders »

    • and particularly the MarkupBuilder class

    for generating arbitrary XML / HTML payloads

    !
    • Compiled statically for fast template rendering
    !
    • Internationalization aware

    • provide the desired Locale in the configuration object

    • usual suffix notation template_fr_FR.tpl

    !
    • Custom base template class

    • ability to provide reusable methods across your templates 30

    View Slide

  85. Markup template engine
    • Based on the principles of Groovy’s « builders »

    • and particularly the MarkupBuilder class

    for generating arbitrary XML / HTML payloads

    !
    • Compiled statically for fast template rendering
    !
    • Internationalization aware

    • provide the desired Locale in the configuration object

    • usual suffix notation template_fr_FR.tpl

    !
    • Custom base template class

    • ability to provide reusable methods across your templates 30
    Spring Boot
    approved

    View Slide

  86. Markup template engine — the idea
    31
    cars  {  
         cars.each  {  
                 car(make:  it.make,  name:  it.name)  
         }  
    }

    View Slide

  87. Markup template engine — the idea
    31
    cars  {  
         cars.each  {  
                 car(make:  it.make,  name:  it.name)  
         }  
    }
    Your template

    View Slide

  88. Markup template engine — the idea
    31
    cars  {  
         cars.each  {  
                 car(make:  it.make,  name:  it.name)  
         }  
    }
    model = [cars: [!
    new Car(make: 'Peugeot', name: '508'), !
    new Car(make: 'Toyota', name: 'Prius’)!
    ]]

    View Slide

  89. Markup template engine — the idea
    31
    cars  {  
         cars.each  {  
                 car(make:  it.make,  name:  it.name)  
         }  
    }
    model = [cars: [!
    new Car(make: 'Peugeot', name: '508'), !
    new Car(make: 'Toyota', name: 'Prius’)!
    ]]
    Feed a model into
    your template

    View Slide

  90. Markup template engine — the idea
    31
    cars  {  
         cars.each  {  
                 car(make:  it.make,  name:  it.name)  
         }  
    }
    model = [cars: [!
    new Car(make: 'Peugeot', name: '508'), !
    new Car(make: 'Toyota', name: 'Prius’)!
    ]]
    !
    !
    !

    View Slide

  91. Markup template engine — the idea
    31
    cars  {  
         cars.each  {  
                 car(make:  it.make,  name:  it.name)  
         }  
    }
    model = [cars: [!
    new Car(make: 'Peugeot', name: '508'), !
    new Car(make: 'Toyota', name: 'Prius’)!
    ]]
    !
    !
    !

    Generate the
    XML output

    View Slide

  92. Markup template engine — the idea
    31
    cars  {  
         cars.each  {  
                 car(make:  it.make,  name:  it.name)  
         }  
    }
    model = [cars: [!
    new Car(make: 'Peugeot', name: '508'), !
    new Car(make: 'Toyota', name: 'Prius’)!
    ]]
    !
    !
    !

    View Slide

  93. Markup template engine — in action
    32
    import  groovy.text.markup.*  
    !
    def  config  =  new  TemplateConfiguration()  
    def  engine  =  new  MarkupTemplateEngine(config)  
    def  tmpl  =  engine.createTemplate('''  
           p("Hello  ${model.name}")  
    ''')  
    def  model  =  [name:  'World']  
    System.out  <<  tmpl.make(model)

    View Slide

  94. Markup template engine — includes
    33
    //  include  another  template  
    include  template:  'foo.tpl'  
           
    //  include  raw  content  
    include  unescaped:  'raw.txt'  
    !
    //  escape  &  include  
    include  escaped:  'to_escape.txt'

    View Slide

  95. Markup template engine — static!
    • Type-checked templates available

    • use createTypeCheckedModelTemplate() 

    instead of createTemplate()

    !
    • Advantages

    • get compilation errors

    o if a variable is not available

    o if you make mistakes in the code snippets

    • even faster templates
    34

    View Slide

  96. Markup template engine — static!
    • With typed check model
    creation method

    !
    !
    !
    !
    !
    !
    !
    !
    !
    • Or declare your model types
    in the template
    35

    View Slide

  97. Markup template engine — static!
    • With typed check model
    creation method

    !
    !
    !
    !
    !
    !
    !
    !
    !
    • Or declare your model types
    in the template
    35
    def  modelTypes  =  [cars:  "List"]  
    !
    def  tmpl  =  engine.  
       createTypeCheckedModelTemplate(

               "page.tpl",  modelTypes)

    View Slide

  98. Markup template engine — static!
    • With typed check model
    creation method

    !
    !
    !
    !
    !
    !
    !
    !
    !
    • Or declare your model types
    in the template
    35
    def  modelTypes  =  [cars:  "List"]  
    !
    def  tmpl  =  engine.  
       createTypeCheckedModelTemplate(

               "page.tpl",  modelTypes)
    modelTypes  =  {  
           List  cars  
    }  
    !
    cars.each  {  car  -­‐>  
           p("Car  name:  $car.name")  
    }

    View Slide

  99. Markup template engine — static!
    • With typed check model
    creation method

    !
    !
    !
    !
    !
    !
    !
    !
    !
    • Or declare your model types
    in the template
    35
    def  modelTypes  =  [cars:  "List"]  
    !
    def  tmpl  =  engine.  
       createTypeCheckedModelTemplate(

               "page.tpl",  modelTypes)
    modelTypes  =  {  
           List  cars  
    }  
    !
    cars.each  {  car  -­‐>  
           p("Car  name:  $car.name")  
    }
    Works with
    createTemplate() too

    View Slide

  100. Markup template engine — static!
    • With typed check model
    creation method

    !
    !
    !
    !
    !
    !
    !
    !
    !
    • Or declare your model types
    in the template
    35
    def  modelTypes  =  [cars:  "List"]  
    !
    def  tmpl  =  engine.  
       createTypeCheckedModelTemplate(

               "page.tpl",  modelTypes)
    modelTypes  =  {  
           List  cars  
    }  
    !
    cars.each  {  car  -­‐>  
           p("Car  name:  $car.name")  
    }

    View Slide

  101. @glaforge — Groovy in 2014 and beyond
    Documentation overhaul

    View Slide

  102. GroovyDoc
    37

    View Slide

  103. GroovyDoc
    37

    View Slide

  104. GroovyDoc
    37

    View Slide

  105. Groovy GDK documentation
    38

    View Slide

  106. Groovy GDK documentation
    38

    View Slide

  107. Brand new documentation
    39

    View Slide

  108. @glaforge — Groovy in 2014 and beyond
    Groovy 2.4

    View Slide

  109. Beta groovy-lang.org website
    41

    View Slide

  110. Beta groovy-lang.org website
    41

    View Slide

  111. Beta groovy-lang.org website
    41

    View Slide

  112. Beta groovy-lang.org website
    41
    Notice the « improve this
    doc » button!

    View Slide

  113. Android support
    • You can use Groovy to code Android apps!

    • use Groovy 2.4.0-beta-1+

    • prefer @CompileStatic

    !
    • Two great posts to get started:

    • http://melix.github.io/blog/2014/06/grooid.html

    • http://melix.github.io/blog/2014/06/grooid2.html
    42

    View Slide

  114. New York Times — Getting Groovy with Android
    • New York Times Getting Groovy with Reactive Android
    43

    View Slide

  115. New York Times — Getting Groovy with Android
    • New York Times Getting Groovy with Reactive Android
    43
    http://bit.ly/nyt-groovy

    View Slide

  116. Android support
    44

    View Slide

  117. Android support
    44
    Source code available:
    https://github.com/melix/gr8confagenda

    View Slide

  118. Groovy Macros
    !
    • Sergei Egorov wants to contribute a macro module

    • https://github.com/groovy/groovy-core/pull/470

    !
    • Simplify creation of AST transformations

    • less boilerplate manipulating the Groovy AST API

    • more powerful and less limited than AstBuilder
    45

    View Slide

  119. Groovy Macros
    !
    • Authoring AST transformations can be verbose:
    46
    def  someVariable  =  new  ConstantExpression("xyz")  
    def  returnStatement  =  new  ReturnStatement(  
           new  ConstructorCallExpression(  
                   ClassHelper.make(SomeCoolClass),  
                   new  ArgumentListExpression(someVariable)  
           )  
    )

    View Slide

  120. Groovy Macros
    • With Groovy Macros, it could be simpler:
    47
    def  someVariable  =  macro  {  "xyz"  }  
    def  returnStatement  =  macro  {    
           new  SomeCoolClass($v{  someVariable  })    
    }

    View Slide

  121. Groovy Macros
    • With Groovy Macros, it could be simpler:
    47
    def  someVariable  =  macro  {  "xyz"  }  
    def  returnStatement  =  macro  {    
           new  SomeCoolClass($v{  someVariable  })    
    }
    Special « macro » command

    View Slide

  122. Groovy Macros
    • With Groovy Macros, it could be simpler:
    47
    def  someVariable  =  macro  {  "xyz"  }  
    def  returnStatement  =  macro  {    
           new  SomeCoolClass($v{  someVariable  })    
    }
    Special « macro » command
    Quasi-quotation

    View Slide

  123. @glaforge — Groovy in 2014 and beyond
    Groovy 3.0

    View Slide

  124. View Slide

  125. @glaforge — Groovy in 2014 and beyond
    New Age
    Meta-Object Protocol
    MOP 2

    View Slide

  126. Goals for the new MOP
    • Leverage & build upon JDK 7+ invoke dynamic

    • get Java-like performance even for dynamic code

    • Rationalize the sedimentation of meta-programming

    • more coherence, less corner cases & inconsistencies

    • Provide a notion of « realm »

    • shield users of « monkey patching »

    • finer-grained control of meta-programming reach

    • Private visibility anyone?
    51

    View Slide

  127. @glaforge — Groovy in 2014 and beyond
    Rewriting the
    Groovy grammar
    with Antlr v4
    Antlr v4 Grammar

    View Slide

  128. Antlr v4 grammar
    • Problems
    • Groovy still uses Antlr v2!

    o but version 3 and 4 are out

    • Groovy’s grammar evolved from a Java grammar

    o harder to fix and evolve, especially with Antlr v2

    • Advantages
    • Start from a clean slate

    • Antlr 4 more tolerant 

    and powerful regarding ambiguities

    • Time to clean some grammar & syntax warts!

    • Need to implement the Java 8 constructs!
    53

    View Slide

  129. Antlr v4 grammar
    • Problems
    • Groovy still uses Antlr v2!

    o but version 3 and 4 are out

    • Groovy’s grammar evolved from a Java grammar

    o harder to fix and evolve, especially with Antlr v2

    • Advantages
    • Start from a clean slate

    • Antlr 4 more tolerant 

    and powerful regarding ambiguities

    • Time to clean some grammar & syntax warts!

    • Need to implement the Java 8 constructs!
    53
    A « Google Summer of Code »
    student helped kick start it

    View Slide

  130. @glaforge — Groovy in 2014 and beyond
    Support the
    new Java 8
    language features
    Java 8 language support

    View Slide

  131. Java 8 support
    • Additional grammar & semantic features to support

    • to keep saying Groovy / Java interoperability is awesome!

    • New in Java 8

    • lambdas

    • method references

    • default methods in interfaces

    • stream API, date / time API

    • annotations on types & repeated annotations
    55

    View Slide

  132. Java 8 support
    • Additional grammar & semantic features to support

    • to keep saying Groovy / Java interoperability is awesome!

    • New in Java 8

    • lambdas

    • method references

    • default methods in interfaces

    • stream API, date / time API

    • annotations on types & repeated annotations
    55
    Groovy had already:
    closures, method pointers, mixins,
    enriched collection & time APIs

    View Slide

  133. @glaforge — Groovy in 2014 and beyond
    Summary

    View Slide

  134. View Slide

  135. Groovy rocks the JVM 

    since 2003!

    View Slide

  136. @glaforge — Groovy in 2014 and beyond
    Q & A

    View Slide

  137. View Slide

  138. Image credits
    • Big rock
    • http://wallpaper.preview-reviews.com/12852-red-rocks-in-a-snowstorm

    • Android robot
    • http://crackberry.com/sites/crackberry.com/files/styles/large/public/topic_images/2013/ANDROID.png?itok=xhm7jaxS

    • Modern MOP
    • http://i933.photobucket.com/albums/ad179/autobin/Wonder%20Mop/wondermop4.jpg

    • Jason
    • http://static.comicvine.com/uploads/original/3/32405/1031312-jason_19_inch_figure_l.jpg

    • Jigsaw
    • http://www.psdgraphics.com/file/psd-jigsaw-icon.jpg

    • Many thanks
    • http://www.trys.ie/wp-content/uploads/2013/06/many-thanks.jpg
    60

    View Slide