Re: Class Cast Exception when using single character key
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
I can see what the author wanted, but it's just wrong. His idea was
that if the key isn't found as a string, and the string length is 1,
let's re-try with a Char. Too easy-going approach... hash keys was
always restricted to String-s, so it just shouldn't be found.
I wonder how to fix this without breaking backward compatibility... I
will have to catch the ClassCatchException. That's better than what
happens now, but creating the new exception still will have some
performance implications.
Anyway, I have always used BeansWrapper, which has a different (IMO
better) hash implementation. That's another possibility unless you
have some huge code-base (because it doesn't exactly behave like the
default object wrapper, so some stuff can break, in theory).
--
Best regards,
Daniel Dekany
Monday, August 22, 2011, 6:54:18 PM, Hinkle, Cameron wrote:
> 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