• Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • script-security-plugin
    • None
    • Jenkins 2.387.1, everything mostly a few weeks old.

      Hi, I'm trying to use this dead simple enum in a shared library:

      package x.y.z
      enum ProjectTypes {
          python,
          node
          // workaround https://issues.jenkins.io/browse/JENKINS-33023
          ProjectTypes() {}
      }

      but I'm hitting a ridiculous bunch of errors. This time I'm really desperate and didn't find any workaround.
      The enum is placed in the library's /src directory and called from a method in a /vars script, that does type coercion from a string such as this, let's call it tripMine.groovy:

      import x.y.z.ProjectTypes
      def call(String projectTypeStr) {
          return projectTypeStr as ProjectTypes
      }

      This is supposed to work thanks to enum's valueOf() static method, but if I call it from a basic scripted pipeline (where the library is included on the folder-level):

      @Library('my-library@my-branch') _
      node {
          stage('trip') {
              def trouble = tripMine('python')
          }
      } 

      I get this exception back:

      org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such static method found: staticMethod x.y.z.ProjectTypes valueOf java.lang.Class java.lang.String 

      This is the main trouble, but the fun continues.

      The exception is from the script-security-plugin, I'm an admin, so I can easily whitelist that in the In-process script approval page, but yikes, it's not there. I found JENKINS-53700, but that's why I abandoned the usual declarative pipeline and used the scripted one. Still no dice.

      Next I tried to add the method to the enum:

      static ProjectTypes valueOf(Class clazz, String str) {
          return python
      } 

      This time the execution passed (why?), but reimplementing the whole method defies the very reason I used enum in the first place.

       

      Is there something sensible I can do to make this work? I've also found this "improvement" suggestion JENKINS-56948, which looks quite like the same thing, but I don't believe that's an improvement material. Hope I'm right   Thanks a lot!

          [JENKINS-71056] Shared library Enum fails on valueOf

          Vit created issue -
          Vit made changes -
          Environment New: Jenkins 2.387.1, everything mostly a few weeks old.
          Vit made changes -
          Description Original: Hi, I'm trying to use this dead simple {{enum}} in a shared library:
          {code:java}
          enum ProjectTypes {
              python,
              node
              // workaround https://issues.jenkins.io/browse/JENKINS-33023
              ProjectTypes() {}
          }{code}
          but I'm hitting a ridiculous bunch of errors. This time I'm really desperate and didn't find any workaround.
          The {{enum}} is placed in the library's {{/src}} directory and called from a method in a {{/vars}} script, that does type coercion from a string such as this, let's call it {{{}tripMine.groovy{}}}:
          {code:java}
          import x.y.z.ProjectTypes
          def call(String projectTypeStr) {
              return projectTypeStr as ProjectTypes
          }{code}
          This is supposed to work thanks to enum's valueOf() static method, but if I call it from a basic scripted pipeline (where the library is included on the folder-level):
          {code:java}
          @Library('my-library@my-branch') _
          node {
              stage('trip') {
                  def trouble = tripMine('python')
              }
          } {code}
          I get this exception back:
          {code:java}
          org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such static method found: staticMethod x.y.z.ProjectTypes valueOf java.lang.Class java.lang.String {code}
          This is the main trouble, but the fun continues.

          The exception is from the script-security-plugin, I'm an admin, so I can easily whitelist that in the In-process script approval page, but yikes, it's not there. I found [JENKINS-53700|https://issues.jenkins.io/browse/JENKINS-53700], but that's why I abandoned the usual declarative pipeline and used the scripted one. Still no dice.

          Next I tried to add the method to the {{{}enum{}}}:
          {code:java}
          static ProjectTypes valueOf(Class clazz, String str) {
              return python
          } {code}
          This time the execution passed (why?), but reimplementing the whole method defies the very reason I used {{enum}} in the first place.

           

          Is there something sensible I can do to make this work? I've also found this "improvement" suggestion [JENKINS-56948|https://issues.jenkins.io/browse/JENKINS-56948], which looks quite like the same thing, but I don't believe that's an improvement material. Hope I'm right :)  Thanks a lot!
          New: Hi, I'm trying to use this dead simple {{enum}} in a shared library:
          {code:java}
          package x.y.z
          enum ProjectTypes {
              python,
              node
              // workaround https://issues.jenkins.io/browse/JENKINS-33023
              ProjectTypes() {}
          }{code}
          but I'm hitting a ridiculous bunch of errors. This time I'm really desperate and didn't find any workaround.
          The {{enum}} is placed in the library's {{/src}} directory and called from a method in a {{/vars}} script, that does type coercion from a string such as this, let's call it {{{}tripMine.groovy{}}}:
          {code:java}
          import x.y.z.ProjectTypes
          def call(String projectTypeStr) {
              return projectTypeStr as ProjectTypes
          }{code}
          This is supposed to work thanks to enum's {{valueOf()}} static method, but if I call it from a basic scripted pipeline (where the library is included on the folder-level):
          {code:java}
          @Library('my-library@my-branch') _
          node {
              stage('trip') {
                  def trouble = tripMine('python')
              }
          } {code}
          I get this exception back:
          {code:java}
          org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such static method found: staticMethod x.y.z.ProjectTypes valueOf java.lang.Class java.lang.String {code}
          This is the main trouble, but the fun continues.

          The exception is from the script-security-plugin, I'm an admin, so I can easily whitelist that in the In-process script approval page, but yikes, it's not there. I found JENKINS-53700, but that's why I abandoned the usual declarative pipeline and used the scripted one. Still no dice.

          Next I tried to add the method to the {{{}enum{}}}:
          {code:java}
          static ProjectTypes valueOf(Class clazz, String str) {
              return python
          } {code}
          This time the execution passed (why?), but reimplementing the whole method defies the very reason I used {{enum}} in the first place.

           

          Is there something sensible I can do to make this work? I've also found this "improvement" suggestion JENKINS-56948, which looks quite like the same thing, but I don't believe that's an improvement material. Hope I'm right :)  Thanks a lot!
          Devin Nusbaum made changes -
          Assignee New: Devin Nusbaum [ dnusbaum ]
          Devin Nusbaum made changes -
          Assignee Original: Devin Nusbaum [ dnusbaum ]

            Unassigned Unassigned
            tlwhitec Vit
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: