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
Groovy has many AST annotations that add code to our class (the Abstract Syntax Tree – AST) before it is compiled. So the compiled class file contains the code added by the AST annotation. With the
@AutoClone annotation a
clone method is added and the class implements the
Cloneable interface. We have different strategies to choose from to support cloning for our class.
I love code. I take care of my code and I like my code to be formatted nicely. No matter if I’m on Eclipse, Netbeans or IntelliJ, I want my code to be formatted the same.
Nowadays we have EditorConfig. In the source code we can place a file
.editorconfig with formatting instructions. These instructions can be read by many Tools like Eclipse, Netbeans, IntelliJ and VisualStudio. If we create an
.editorconfig file with formatting instructions, these rules are automatically applied.
An example of an
.editorconfig file looks like:
root = true
indent_style = space
indent_size = 2
continuation_indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_size = 4
continuation_indent_size = 4
As you see you can define specific formatting rules for different file types.
Happy coding and formatting!
When we write code with a lot of method chaining that involves closures and the use the Reformat code feature of IntelliJ IDEA things might get screwed up. Luckily we can changes some Groovy formatting rules in IntelliJ IDEA to prevent the reformatting.