How to invoke instance methods from within a closure?


(Abhijit Sarkar) #1

I’ve a method as shown below that calls another instance method from the same class. However, the build fails with MissingMethodException unless I make the later method static. Is there a way not to have to do that?

private List<String> myMethod() {
    wsdlFiles.each { wsdlURL ->
        wsdlUrls.add(MyClass.getURLAsString(new File(wsdlDir, wsdlURL)))
    }
    wsdlUrls
}
private static String getURLAsString(File f) {
    f.toURI().toURL().toString()
}

(Peter Niederwieser) #2

Is this a build script or a class? Does it work when you make the called method non-private?


(Abhijit Sarkar) #3

It’s a class. And it works when the method is made non-private. However, I’d rather make it static than non-private due to encapsulation concerns. Thank you.


(Abhijit Sarkar) #4

It’s a class. And it works when the method is made non-private. However, I’d rather make it static than non-private due to encapsulation concerns. Thank you.


(Peter Niederwieser) #5

‘protected’ or ‘@PackageScope’ will probably work as well.