Category Archives: Java

Book Review: Making Java Groovy

kousen_cover150If you treat a book like an investment this is a good choice both in terms of money and time. Groovy differs from another JVM languages which try to compete with Java like Scala. Groovy approach is quite different. It is not replacement, it fulfils shortages and makes cumbersome stuff in Java code straightforward.

If you have a background in other languages like Python, Ruby, Perl. Java could sometimes be source of frustration. If you came up with thought I would like to write this in one aforementioned languages and you have to unfortunately write in Java, Groovy is answer for your desire. Groovy takes best from said languages. The same in case of some common tasks like parsing or creating xml you have to write a lot of boilerplate code in Java. Next example, if functional programming made impression on you, for example dealing with collections in Scala, Groovy gives you similar capabilities. And syntax in Groovy is much more natural.

First I have to mention that before I’ve read Making Java Groovy, I read Groovy in Action second edition, which is great and will be available from Manning very soon. I read early version so called MEAP and when I noticed Making Java Groovy my first thought was hmm, yet another Groovy book. But I started to read it. It appeared that this book is much different than GiA. While GiA is great Groovy reference and manual, Making Java Groovy is different.

If you are experienced Java developer who is seeking how to make your life easier, write less code and be more efficient and you don’t afraid to learn new stuff. This book is for you. Ken Kousen did a good job. I think that his style of writing was inspired with his lecturer and couch experience. So topics are light, you got a lot of simple but real life and useful examples. Language is not dry and monotonous. Author has good sense of humour. Everything is in order not to make reader bored or reluctant. He doesn’t explain every detail of so called Groovy magic but gives a reader taste what she/he can obtain from it. So for instance he mentioned few of AST transformations and if you feel a power of it for sure you would like to read GiA.

Book is quite good introduction to Groovy basics. PART 1 shows groovy basics in concise manner. I think it could be kind of eye opener for average Java developer with typical examples like dealing with XML, json, constructing URLs using closures. That can be done in concise and elegant form and still it can be integral part of Java project.

PART2 shows 2 typical fields in which Groovy beats everything else. Namely Unit Testing and building project. Throughout whole book author uses Gradle to build project which is far modern alternative to Maven and Ant. Moreover he uses frequently powerful BDD testing tool Spock.

My favourite part of book is PART 3: GROOVY IN THE REAL WORLD. Among others it explains how Groovy can make Spring developer life easier. It clearly shows real life examples that without using a lot of boilerplate code, reflection and substantial effort as in case of pure Java, you can get from Spring much more writing clear and simple code.

If you read this book I’m sure that you will start to write some parts of your code in Groovy and become much more productive. And this is what Groovy was design for, to fulfil lacks of Java, and add convenience tools for agile developer.

How to obtain class name within it’s sources in Java 7

When we implement a class there is frequently need to obtain class name which we currently develop. This is especially true when we implementing logging.
Lets look on below short example:

private static final Logger LOG = Logger.getLogger(ClazzName.class, Logger.NF);

The result as we see is that we need to every time on every class which is supposed to do logging enter a class name.
This is cumbersome and more over error prone. Solution thanks to introduction in Java 7 method handles is fairy simple, let’s look
and next example:

private static final Logger LOG = Logger.getLogger(MethodHandles.lookup().lookupClass(), Logger.NF);