Uploaded image for project: 'Velocity'
  1. Velocity
  2. VELOCITY-730

Property references don't work with maps that implement Map indirectly (such as Google Collections ImmutableMap)

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.6.1
    • 1.7
    • Engine
    • None

    Description

      If you pass a map created using Google Collections ImmutableMap into a Velocity context, and try to access its values using "map.key" style property syntax, it fails to resolve.

      I believe the problem is in MapGetExecutor:

      http://svn.apache.org/viewvc/velocity/engine/tags/1.6.1/src/java/org/apache/velocity/runtime/parser/node/MapGetExecutor.java?view=co

      Specifically, this code:

      Class [] interfaces = clazz.getInterfaces();
      for (int i = 0 ; i < interfaces.length; i++)
      {
      if (interfaces[i].equals(Map.class))
      {
      ...

      The sneaky thing about ImmutableMap is that ImmutableMap.of(...) doesn't return an instance of ImmutableMap, but one of serveral subclasses optimized for however many items are in the map. These subclasses extend ImmutableMap but do not directly implement Map, so that code above fails to recognize them.

      A simpler and more accurate way to tell whether the class implements Map, either directory or indirectly, is "if (Map.class.isAssignableFrom(clazz))..."

      Attachments

        Activity

          People

            Unassigned Unassigned
            tmoore Tim Moore
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: