Dynamic Groovy Pt. 1

Certainly you know that Groovy is one of the dynamic languages published in the last years. There are some languages with this approach: Ruby, JRuby, Python, PHP, JavaScript and many more.

Groovy is a little bit special if you are a Java-Developer. Groovy is built on the top of the Java-Platform. So it is easy to learn the concepts and syntax of this nice language. Aside of powerful syntax enhancements there are the dynamic approach. With the Meta Object Protocol (MOP) you are able to analyze and change every Object.

Getting all properties of an Object

Properties in Groovy are a little bit special. In difference to fields a property always have a Getter-/Setter-Method. It is very easy to get all of such properties:

def someObject = new SomeJavaObject()
    println it.name

With this codefragment you get a list of all properties of the Object someObject. The content of this list are groovy.lang.MetaBeanProperty. With this class you can

  • get and set the field
  • get and set the Getter and Setter separately
  • query for a special property

You can see in Picture 1 the Classdiagram of the MetabeanProperty class. With the help of this class you can easily analyze and change existing Groovy classes.


Picture 1: Classdiagram of groovy.lang.MetaBeanProperty

What is a MetaClass?

Every Class has some meta informations. This are informations about fields, properties and methods. Additionally it exists an invokeMethod. This method is used to call dynamically methods of Groovy Classes. The behaviour of this method is similar to the Reflection mechnism of Java (you know the package java.lang.reflect?).


Picture 2: Classstructure of groovy.lang.MetaClass

Dynamically adding method to classes

With the help of this class you are able to add new methods to an existing class. Even final (and immutable) classes could be extended:

String.metaClass.toFirstUpper << {
	delegate[0].toUpperCase() + delegate.substring(1)

This little fragment adds one method to the final class java.lang.String. After this code is executed you can access this from your¬† Groovy Code. Please attend that this only works if you are calling the additional methods from Groovy. You cannot call it from your Java Code, because the Compiler doesn’t know anything of this additonally method. But you can use reflection with a simple invokeMethod to use the dynamically added method.

In Groovy you can use the new method directly:

def myString = "thisIsASimpleString"
assert "ThisIsASimpleString" == myString.toFirstUpper()

If you use Java you need to use Reflection

String myString = "thisIsASimpleString";
Method m = String.class.getMethod("toFirstUpper", new Class[]{});
assert "ThisIsASimpleString" == m.invokeMethod(myString, new Object[]{});

Adding static methods

This works for static methods, too.

public class Customer{
   String firstName
   String lastName

Customer.metaClass.static.create << { String first, String last ->
    new Customer(firstName: first, lastName: last)

assert "Thorsten" == Customer.create("Thorsten", "Kamann").firstNam


This was Part 1 of Dynamic Groovy. In the next part we see how to add dynamically add constructors, properties, and adding methods to interfaces.