Class Cast Exception when using single character key

"Hinkle, Cameron" <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
I am getting a class cast exception from Freemarker when using a lookup key that is just a single character:

Caused by: java.lang.ClassCastException: java.lang.Character cannot be cast to java.lang.String
        at java.lang.String$CaseInsensitiveComparator.compare(String.java:1211)
        at java.util.TreeMap.getEntryUsingComparator(TreeMap.java:351)
        at java.util.TreeMap.getEntry(TreeMap.java:322)
        at java.util.TreeMap.get(TreeMap.java:255)
        at freemarker.template.SimpleHash.get(SimpleHash.java:203)
        at freemarker.core.Dot._getAsTemplateModel(Dot.java:76)
        at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
        at freemarker.core.Assignment.accept(Assignment.java:90)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.MixedContent.accept(MixedContent.java:92)
...

I took a look at the source and I don't see any way around it except using more than one character as my key:

>From SimpleHash.get(String key), line 199:

if(key.length() == 1) {
      // just check for Character key if this is a single-character string
      Character charKey = new Character(key.charAt(0));
      result = map.get(charKey);
      if (result == null && !(map.containsKey(key) || map.containsKey(charKey))) {
            return null;
      }
      else {
            putKey = charKey;
      }
}
else if(!map.containsKey(key)) {
      return null;
}
else {
      putKey = key;
}

It looks like TreeMap is being used as the implementation of the map field in SimpleHash and when TreeMap runs through the tree trying to find the right value, it tries to compare another key stored as a String to this Character object that Freemarker is requesting.  Any idea why the key is being converted to a Character and is there any way around it?  I don't see the same thing happening on SimpleHash.put() so I find it really strange that it would happen on get().

Thanks,
Cameron

Cameron Hinkle  |  Senior Application Engineer  |  Nike DCIT  |  503.532.3075  |  7. Evolve Immediately |

------------------------------------------------------------------------------
uberSVN's rich system and user administration capabilities and model 
configuration take the hassle out of deploying and managing Subversion and 
the tools developers use with it. Learn more about uberSVN and get a free 
download at:  http://p.sf.net/sfu/wandisco-dev2dev

_______________________________________________
FreeMarker-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.