tags:

views:

43

answers:

2

How can I call a method based on the value of a string in Groovy? For example instead of

switch (val)
{
Case "one":
Obj.one()
break
Case "two":
Obj.two()
Break
}

I’d like to do something like obj.val where val contains either "one" or "two" instead of a case statement.

A: 

I'm not that familiar with Groovy, but I think that invokeMethod is what you're looking for.

Matt Ball
+5  A: 

Dynamic method invocation looks like this

obj."$val"()
Michael Borgwardt