Release notes for Groovy 2.4

Android Support

With Groovy 2.4, you can write Android applications in Groovy!

A quick getting started guide is available on the Groovy website.

To build your Android applications with the Groovy support, you’ll be able to use the Gradle Groovy Android plugin.

The SwissKnife library builds upon the Groovy support to offer very useful AST transformations that kill the usual Android boilerplate code, for instance for dealing with UI events, with logic to be run in background threads, or make objects easily "parcelables", etc.

To further understand the new Android support, you can read the following articles by Cédric Champeau:

And discover presentations on the Android support:

The work on the Android support also lead to various optimizations in terms of bytecode generation, as explained further down, as well as, for instance, improving the handling of overloaded setters (GROOVY-2049,GROOVY-6084, GROOVY-2500) which are frequent in the Android SDK.

Performance improvements and reduced bytecode

This new major release of Groovy has seen various improvements across the board to reduce the quantity of bytecode produced, to lower memory consumption of internal data structures, fine tune bytecode for better performance.

Here are some of the tickets related to the topic:

  • Cheaper comparison operations (GROOVY-7194)

  • Reduced memory consumption for respondsTo() (GROOVY-7178)

  • For fully statically compiled classes, MOP related generated methods are not needed (GROOVY-6990)

  • Remove unneeded inner class distributor methods when no inner classes are present (GROOVY-6993)

  • Removal of the timestamp in Groovy classes (GROOVY-6308)

  • Optimization of primitive type conversions with the as operator (GROOVY-7140)

Traits @SelfType annotation

Sometimes, it’s desired to be able to restrict a trait’s application so that it can only be applied to subclasses of a certain type. That’s what the @SelfType annotation is for (GROOVY-7134).

Here’s a concrete example of @SelfType in action.

import groovy.transform.*

class Component {
   void doSomething() {
       println "Done!"
   }
}

@SelfType(Component)
@TypeChecked
trait ComponentDecorator {
   void logAndDoSomething() {
       println "Going to do something"
       doSomething()
   }
}

class ConcreteComponent
   extends Component
   implements ComponentDecorator {}

def c = new ConcreteComponent()
c.logAndDoSomething()

The ComponentDecorator trait is calling the doSomething() method from the Component sub-class to which it will be applied. If you don’t specify the @SelfType(Component) annotation, when using static type checking or static compilation, the compiler will throw a compilation error as it wouldn’t know where the doSomething() method would be coming from. With the annotation, you instruct the compiler to figure out that this trait will only be applied to child of Component that will have that method available. @SelfType is interesting in the context of static type checking or compilation, but is not needed if your code is dynamic as the resolution will take place at runtime as usual.

GDK improvements

  • System.currentTimeSeconds() to get the current time in seconds (GROOVY-6294)

  • List#getIndices() to get a range representing the indices of the elements of the list (GROOVY-7171)

  • More collection related methods are moved to iterator-based variants to apply to all iterable collection types (GROOVY-6863) and missing methods have been added like init(), dropRight(), takeRight() (GROOVY-6867)

  • Iterable gets disjoin(), minus() and toSpreadMap() methods (GROOVY-6920)

  • Refinements and consistency for existing collection methods, leveraging iterable approaches for stream-like traversals, consistency for mutation in place vs  new collection creation, minor optimizations, etc. (GROOVY-6945)

  • New List#removeAt(index) and Collection#removeElement(Object) methods (GROOVY-6952)

  • Iterable gets a size() method like iterators (GROOVY-7085)

AST transformations

  • The @ToString transformation offers an includeSuperProperties parameter so properties from the super class are also present in the string representation (GROOVY-7161)

  • You can define the compilation phase for the @ASTTest transformation for testing your AST transformations (GROOVY-6968)

  • @Synchronized supports explicit static locks to be used by instance methods if needed (GROOVY-7030)

  • Clean up generated code for @AutoExternalizable (GROOVY-6889) and @EqualsAndHashCode (GROOVY-6893) the when using @CompileStatic

  • @Builder’s default and initializer strategies improved Java integration (GROOVY-6875)

  • @PackageScope allowed on constructors too (GROOVY-6839)

Groovysh improvements

The venerable Groovysh shell continues seeing some useful improvements:

  • Groovysh supports custom .rc and .profile scripts to be loaded on startup (GROOVY-6943)

  • completion of instanceof statements (GROOVY-7200)

  • completion of static members only displayed in a static context (GROOVY-6622)

  • completion candidates in color (GROOVY-6563)

  • with :set interpreterMode true, you can let Groovysh to let you see and use locally-defined variables after further line executions (GROOVY-6623)

  • the :load command supports file names containing spaces (GROOVY-6942)

  • make arguments and flags consistent with the groovy command and allow the launch of a script on startup passed as argument and continue execution of Groovysh (GROOVY-6754)

  • make it easier to subclass Groovysh for reuse as an embedded shell (GROOVY-6752)

Miscellaneous

  • Allow Ant targets declaration by AntBuilder without immediate execution (GROOVY-2900)

  • Make NamespaceBuilder automatically detect namespace declarations (GROOVY-6890)

  • Implement and register type checking extensions as subclasses of TypeCheckingExtension (GROOVY-6739)

  • ConfigObject overrides toString() and offers a prettyPrint() method (GROOVY-7183)

  • Improved type checking for certain GDK methods (GROOVY-6966)

  • Grape is using JCenter through HTTP first for resolving dependencies, and now HTTPS is used for better security (GROOVY-7152)

  • Parameters of @DelegatesTo and @ClosureParams are better aligned (GROOVY-6956)

  • Multiple labels are supported on the same statement (GROOVY-3298)

Breaking changes

A few issues fixed might also be considered breaking changes in some situations:

  • Malformed class names for closures in inner classes (GROOVY-5351)

  • Avoid creation of MOP methods in static compilation (GROOVY-6990)

  • Reduce memory consumption for respondsTo() (GROOVY-7178)

  • Making Groovysh more easily extendable and embeddable (GROOVY-6752)

More information

You can browse all the tickets closed for Groovy 2.4 in JIRA.