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:

Java

1

2

3

4

@NamedVariant

StringfullName(StringlastName,@NamedParamStringfirstName){

"$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

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

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

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

Java

1

value=value?:'Some Default Value'

you can simply do:

Java

1

value?='Some Default Value'

For example:

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.

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.

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!