tags:

views:

223

answers:

2

why does this throw an exception?

messageSource.getMessage('UserService.msg.forgot.unknown', ["[email protected]"], null)

unless I do this...

def Object[] args = ["[email protected]"]
messageSource.getMessage('UserService.msg.forgot.unknown', args, null)
+5  A: 

Because ["[email protected]"] evaluates to an ArrayList, not an array:

groovy:000> o = ["asdf"]
===> [asdf]
groovy:000> o.getClass()
===> class java.util.ArrayList

OTOH your declaration creates an array of Objects:

groovy:000>  Object[] args = ["asdf"]
===> [Ljava.lang.Object;@14e113b

and the method you're calling needs an array.

Nathan Hughes
makes sense, but seems to make the method a hassle to use, I guess I expected it to be more "groovy"
Aaron Saunders
You could change it to ["[email protected]"].toArray(). Not much better but at least it's inline.
Nathan Hughes
+1  A: 

Nathan has already (correctly) explained the reason for this behavior at the language level. I just want to move one abstraction level up: Why are you using Spring's MessageSource directly, in the first place? In Grails there is a message tag, that wraps the message source:

g.message(code: 'UserService.msg.forgot.unknown', args: ["[email protected]"])
Daniel Rinser