Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.5
    • Fix Version/s: 1.6, 2.x
    • Component/s: None
    • Labels:
      None

      Description

      If possible and as much as possible without breaking JDK 1.3/JDK 1.4 runtime support, i would like to see us support varargs for method calls. So,

      public class MyTool {
      public List combine(List... lists)

      { ... }

      }

      #set( $twoLists = $myTool.combine($list1, $list2) )
      #set( $threeLists = $myTool.combine($list1, $list2, $list3) )

      will work out of the box, without need for the user to rebuild Velocity themselves.

      1. varargs.2.diff
        30 kB
        Nathan Bubna

        Activity

        Hide
        Nathan Bubna added a comment -

        The problem is that varargs are a compile-time trick pulled off by the jdk. So, at runtime, a vararg (e.g. String... foo) is seen as an array of that type (e.g. String[] foo).

        This means we can call vararg methods by passing an array, but it would be nice to pull off some uberspect magic to allow vararg methods to be called with a variable number of arguments, rather than a varying sized array. Much simpler that way, especially as Velocity's so-called "array" syntax actually creates an ArrayList, making the workaround for this that much trickier.

        At first glance, this may be doable via some tweaks to MethodMap's isApplicable() and moreSpecific() methods.

        Show
        Nathan Bubna added a comment - The problem is that varargs are a compile-time trick pulled off by the jdk. So, at runtime, a vararg (e.g. String... foo) is seen as an array of that type (e.g. String[] foo). This means we can call vararg methods by passing an array, but it would be nice to pull off some uberspect magic to allow vararg methods to be called with a variable number of arguments, rather than a varying sized array. Much simpler that way, especially as Velocity's so-called "array" syntax actually creates an ArrayList, making the workaround for this that much trickier. At first glance, this may be doable via some tweaks to MethodMap's isApplicable() and moreSpecific() methods.
        Hide
        Nathan Bubna added a comment -

        Here's a proposed patch for this. I need to spruce up and flesh out the tests a bit more, but i think i have most of the bases covered.

        And for the observant, yes, this would allow vararg method calls in VTL for any method whose final parameter is an array. Java 5 is not needed at all. The only backwards compatibility issue would be that some previously broken method calls might now work. I think that's fine though.

        Thoughts? I'll try to provide a link to a build shortly to make this easy to play with. I'd love some feedback on this (re: implementation, effects, performance, etc) before i commit it.

        Show
        Nathan Bubna added a comment - Here's a proposed patch for this. I need to spruce up and flesh out the tests a bit more, but i think i have most of the bases covered. And for the observant, yes, this would allow vararg method calls in VTL for any method whose final parameter is an array. Java 5 is not needed at all. The only backwards compatibility issue would be that some previously broken method calls might now work. I think that's fine though. Thoughts? I'll try to provide a link to a build shortly to make this easy to play with. I'd love some feedback on this (re: implementation, effects, performance, etc) before i commit it.
        Hide
        Nathan Bubna added a comment -

        Here's an updated version that has more thorough testing and fixes a few bugs in the previous one.

        If you want to try it out, here's a build with the patch applied:

        http://people.apache.org/~nbubna/velocity/engine/1.6-dev/

        Show
        Nathan Bubna added a comment - Here's an updated version that has more thorough testing and fixes a few bugs in the previous one. If you want to try it out, here's a build with the patch applied: http://people.apache.org/~nbubna/velocity/engine/1.6-dev/
        Hide
        Will Glass-Husain added a comment -

        I like it. Useful extension, easy to understand, works well with existing syntax. Makes sense to factor out methods from MethodMap into IntrospectionUtils as well.

        FYI. A related thing I've always wanted to see is the ability to use expressions in method calls.

        Show
        Will Glass-Husain added a comment - I like it. Useful extension, easy to understand, works well with existing syntax. Makes sense to factor out methods from MethodMap into IntrospectionUtils as well. FYI. A related thing I've always wanted to see is the ability to use expressions in method calls.

          People

          • Assignee:
            Unassigned
            Reporter:
            Nathan Bubna
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development