Reputation: 391
I have 2 method which get the same params:
void removeJob(def user,def company,String job){}
void addJob(def user,def company,String job){}
I want to use the invokeMethod() but I'm not sure how. I try:
def methodName = selectedChange[2]=='true'?'add':'remove'
roleService.invokeMethod(methodName+'Job',[userSelected, selectedCompany,selectedChange[1]])
But I got errors
Upvotes: 0
Views: 41
Reputation: 75681
You can call the method dynamically using a GString for the method name:
String methodName = selectedChange[2] == 'true' ? 'add' : 'remove'
roleService."${methodName + 'Job'}"(userSelected, selectedCompany, selectedChange[1])
This also works with properties, e.g.
String propertyName = 'fooCount'
int count = person."$propertyName"
Upvotes: 1