Search code examples
javacastingfreemarkerbigdecimal

Cast BigDecimal to Short in Freemarker


I have a Map<Short, String> in my freemarker value-stack and I need to print a value in the template in this way:

${map.get(3)}

Since freemarker interprets that 3 as a BigDecimal an exception is thrown:

freemarker.core._TemplateModelException:[...]
[...]
Caused by: java.lang.ClassCastException: java.lang.Short cannot be cast to java.math.BigDecimal
    at java.math.BigDecimal.compareTo(BigDecimal.java:220)
    at java.util.TreeMap.getEntry(TreeMap.java:352)
    at java.util.TreeMap.get(TreeMap.java:278)

Of course I can make the String value accessible in the value stack, but is there a more elegant way to cast numbers to Short in freemarker?

(I'm using freemarker 2.3.25)


Solution

  • Try ${map.get(3?short)}.

    This is a problem with calling API-s that don't declare the type of their parameter (such as Map.get(Object); the generic type parameter is erased during compilation).