In groovy, is there a way to check if an object has a given method?

Viewed 19336

Assuming that I have an object someObj of indeterminate type, I'd like to do something like:

def value = someObj.someMethod()

Where there's no guarantee that 'someObj' implements the someMethod() method, and if it doesn't, just return null.

Is there anything like that in Groovy, or do I need to wrap that in an if-statement with an instanceof check?

6 Answers

In very concise way you can use this:

if(someObj.&methodName){
 //it means someObj has the method
}
Related