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

Complex enum are not supported in Shared Library

XMLWordPrintable

      I've noticed that when I override a method for a particular enum value, my pipeline fails

      I've reproduce the error with the following elements. I declare an enum in a dedicated class file

      enum Day {
          SUNDAY(false) {
              String aName() {
                  return "Easy day !"
              }
          },
          MONDAY(true),
      
          boolean workingDay
      
          Day(boolean workingDay) {
              this.workingDay = workingDay
          }
      
          String aName() {
              return "Thought day !"
          }
      }
      

      Then in a pipeline script I'm using it

      import com.jenkins.Day
      node {
          println "${Day.MONDAY} is a working day ? ${Day.MONDAY.isWorkingDay()}"
          println "${Day.MONDAY} is ${Day.MONDAY.aName()}"
          println "${Day.SUNDAY} is ${Day.SUNDAY.aName()}"
      }
      

      When I run this pipeline I can see the following error

      org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: unclassified field java.lang.Class MONDAY
      	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.unclassifiedField(SandboxInterceptor.java:397)
      	at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:381)
      	at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:284)
      	at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:288)
      	at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
      	at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
      	at com.bridge.jenkins.pipelines.deploy.WorkflowScript.run(WorkflowScript:7)
      	at ___cps.transform___(Native Method)
      	at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
      	at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
      	at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
      	at sun.reflect.GeneratedMethodAccessor307.invoke(Unknown Source)
      ...

      Besides I've noticed a different behaviour when I'm declaring such enum directly in the pipeline script, like

      enum Day {
          SUNDAY(false) {
              String aName() {
                  return "Easy day !"
              }
          },
          MONDAY(true),
      
          boolean workingDay
      
          Day(boolean workingDay) {
              this.workingDay = workingDay
          }
      
          String aName() {
              return "Thought day !"
          }
      }
      
      
      node {
          println "${Day.MONDAY} is a working day ? ${Day.MONDAY.isWorkingDay()}"
          println "${Day.MONDAY} is ${Day.MONDAY.aName()}"
          println "${Day.SUNDAY} is ${Day.SUNDAY.aName()}"
      }
      

      I can see a different error

      [Pipeline] {
      [Pipeline] echo
      MONDAY is a working day ? true
      [Pipeline] echo
      MONDAY is Thought day !
      [Pipeline] End of Pipeline
      java.lang.NullPointerException
      	at com.cloudbees.groovy.cps.impl.CpsCallableInvocation.invoke(CpsCallableInvocation.java:40)
      	at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:62)
      	at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)
      	at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixName(FunctionCallBlock.java:77)
      	at sun.reflect.GeneratedMethodAccessor137.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:83)
      ...
      

      I suspect an issue with CPS transformation or something like which does not support this tricky enum

            Unassigned Unassigned
            jgiovaresco Julien Giovaresco
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: