We can inspect the number and type of parameters defined for a closure very easily. A closure has the properties maximumNumberOfParameters
and parameterTypes
for this. So in our code we can ask a closure how many parameters are expected and even which type the parameters are.
// Two simple closure with one and two parameters. def one = { it.toUpperCase() } def two = { String s, upper -> if (upper) { s.toUpperCase() } else { s.toLowerCase() } } def runClosure(cl) { switch (cl.maximumNumberOfParameters) { case 1: assert [java.lang.Object] == cl.parameterTypes cl.call('Groovy') break case 2: assert [java.lang.String, java.lang.Object] == cl.parameterTypes cl('Groovy', false) break } } assert 'GROOVY' == runClosure(one) assert 'groovy' == runClosure(two)