In Groovy we can add a method named
call to a class and then invoke the method without using the name
call. We would simply just type the parentheses and optional arguments on an object instance. Groovy calls this the call operator:
(). This can be especially useful in for example a DSL written with Groovy. We can add multiple
call methods to our class each with different arguments. The correct method is invoked at runtime based on the arguments.
To create JSON output with Groovy is easy using JsonBuilder and StreamingJsonBuilder. In the samples mentioned in the links we create a JSON object with a key and values. But what if we want to create JSON with a root JSON array using
StreamingJsonBuilder? It turns out to be very simple by passing a list of values using the constructor or using the implicit method
Since Groovy 2.4.8 we can use the
uncapitalize method on
CharSequence objects. The
capitalize method was already available for a long time, but now we have the opposite as well.
In the following example we see that the
uncapitalize method only replaces the first letter of a
String value to lower case:
assert 'Groovy'.uncapitalize() == 'groovy'
assert 'MrHaki'.uncapitalize() == 'mrHaki'
String message = 'Groovy Rocks!'
assert message.uncapitalize() == 'groovy Rocks!'
Written with Groovy 2.4.8.
Original blog post
In functional programming we have the concept of an identity function. An identity function returns the same result as the input of the function. Groovy has a lot of functional paradigms including a identity function. Of course in Groovy’s case it is an identity closure. It is defined as a constant in the
Closure.IDENTITY. If we use this closure we get the same result as the argument we provide.
Groovy adds a lot of useful methods to the Java JDK classes. One of them is the
sleep method that is added to all objects. With the
sleep method we can add a pause to our code. The
sleep method accepts a sleep time in milli seconds. The implementation of the method will always wait for he given amount of milli seconds even if interrupted. But we can add a closure as extra argument, which is invoked when the
sleep method is interrupted. We should return
true for the closure to really interrupt, otherwise we use
When we use the property syntax of Groovy to get the value for a property, Groovy will actually try to invoke a
get method for that property if it is available. So for example if we have the statement
user.getName() is invoked. If we want to reference a property field directly, so bypassing the
get method, we must place an
@ in front of the property field name. In the previous example we would write
user.@name to get the field value directly. The same rules apply for setting a value for a property with the Groovy syntax. If we write
user.name = 'mrhaki' then actually
user.setName('mrhaki') is invoked. We can use the
@ prefix also to set a value without invoking the
set method for that property. So in our example it would be
user.@name = 'mrhaki' and the
setName method is not used.
We have many ways to provide configuration properties to a Spring (Boot) application. We can add our own custom configuration properties format. For example we can use Groovy’s
ConfigObject object to set configuration properties. We need to read a configuration file using
ConfigSlurper and make it available as a property source for Spring. We need to implement two classes and add configuration file to support a Groovy configuration file in a Spring application.
Adding logging support to a class in Groovy is easy. We can choose to add SLF4J, Log4j, Log4j2, Apache Commons or Java Util Logging to our class. The default implementation of the Abstract Syntax Tree (AST) transformation is to add a
log field of the correct type. As category name the complete class name (including the package) is used. We can change the name of the field with the
value attribute. To alter the category name we use the attribute
In a previous post we learned how to use the
toMapString methods. With these methods we create a String representation of a
Map object. With a bit of Groovy code we can take such a
String object and turn it into a
Groovy adds to
Map objects the
toMapString method. With this method we can have a String representation of our
Map. We can specify an argument for the maximum width of the generated
String. Groovy will make sure at least the key/value pairs are added as a pair, before adding three dots (
...) if the maximum size is exceeded.
def course = [
name: 'Groovy 101',
location: 'The Netherlands']
assert course.toMapString(15) == '[name:Groovy 101, ...]'
assert course.toMapString(25) == '[name:Groovy 101, teacher:mrhaki, ...]'
As mentioned in a previous post we can use the
toListString method to represent a
List as a
def names = ['mrhaki', 'hubert']
assert names.toListString(5) == '[mrhaki, ...]'
Written with Groovy 2.4.7.
Original blog post