-
New Feature
-
Resolution: Unresolved
-
Major
-
Shared Groovy Libraries plugin 2.8
Jenkins 2.49
It is not possible to pass Groovy method references as parameters to other methods if the method reference points to a DSL object. The code below demonstrates that the method reference works fine with the "println" method available to all Groovy objects, but fails when the reference points to "steps.echo", where steps is the DSL object available in all Jenkinsfiles.
foo(this.&println) foo(steps.&echo) def foo(arg) { arg.call("hello") }
fails with the following:
[Pipeline] echo hello [Pipeline] End of Pipeline [BFA] Scanning build for known causes... [BFA] No failure causes found [BFA] Done. 0s hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: org.jenkinsci.plugins.workflow.cps.DSL.echo() is applicable for argument types: (java.lang.String) values: [hello]
So this fails because echo isn't technically a method. We cheat to make it behave sort of like one, but behind the scenes, echo('foo') gets magically dispatched via invokeMethod in CpsScript and then DSL (from workflow-cps to actually invoking the appropriate StepExecution class, etc...I'll leave this open for a bit to see if I can find an elegant way to cheat around this and present fake "methods" for this sort of usage, but I wouldn't get your hopes up. =)