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

Assignment from a method doesn't work in a closure

XMLWordPrintable

      The following produces a strange array conversion when assigning a string from a method within a closure.

      def lst = [1,2,3,4];
      lst.findAll {
      try {
      a = 1 / 0
      } catch(e) {
      String msg = e.getMessage();
      }
      }
      

      Producing the exception

      groovy.lang.MissingMethodException: No signature of method: Script1.toArray() is applicable for argument types: () values: []
      Possible solutions: notify(), toString(), toString(), every()
      	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:58)
      	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:64)
      	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:54)
      	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
      	at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:155)
      	at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
      	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:123)
      	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:110)
      	at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:153)
      	at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:157)
      	at org.kohsuke.groovy.sandbox.impl.Checker$checkedCall$0.callStatic(Unknown Source)
      	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
      	at Script1$_run_closure1.doCall(Script1.groovy:6)

      The workaround is to split the declaration

      def lst = [1,2,3,4];
      lst.findAll {
          try {
              a = 1 / 0
          } catch(e) {
              String msg = ""
              msg = e.getMessage();
          }
      }
      

            Unassigned Unassigned
            pwiseman Peter Wiseman
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: