MissingMethodException when adding String.metaClass.call then using it on String literal

63 Views Asked by At

I'm trying to make String/GString objects callable with something like

String.metaClass.call = { println toUpperCase() + " " + it() }

This is producing a MissingMethodException when called on literals, though not always:

def a = 'abc'

a{ 2 * 3 }     //ABC 6                     -> OK
'a'{ 2 * 4 }   //ABC 8                     -> OK
'abc'{ 2 * 5 } //MissingMethodException    -> ERROR

The error is:

groovy.lang.MissingMethodException: No signature of method: ConsoleScript11.abc() 
is applicable for argument types: (ConsoleScript11$_run_closure4) 

I'll skip the fact that 'a'{ 2 * 4 } works while 'abc'{ 2 * 5 } doesn't. My question really is: is there a way to make invocations on literals work without Groovy trying to resolve methods with quoted identifiers?

0

There are 0 best solutions below