Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-44537

Cannot pass Groovy method references as parameters for DSL objects

      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]
      

          [JENKINS-44537] Cannot pass Groovy method references as parameters for DSL objects

          Andrew Bayer added a comment -

          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. =)

          Andrew Bayer added a comment - 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. =)

            Unassigned Unassigned
            boon Joe Harte
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: