antlr / stringtemplate4

StringTemplate 4
http://www.stringtemplate.org
Other
955 stars 231 forks source link

4.1 gives ClassCastException on use of .keys syntax #214

Closed richard-melvin closed 5 years ago

richard-melvin commented 5 years ago

A model adapter returns a Map<myClass1, MyClass2> for attribute x.

A template uses that value with $if (!x.keys.empty)$

Get a class cast exception with stack trace

    at java.base/java.lang.String.compareTo(String.java:125)
    at java.base/java.util.TreeMap.getEntry(TreeMap.java:350)
    at java.base/java.util.TreeMap.containsKey(TreeMap.java:231)
    at org.stringtemplate.v4.misc.MapModelAdaptor.getProperty(MapModelAdaptor.java:45)

This is because the code now indexes into the map with the string 'keys', instead of using it to find the keyset.

        else if ( map.containsKey(property) ) value = map.get(property); // <- executes and crashes
        else if ( map.containsKey(propertyName) ) { // if can't find the key, try toString version
            value = map.get(propertyName);
        }
        else if ( property.equals("keys") ) value = map.keySet(); // <- intended behaviour

This worked correctly on 4.0.8.

`

parrt commented 5 years ago

oh, interesting. hmm...yeah, i think we made that a keyword or something. :( Sorry but I doubt I'll get to this.

sharwell commented 5 years ago

I believe this was one of my changes. I'll take a look.

sharwell commented 5 years ago

Yeah the change is https://github.com/antlr/stringtemplate4/pull/185. However, according to the Map interface it seems like this should be fine. I need to look into the specifics where the exception is thrown.