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

Global library class calling another global libraries static method fails

XMLWordPrintable

      I ran into a strange issue with with a global library using the legacy way of the Git repository in the workflowLibs folder.
      When calling another class' static method in the Global library (in this fashion at least) caused the execution to fail.
      I was trying to write behavior similar to the Checks.checkArg(boolean argument) method.
      I think an example will help explain at least what I ran into.

      Code

      com.mkobit.MyClass

      package com.mkobit
      
      class MyClass implements Serializable {
        final String myVar
      
        MyClass(final String myVar) {
          this.myVar = Objects.requireNonNull(myVar)
        }
      
        MyClass(final boolean arg) {
          Checks.checkArg(arg)
          this.myVar = 'got through'
        }
      }
      
      

      com.mkobit.MyClassFactory

      package com.mkobit
      
      class MyClassFactory {
        static MyClass create() {
          new MyClass(null)
        }
      }
      
      

      com.mkobit.Checks

      package com.mkobit
      
      class Checks {
      
        private Checks() {}
      
        static void checkArg(boolean arg) {
          if (!arg) {
            throw new IllegalArgumentException()
          }
        }
      }
      

      Pipeline

      import com.mkobit.MyClass
      import com.mkobit.MyClassFactory
      import com.mkobit.Checks
      
      try {
          new MyClass(null)
      } catch (NullPointerException npe) {
          echo 'NPE caught for single arg constructor'
      }
      
      try {
          Checks.checkArg(false)
      } catch (IllegalArgumentException iae) {
          echo 'IAE caught for static call'
      }
      
      try {
          MyClassFactory.create()
      } catch (NullPointerException npe) {
          echo 'NPE caught for other class factory method'
      }
      
      try {
          new MyClass(false)
      } catch (IllegalArgumentException iae) {
          echo 'IAR caught for Checks'
      }
      

      Output from execution

      Started by user a
      [Pipeline] echo
      NPE caught for single arg constructor
      [Pipeline] echo
      IAE caught for static call
      [Pipeline] echo
      NPE caught for other class factory method
      [Pipeline] End of Pipeline
      com.cloudbees.groovy.cps.impl.CpsCallableInvocation
      	at sun.reflect.GeneratedConstructorAccessor129.newInstance(Unknown Source)
      	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
      	at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
      	at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:263)
      	at com.mkobit.Checks.checkArg(Checks.groovy)
      	at com.mkobit.Checks$checkArg.call(Unknown Source)
      	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
      	at com.mkobit.MyClass.<init>(MyClass.groovy:11)
      	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
      	at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
      	at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105)
      	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235)
      	at org.kohsuke.groovy.sandbox.impl.Checker$3.call(Checker.java:194)
      	at org.kohsuke.groovy.sandbox.GroovyInterceptor.onNewInstance(GroovyInterceptor.java:40)
      	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onNewInstance(SandboxInterceptor.java:128)
      	at org.kohsuke.groovy.sandbox.impl.Checker$3.call(Checker.java:191)
      	at org.kohsuke.groovy.sandbox.impl.Checker.checkedConstructor(Checker.java:188)
      	at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.constructorCall(SandboxInvoker.java:20)
      	at WorkflowScript.run(WorkflowScript:24)
      	at ___cps.transform___(Native Method)
      	at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:96)
      	at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)
      	at sun.reflect.GeneratedMethodAccessor337.invoke(Unknown Source)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:498)
      	at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
      	at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
      	at com.cloudbees.groovy.cps.Next.step(Next.java:58)
      	at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:154)
      	at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
      	at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:33)
      	at org.jenkinsci.plugins.workflow.cps.SandboxContinuable$1.call(SandboxContinuable.java:30)
      	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
      	at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:30)
      	at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:163)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:324)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$100(CpsThreadGroup.java:78)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:236)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:224)
      	at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:63)
      	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      	at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
      	at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
      	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      	at java.lang.Thread.run(Thread.java:745)
      Finished: FAILURE
      
      

            Unassigned Unassigned
            mkobit Mike Kobit
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: