Tuesday 2 March 2010

When is a string not a string - when it is groovy :)

I have some groovy code which basically populates a map and then some code later on which gets the value out of a map.

The keys are strings. Should be simple right?

The problem is that is you use groovy's excellent string facilities (i.e. "this is a string with a ${parameter}") then no, you don't have a java.lang.String you get a org.....GStringImpl.

The code that was inserting into the map was using this feature, the code that was retrieving it was using a plain java.lang.String. The solution was to cast the key to a string (actually no need to cast; String s = "this is a string with a ${parameter}" works fine).

Stupid that GStringImpl.equals doesn't take this into account.....