antlr / stringtemplate4

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

Fix backward compatibility issues introduced by #233 #241

Closed Clashsoft closed 4 years ago

Clashsoft commented 4 years ago

Reverts NumberRenderer and StringRenderer to accept Object instead of Number or String respectively.

NumberRenderer always worked with Object before. Actually it should be called FormatRenderer or similar because it merely uses Formatter, nothing Number-specific.

StringRenderer now uses the pre-#233 behaviour of accepting Object at runtime, but failing if the actual value is not a String due to the forced cast.

The main motivation is that #233 broke code like this:

class CustomStringRenderer extends StringRenderer {
    @Override
    public String toString(Object value, String formatString, Locale locale) {
        if ("decap".equals(formatString)) { return ... };
        return super.toString(value, formatString, locale);
    }
}

Now suddenly that method no longer overrides toString from StringRenderer, because it had the signature toString(String, String, Locale) in #233. And now we have breaking changes!

See https://github.com/Clashsoft/GenTreeSrc/blob/master/src/main/java/de/clashsoft/gentreesrc/codegen/StringRenderer.java for an example of this in the wild.