Apache Groovy Tidbits

Published: Feb 4, 2022
Updated: Mar 28, 2022

Lately I’ve been dabbling in JSL (Jenkins Shared Libraries). The JSL is usually written in Groovy, so I thought it’d be interesting to read the Groovy Language Documentation. Here are some of the tidbits I learned:

List Literal #

An empty list:

def list = []

A populated list:

def list = ['a', 'b']

Map Literal #

An empty map:

def map = [:]

A populated map:

def map = [firstName: 'Jane', lastName: 'Doe']

Spread List Elements #

def list1 = ['a', 'b']
def list2 = ['c', 'd']
def list3 = [*list1, *list2]

assert ['a', 'b', 'c', 'd'] == list3

Spread Map Elements #

def map1 = [firstName: 'Jane']
def map2 = [lastName: 'Doe']
def map3 = [*:map1, *:map2]

assert [firstName: 'Jane', lastName: 'Doe'] == map3

Expando #

From the docs:

The Expando class can be used to create a dynamically expandable object.

You can add any field or method you want:

def expando = new Expando()

expando.name = 'Jane'
expando.add = { a, b -> a + b }

assert 'Jane' == expando.name
assert 4 == expando.add(2, 2)

Elvis Operator #

It’s like a shortened ternary when you want a sensible default.

This:

def map = [:]

assert 'Unknown' == map.name ? map.name : 'Unknown'

Is the same as this:

def map = [:]

assert 'Unknown' == map.name ?: 'Unknown'

Safe Navigation Operator #

This throws a NullPointerException:

def map = null

assert null == map.name

This doesn’t:

def map = null

assert null == map?.name

Safe Index Operator #

This throws a NullPointerException:

def list = null

assert null == list[0]

This doesn’t:

def list = null

assert null == list?[0]

Truth #

Use !! (bang bang boolean) to coerce an expression to a boolean:

assert false == !! []
assert false == !! [:]
assert false == !! 0
assert false == !! ''
assert false == !! null

assert true == !! ['a', 'b']
assert true == !! [firstName: 'Jane']
assert true == !! 1
assert true == !! 'Jane'
assert true == !! new Expando()