Varags arguments to enum constructors don't work

This issue is archived. You can view it, but you can't modify it. Learn more

XMLWordPrintable

      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);
          }
      

            Assignee:
            Andrew Bayer
            Reporter:
            Magnus Reftel
            Archiver:
            Jenkins Service Account

              Created:
              Updated:
              Resolved:
              Archived: