Named parameters compilation fails with @CompileStatic - how to fix it?

158 Views Asked by At

This piece of code works perfectly in dynamically typed Groovy

buildDirectory.traverse(type: FILES, nameFilter: ~/dependency-updates-report.xml/) {
    reports << it
}

but when adding @CompileStatic to the class it get an error, something along the line

'traverse' in 'org.codehaus.groovy.runtime.ResourceGroovyMethods' cannot be applied to ...

Is the Map parameter the problem here?

EDIT buildDirectory is of type java.io.File and is injected.

1

There are 1 best solutions below

0
On BEST ANSWER

In case of a static compilation like that one, you need to make an explicit cast to:

buildDirectory.traverse([type: FILES, nameFilter: ~/dependency-updates-report.xml/] as Map) {
    reports << it
}

The problem here is that File.traverse() method expects a map of type

Map<String, Object>

while the Groovy idiomatic map creation creates a map of type

LinkedHashMap<String, Serializable>

This Serializable is a problem here because it is not a subclass of the java.lang.Object.