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

Varags arguments to enum constructors don't work

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      It appears that varargs constructors don't work for enums. The following SandboxInterceptorTest test case demonstrates this:

          @Test public void enumWithVarargs() throws Exception {
              String script = "enum Thing {\n"
                  + "  FIRST(\"The first thing\"),\n"
                  + "  String[] descriptions;\n"
                  + "  public Thing(String... descriptions) {\n"
                  + "    this.descriptions = descriptions;\n"
                  + "  }\n"
                  + "}\n"
                  + "Thing.values()[0].descriptions[0]\n";
              String expected = "The first thing";
              assertEvaluate(new GenericWhitelist(), expected, script);
          }
      

      The result is:

      org.junit.ComparisonFailure: 
      Expected :The first thing
      Actual   :T
      

      It appears that something goes wrong when building the argument array, as the following testcase triggers an ArrayIndexOutOfBoundsException:

          @Test public void enumWithStringAndVarargs() throws Exception {
              String script = "enum Thing {\n"
                  + "  FIRST(\"The first thing\"),\n"
                  + "  String description;\n"
                  + "  public Thing(String description, int... unused) {\n"
                  + "    this.description = description;\n"
                  + "  }\n"
                  + "}\n"
                  + "Thing.values()[0].description\n";
              String expected = "The first thing";
              assertEvaluate(new GenericWhitelist(), expected, script);
          }
      

        Attachments

          Issue Links

            Activity

            Hide
            magnusr Magnus Reftel added a comment -

            If this is a duplicate of JENKINS-33023, then JENKINS-33023 should be re-opened. Enums with varargs constructors do not work on master as of 3d8b4045415ffe48289c79ac2905df3a424e94dc.

            The fixes related to JENKINS-33023 made the varargs methods created for enum initialisation work, as long as the enum constructors themselves were not varargs ("public Thing(String... descriptions)"). If the constructors are varargs, the resulting methods still do not work. Just try adding the two test cases from the description to SandboxInterceptorTest and you'll see.

            Show
            magnusr Magnus Reftel added a comment - If this is a duplicate of JENKINS-33023 , then JENKINS-33023 should be re-opened. Enums with varargs constructors do not work on master as of 3d8b4045415ffe48289c79ac2905df3a424e94dc. The fixes related to JENKINS-33023 made the varargs methods created for enum initialisation work, as long as the enum constructors themselves were not varargs ("public Thing(String... descriptions)"). If the constructors are varargs, the resulting methods still do not work. Just try adding the two test cases from the description to SandboxInterceptorTest and you'll see.
            Hide
            magnusr Magnus Reftel added a comment -

            Patch adding the two test cases from the description to SandboxInterceptorTest.java

            Show
            magnusr Magnus Reftel added a comment - Patch adding the two test cases from the description to SandboxInterceptorTest.java
            Hide
            abayer Andrew Bayer added a comment -

            So this actually works at least as of script-security 1.34...once you get rid of the trailing commas after the last enum element. That's not valid Groovy syntax in the first place and so won't work regardless.

            I've got a PR up with the tests verifying this works at https://github.com/jenkinsci/script-security-plugin/pull/151

            Show
            abayer Andrew Bayer added a comment - So this actually works at least as of script-security 1.34...once you get rid of the trailing commas after the last enum element. That's not valid Groovy syntax in the first place and so won't work regardless. I've got a PR up with the tests verifying this works at https://github.com/jenkinsci/script-security-plugin/pull/151
            Hide
            scm_issue_link SCM/JIRA link daemon added a comment -

            Code changed in jenkins
            User: Andrew Bayer
            Path:
            src/test/java/org/jenkinsci/plugins/scriptsecurity/sandbox/groovy/SandboxInterceptorTest.java
            http://jenkins-ci.org/commit/script-security-plugin/23cc8e4491a1323299481f1e3298cc9e73ba3678
            Log:
            JENKINS-35294 Test verifying enums with varargs work

            Show
            scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: Andrew Bayer Path: src/test/java/org/jenkinsci/plugins/scriptsecurity/sandbox/groovy/SandboxInterceptorTest.java http://jenkins-ci.org/commit/script-security-plugin/23cc8e4491a1323299481f1e3298cc9e73ba3678 Log: JENKINS-35294 Test verifying enums with varargs work
            Hide
            scm_issue_link SCM/JIRA link daemon added a comment -

            Code changed in jenkins
            User: Andrew Bayer
            Path:
            src/test/java/org/jenkinsci/plugins/scriptsecurity/sandbox/groovy/SandboxInterceptorTest.java
            http://jenkins-ci.org/commit/script-security-plugin/244f31e37c05c5cf5f85607be99b7cbc8d3175dc
            Log:
            Merge pull request #151 from abayer/jenkins-35294

            JENKINS-35294 Test verifying enums with varargs work

            Compare: https://github.com/jenkinsci/script-security-plugin/compare/bee837dae519...244f31e37c05

            Show
            scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: Andrew Bayer Path: src/test/java/org/jenkinsci/plugins/scriptsecurity/sandbox/groovy/SandboxInterceptorTest.java http://jenkins-ci.org/commit/script-security-plugin/244f31e37c05c5cf5f85607be99b7cbc8d3175dc Log: Merge pull request #151 from abayer/jenkins-35294 JENKINS-35294 Test verifying enums with varargs work Compare: https://github.com/jenkinsci/script-security-plugin/compare/bee837dae519...244f31e37c05

              People

              • Assignee:
                abayer Andrew Bayer
                Reporter:
                magnusr Magnus Reftel
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: