Features

Groovy Improvements in IntelliJ IDEA 2018.2

IntelliJ IDEA 2018.2 comes with support for Groovy 2.5 features, and for the current draft of the Groovy 3.0 specification (note that Groovy 3.0 is in alpha and functionality may change in the future).

Groovy 2.5

IntelliJ IDEA 2018.2 supports new features in Groovy 2.5 like the AST Transformations Annotations. If, for example, you want to call a method with named parameters (and you don’t want to declare that method as taking a Map, which is one way Groovy provides Named Arguments), you can use the @NamedParam attribute.

Consider a method like this one:

@NamedVariant
String fullName(String lastName, @NamedParam String firstName) {
    "$firstName $lastName"
}

This method can be called with two String parameters, or you can use the parameter name.  This can be particularly useful with methods that multiple parameters of the same type as it’s much clearer what each value represents.

Groovy 3.0

IntelliJ IDEA 2018.2 lets you write valid Groovy 3.0 code.  For example, Groovy 3.0 supports some classic Java syntax, like do while

Java 8 features also make it into this version of Groovy, for example now you can use method references in Groovy code.

02-method-references-2

You can also add default methods to interfaces in Groovy 3.0. These will give you a default implementation of a method if the method isn’t implemented in a subclass, or you can override the functionality as you would with any superclass.

Groovy 3.0 adds a simpler way to negate the instanceof and in operators.  Previously, you had to put the expression inside brackets and negate the whole expression, but now you simply have to put the exclamation mark in front of the keyword itself to negate it

03-not-in-instanceof

Also new is a shorthand way to assign a default value if no value is present, via the Elvis assignment shorthand ?=. Instead of having to do something like

value = value ?: 'Some Default Value'

you can simply do:

value ?= 'Some Default Value'

For example:

05-elvis

Existing versions of Groovy let you safely navigate objects that might have null values, but this was not supported for arrays.  Groovy 3.0 adds this feature for array indexes so it’s now possible to refer to an index on an array that might be null, or a value from an array that might be null, without the risk of throwing a NullPointerException.

07-safe-index

Groovy 3.0 has added the === and !== equality operators which you can use to check if two variables are actually the same object. This is the equivalent of using the is() method.

06-equality

All code examples in this blog post are either the same as, or adapted from, the examples in the current Groovy 3.0 release notes. Given that the release is still in alpha, the examples in the release notes may change, and the examples in this blog post might stop working. Groovy 3.0 is a work in progress, and as such IntelliJ IDEA’s support will also progress and possibly change.  Watch this blog for more updates!

 

image description

Discover more