Archive for the ‘Groovy’ Category

A little bit of housework – MOPping up

December 31, 2016 Leave a comment

in doing some basics you keep relearning some stuff so here’s a quick recap on Groovy MOP.

Each groovy class has a metaClass, who’s default class is groovy.lang.MetaClassImpl.  If you set a property on the metaClass groovy changes this to be ExpandoMetaClass.  Normally calls to methods on your class are normally redirected to the metaClass to call the methods on your class using the invokeMethod call on the metaClass

Using the metaClass of your class you can lookup the methods on your class  like this

class MyIntercepted {

    def myMethods () {
        println "methods fom meta class"
        int total = 0
        def list = this.getMetaClass().getMethods().each {total++; println "$"}
        println "total methods found : $total \n"

    def myMetaMethods () {
        println "list of meta methods from meta class"
        int total = 0
        def list = this.getMetaClass().getMetaMethods().each {total++; println "$"}
        println "total of metaMethods found : $total \n"

    //has higher priority than invokeMethod
    /*def methodMissing (String name, args) {
        println "missing method: method $name ($args) was called by methodMissing"

    //will be called if no method can be matched
    def invokeMethod (String name, args) {
        println "metaclass is of type " + this.getMetaClass().getClass()
        println "method $name ($args) was called intercepted by invokeMethod"

// and use it 
MyIntercepted mi = new MyIntercepted()

mi.myMethods()       //class method exists
mi.myMetaMethods()   //class method exists
mi.smoke()           //class method doesn't exist 


calling myMethods() lists out the basic class methods found via the metaClass including those of GroovyObject, and my two class methods.

calling the myMetaMethods() will list out the broader list of metaMethods that Groovy embellishes each class with with via the GDK, which includes the “println” method.

if you implement def invokeMethod (String name, args), as shown (and you have to put the String type to name to get right dynamic dispatch), then when you call the nonexistent smoke(), method then Groovy calls the invokeMethod call where you can see the trace wriiten to your console.

if you uncomment the def methodMissing() this has higher priority and so this methodMissing() is called when the mi.smoke() method is called.

if you change your class to implement the marker interface GroovyInterceptable like this, then groovy intercepts all calls on the class and directs them to the invokeMethod call, including metaMethods such as println. Because of this you can’t use println as this is intercepted as wll and go into stackoverflow loop. you have to use the System.out.println to get trace output.

class MyIntercepted implements GroovyInterceptable {

    def myMethods () {
        println "methods from meta class"
        int total = 0
        def list = this.getMetaClass().getMethods().each {total++; println "$"}
        println "total methods found : $total \n"

    def myMetaMethods () {
        println "list of meta methods fom meta class"
        int total = 0
        def list = this.getMetaClass().getMetaMethods().each {total++; println "$"}
        println "total of metaMethods found : $total \n"

    //has higher priority than invokeMethod
    def methodMissing (String name, args) {
        println "missing method: method $name ($args) was called by methodMissing"

    //will be called if no method can be matched
    def invokeMethod (String name, args) {
        System.out.println "method $name ($args) was called intercepted by invokeMethod"

So how do call method if you have intercepted all the calls? You lookup the method and if it exists you can use the method.invoke(this, args) on it

    //will be called if no method can be matched
    def invokeMethod (String name, args) {
        //System.out.println "metaclass is of type " + this.getMetaClass().getClass()
        System.out.println "method $name ($args) was called intercepted by invokeMethod"
        MyIntercepted.metaClass.invokeMethod(this, 'println', "invoked println from metaClass.invokeMethod ")
        MetaMethod method = MyIntercepted.metaClass.getMetaMethod("hello")
        if (method) {
            System.out.println "looked up hello metamethod and tried to call it "
            method.invoke(this, "\tmethod.invoke: invoked hello metamethod")
            method.invokeMethod('println', "\tmethod.invokeMethod: invoked println directly from a meta method")

however you have to be careful like with println as this will again go into stack overflow

if you override the invokeMethod on the metaClass like this

MyIntercepted mi = new MyIntercepted()
mi.metaClass.invokeMethod = {String name, args -> System.out.println "used overriden metaClass invokeMethod"}

it has lower priority than a class method of same the name implemented in the class. whereas if you have a methodMissing on a class and one on the metaClass, the resolution path uses the metaClass implementation first!

Categories: Groovy Tags: ,

Quick note on groovy testing a spock

February 29, 2016 Leave a comment

Been getting to grips with gradle, groovy and spock, on a little project and couldnt figure out how to get the GroovyTestCase power assertions available to spock tests.

So eventually found it you have to add the following import

import static groovy.test.GroovyAssert.*
import static org.assertj.core.api.Assertions.*

this enables you to use the the groovy power asserts (shouldfail, assertFalse, etc ), the other is a fluent assertion library that has some interesting features – you need to update your compile dependencies to include ‘org.assertj:assertj-core:3.0.0’

I have not played with the Hamcrest matchers at this point but probably worth a look as these with Spock too

If you want Gradle to find and run your tests i’ve found its easiest to end your class file nameTest.groovy then you can run single shot tests for example like this

gradle -Dtest.single=DynamicRuleSpecification test

which will run the DynamicRuleSpecificationTest.groovy in your ‘src/test/groovy’ folder

Happy testing

Categories: gradle, Groovy, Testing Tags: ,

silly problems with Spring @Value ‘${}’ with IoC injection

February 23, 2016 Leave a comment

hit some basic problems with Spring injection today worth noting for others

I was trying out @Value injection today in spring and hit some basic issues

1. when you use @Value in groovy and spring – despite the fact that you might want to use a ${} expression, you cant wrap this in “” which you might expect. The Groovy compiler gets in a huff and declares the the value is not a String (technically its a GString in groovy).

to make this work in groovy you have write something like (note single ”)

@Value ('${}')

This is counter intuitive if you are used to the normal “” Gstring interpolation in Groovy. To make it work with “” you have to try

@Value ("${}".toString())

or just use the ” as previously shown.

2. second issue: if injecting values from in root of your classpath , then note that the properties values in that file default to string, so `x.y.z=”hi”` will return a string including the quotes, which is not what you expect. just leave the strings unadorned to inject correctly.

3. then, if you use param injection alone like this

void setHost ( @Value ('${}') String host)

then you get a null injection (not sure why). To fix this, either

a) move the

@Value ('${}'

to method level, or

b) use @autowired at method level and leave the

@Value ('${}')

at parameter level as well,  to get the injection to occur.

It’s these small nuances that take so much time, hopefully someone else will benefit and save a couple of hours.

4. Lastly, you can lookup properties using the Spring Environment like this. Just use the @Autowired on method or constructor, and do the lookup /type conversion using the env.getRequiredProperty call like this

class Props {

    private Map props = [:]
    boolean jmxEnabled
    String host

    @Autowired  //test inject the env
    Props (Environment env) {
        jmxEnabled = env.getRequiredProperty("org.softwood.jmx", Boolean.class)


    //format either use Autowired for method and @value on param
    // or just @value as method injection will work
    void setHost ( @Value ('${}') String host)  {

        assert host
        println "set host by injection" = host
        props[host] = host


accessing Google geocode api with groovy

February 23, 2016 Leave a comment

This is just a quick post on how you can get started with the google geocode API services from groovy

you start by visiting Google set to get an authentication key

use the ‘get a key’ button in the second section. this runs a wizard for you and you complete your details. I set up a server key for dev pc as i’m playing and that’s the registered server name i used.

At the end of this process google will generate an API key for you which you should record safely and securely. when coding read this value from a config file, etc so that you don’t encode it directly in your code.

Once setup with this you try the API out. There is a interactive javascript version of this api which i havent used – i’m using the json webservice, and there is an XML version of the service also.

The return result type has been encoded into the base URL, and not using accept headers.

first lets start with location class, that you’ll provide instance of with the address you want to get lat/long for. (you can do this in reverse as well)

class Location {
String houseNumber
String street
String city
String state
String zip
String country

double latitude
double longitude

String toString () {"$street, $city, $zip, $state, $country : latitude:$latitude, longitude:$longitude"}

i’m not persisting this put you do so readily as local cache etc.

The web service is located with a url base of

 def base = ""

what the geocoder service will do is take a location (with populated record details) and build an address parameter, by URLencoding an array of address data from the location, using the join(‘,’) as the separator.

You then creat a params array for the web service, by taking a map of the address and your server API key (there is an optional components attribute that i have not used here – see google api defn for details). you then collect and join the key=value pairs you build to get the resultant param format for the call.

you then use JsonSlurper to parse the result of the call, (assert it all went ok), and then read the lat, lng values back (google api shows you the structure of the result set) and store these back in the location instance, converting the result to Double as you do so.

class Geocoder {
    def base = ""

    void completeLatLong (Location loc){
        def addressFields =  loc.street ? [loc.street,,,loc.state,] : [,]
        def address = addressFields.collect {URLEncoder.encode (it, 'UTF-8')}.join (',')
        def params = [address:address,
                      key:<your api key here>]
        def url = base +params.collect {k,v -> "$k=$v"}.join ('&')

        def response = new JsonSlurper().parse (url.toURL())

        assert response.status == "OK"
        loc.latitude = response.results[0]
        loc.longitude = response.results[0].geometry.location.lng.toDouble ()


you can now write tests, or simple driver script to use the service (fill in your own street, city, etc.

def loc = [street: "<your street>", city: "<your city>", zip: "<your zip>", state:"<your state>", country:"<your country>"] as Location

def geo = new Geocoder()
geo.completeLatLong (loc)

println loc  //should return completed lat/long from the successful service 

This should return you the loc record with the added geocoding details, which you can use elsewhere

Categories: Groovy

Groovy DSL and example notes to myself

February 22, 2016 Leave a comment

most of this stuff is already out there but i wanted to try and make sure i’d understood command chaining and other tricks

so first off we note that groovy allows your to drop ‘()’ off methods where there are multiple attributes (its still required for no arg method though)

def method1 ()
def method2 (arg1, arg2=null)
def method3 (Map map, arg1, arg2=null, ...)

method2 (someArg) //is equivalent to
method2 someArg

//also with mixed named/unamed attributes groovy collects all named
// attributes as into a 1st map attribute of the method

//these two are equivalent
method3 (address: "the house", 0143 222222, street: "south close")
method3 address: "the house", street: "south close", 0143 222222

Also if your function returns itself or another function (or a closure) then the dot invocation is optional, for the compiler can read this when we drop the ‘.’

def method4 () {
    println "hello"
return this
method4().method4 () //is equivalent to 
method4() method4()

so this gives rise to some interesting examples of using groovy to work to build
DSL facilities.

so we’ll build an example up as we go of a groovy script to show some of the options, using a simple robot like DSL which can go in some direction, travel some distance, at some speed.

first i added a couple of static imports for enums that come later in the script

import static Direction.*
import static DistanceUnit.*
import groovy.transform.TupleConstructor

i then defined a couple of enums for a DistanceUnit and Duration as we are going
to embellish Numbers with some additional method facilities

//using static import to get 'left' and 'right' direction  enums -
//see static imports above
enum Direction {
    forward, backward, left, right

enum DistanceUnit {
 centimeters ('cm', 0.01),
 meters ('m', 1),
 kilometers ('km', 1000)

 String abbreviation
 double multiplier

 DistanceUnit (String abbr, double mult) {
 this.abbreviation = abbr
 this.multiplier = mult}

enum Duration {
    millisecond ('ms', 0.001),
    second ('s', 1),
    hour ('h', 60),
    day ('d', 60*24)

    String abbreviation
    double multiplier

    Duration (String abbr, double mult) {
        this.abbreviation = abbr
        this.multiplier = mult}

//define a symbol for hour in script, used when getting a speed = dist/dur
def h = Duration.hour

We the want to create a couple of classes for Speed and Distance, but use the @TupleConstructor annotation so that we can avoid having to type ‘new’ when we wish to create some instances of these classes. Also as we wish to have something like ‘’ we need to overload the div operator for the Distance class and return a Speed from this operation.

//avoid having to use new in declaration
class Distance {
    Number amount
    DistanceUnit unit

    //added div overload so that we can have distance over unit of time
    Speed div (Duration dur) {
        new Speed (amount, unit, Duration.hour)

    String toString () {"$amount $unit"}
class Speed {
    Number amount
    DistanceUnit unit
    Duration duration

    String toString () {"$amount $unit/$duration.abbreviation"}

Now through metaprogramming we can embellish Numbers with some methods to get a Distance like this

//add new meta method to Number- return the number (delegate) as instance of Distance
//global change to Number though - probably want to limit this using categories but forces us to inject
//and use groovy shell evaluate to do the assessment
Number.metaClass.getMeters = {new Distance (delegate, DistanceUnit.meters)}

However this is a global change to Number, and we probably wish to limit the scope of these methods, so lets define a Category instead like this

//limit the scope of added methods to use closure
class DistanceCategory {
    static Distance getCentimetres (Number num) {
        new Distance (num, DistanceUnit.centimeters)

    static Distance getMeters (Number num) {
        new Distance (num, DistanceUnit.meters)

    static Distance getKilometers (Number num) {
        new Distance (num, DistanceUnit.kilometers)

    //short form
    static getCm (Number num) {

    static getM (Number num) {

    static getKm (Number num) {
        getKilometers (num)

So using the above our first go for a DSL might be to be to define a function like this

def move1 (Direction dir, Distance d) {
        println "move1: moving $dir by $d"

//works because 3.meters calls the new metamethod to return instance 
//of Distance.  
//wrap in 'use' closure if using category class
move1 left, 3.meters

thats not too bad, we have removed the parentheses, and invoked the getMeters()
metamethod to return a Distance.

This will print ‘move1: moving left by 3 meters’ on the console

we can get a bit more readable by defining our move method like this. Ive used
the category class this time, not the global addition of meta method to Number.

//when a method call is made with a mix of named and no named params, then grooy puts all
//the named params into a map - as first param, and the rest as unnamed param in the order seen
//so define a new move like this

def move2 (Map m, Direction dir) {
    println "move2: moving $dir by $ at $"

//use the move DSL and Category to add metamethods
use (DistanceCategory) {
move2 right, by: 3.meters, at: / h

This works as groovy collects the named parameters and drops them into the map,
and provides the remaining attributes, after that.

This writes ‘move2: moving right by 3 meters at 5 kilometers/h’ to the console.

However it would be nice to drop the ‘,’ and the : in the maps to make this more
readable. so here is the last trick using command chains (return a callable class – here a closure for next call)

//using command chains now
def move3 (Direction dir) {
    [by: {Distance dist ->
        [at: {Speed speed ->
            println "move3: moving $dir by $dist, at $speed"

use (DistanceCategory) {
    move3 right by 3.meters at / h
    //is the same as
    move3 (right).by (3.meters).at(

now when you provide the direction to the move3 method, it returns a map with key ‘by’ and a single closure as its value which is invoked with 3.meters. This
immediately returns another map with key ‘at’, and another closure which is
invoked with the 5km/h (using the overloaded Distance class to pass in a Speed as the attribute to the closure, the ‘h’ property was defined previously in the script binding above.

This can make your head explode walking through the tautology, but if you step through what the compiler sees you should be able to ‘see’ what this does.

Lastly I tried this

def func (arg = null) {
    println "got args as " + arg ?: null
    if (!arg) {
        println "setting up no arg closure in map"
        [by : { -> println "no args closure: hello william "}()]
    else {
        println "setting up with arg closure in map"
        [by: { mapArg -> println "with args closure : $mapArg" }]

//println "calling no args on func : "
func() by

//println "calling with args on func : "
println "calling args on func : "
func("someArg") by 3

In the first case we call ‘func’ with no args, and the function definition defines a single map with key ‘by’ that has a closure with no args. as no args as passed you have to add the ‘()’ in the first map to execute the returned closure. In the second case you provide an attribute for func, which sets up a map with key ‘by’ that takes an argument. so you can trigger this with the by 3 and call that closure when it returns.

All this best done with a clear head and step each bit through at a time.

You can also provide the DSL boilerplating in one class and separate the language script in another through using groovy shell evaluate and injecting the compiler customisers, static imports and extended base script to that the evaluate method inherits all the DSL
definition parts like this

def binding = new Binding (//add some variables etc ) 

def importCustomizer = new ImportCustomizer()
importCustomizer.addStaticStars //etc 

def conf = new CompilerConfiguration()
conf.addCompilationCustomers (importCustomizer)
conf.scriptBaseClass =  //specify the script base class

def shell = new GroovyShell (this.class.classLoader, binding, config)
shell.evaluate '''
      move left...
Categories: Groovy, Uncategorized Tags:

GPARS Actors and sleeping barber example

November 27, 2014 Leave a comment

started to look at the GPARS library and how to apply this into my programms – found an earlier example from Hamlet D’arcy, but it was out of date relative to latest GPARs constructs, and scheduling work at particular times

So i’ve had a go at relaunching it in new clothes. In order to simulate the waiting room i’ve used blocking queue of fixed size. Adding a customer will block when the waiting room is full – until someone (the barber takes one as his next customer)like this by sending a message to the actor

     barber << check client  //message to actor

in the second approach i've used the timer service to repeatedly schedule a message to a second barber ( not an actor ) which runs the action takes a client and reschedules itself for next ask

def barber2 = Executors.newScheduledThreadPool(1)
	     println 'Barber2: Next customer please!'
		 Customer customer = waitingRoom.take()
		 println "${} gets a haircut at ${new Date().format('H:m:s')}" }
 as Runnable, 0, 5, TimeUnit.SECONDS)

the complete groovy script solution looks like this

package com.softwood.script

import groovy.transform.Immutable

import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit

// create waiting room can't hold more than 12!  array queue of customers
def waitingRoom = new ArrayBlockingQueue(12)

 * ArrayBlockingQueue filled with customers that need a haircut. 
 * And the barber is an executor service with a scheduled task to 
 * give haircuts. The number of threads in the scheduled thread 
 * pool is 1 because there's only one barber. The barber takes 
 * customers from the waiting room and cuts their hair once every 
 * 15 minutes. The call to waitingRoom.take() is blocking... if 
 * there is a customer ready then he is serviced immediately, and 
 * if one is not, then the call blocks until someone is available. 
 * Once thing to note... the waitingRoom has a size of 12... if a 
 * 13th customer is added then the calling code will either block 
 * until there is enough room or throw an exception. There is an 
 * API to do either case.
final def group = new DefaultPGroup ()

//event driven actor model 
final def barberShop = group.reactor {message ->
	switch (message) {
		case Enter :
			println "${} enters and is waiting for a haircut"
			//add customer from message to the waiting room 
			waitingRoom.add (message.customer)
		case "quit":
			println "called quit on barbershop - now new customers can come in"

// message to post on barberShop
class Enter {
	Customer customer

//customer for barbershop
class Customer {
	String name

//create a single barber - thread pool size of 1
final DefaultPGroup barberGroup = new DefaultPGroup (1)

println "barber opening shop"
//this model auto starts the barber
final barber = {
	loop {
		react {
			println "barber> asked to -> $it"
			println "barber> Next Customer Please!"
			Customer customer = waitingRoom.take()  //blocking call on waiting room
			println "barber> ${} gets a haircut at ${new Date().format('H:m:s')}"

def barber2 = Executors.newScheduledThreadPool(1)
	     println 'Barber2: Next customer please!'
		 Customer customer = waitingRoom.take()
		 println "${} gets a haircut at ${new Date().format('H:m:s')}" }
 as Runnable, 0, 5, TimeUnit.SECONDS)

//add some customers to waiting room 
barberShop << new Enter(customer: new Customer(name: 'Jerry'))
barberShop << new Enter(customer: new Customer(name: 'Phil'))
barberShop << new Enter(customer: new Customer(name: 'Bob'))
barberShop << new Enter(customer: new Customer(name: 'Ron'))
barberShop << "quit"

//send 'message' to first barber 
barber << "check client  "

barber.join (20, TimeUnit.SECONDS)
println "customers in queue ${waitingRoom.size()}"
println "barber closing shop"

i’ve used @ immutable to construct a a thread safe message with thread safe ‘customer’ to pass to the waiting room

Categories: GPARS, Groovy Tags: , , ,

Groovy 2 asType conversation

August 13, 2014 Leave a comment

Groovy enables you to provide overload a method “asType (Class clazz)”. However i had some difficulty with this relating to duck typing which didn’t work. Not entirely sure why.

i’d been trying to use a grails app with joda date – LocalDate, and new java8 LocalDate. however the java8 version kept crashing when i ran a test – so i dropped back to java 7 and Joda package.

Hibernate doesn’t seem to handle Joda/Java LocalDates (havn’t tried this very far). So i kept it simple i created a Birthday Domain Class with three int params (year, month,date) and wanted to ensure i could generate a LocalDate from this.

first attempt was try this using duck typing

	Object asType (clazz) {
		if (clazz == LocalDate) {
			new LocalDate (year, month, day)
		else {


however this wouldn’t fire when i tried

def bday = new Birthday (year:1962, month:10, day:20) as LocalDate

and it said it couldn’t handle the caste at runtime – odd.

eventually tried and explicitly set the type for the method and then it ran successfully and did the dynamic caste

	Object asType (<strong>Class</strong> clazz) {
		if (clazz == LocalDate) {
			new LocalDate (year, month, day)
		else {


adding the explicit type did it

Categories: Grails, Groovy Tags:
%d bloggers like this: