And methodName is a string with the real method name.
This works in Groovy, but not everywhere. I seem to have the most problem with this in a Groovy script as opposed to a Groovy class.
(Command pattern)
So in my script I receive some JSON data (the command) and use one of its properties for the String with the method name. If I could call the method dynamically means I don't need to write a switch statement to check the value of that property to determine which method to call. So instead I name the method the same as a value in the property.
In the following code I pass json of {"action": "listOfCountries"}
In the script I have
As you can see the closure/method has the same name as in the json. When I call it I get the MissingMethodException thrown. But if I replace the call to "$action" to listOfCountries it calls that method fine.
I can't find a solution.
I wonder can I do "$action".asClosure().call(data, socket) or something like that, that was off the top of my head, so the method might not be called call.