fustaki
fustaki

Reputation: 1614

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)

Upvotes: 1

Views: 1053

Answers (2)

ddekany
ddekany

Reputation: 31112

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).

Upvotes: 2

Rohit Gulati
Rohit Gulati

Reputation: 542

Try

${map.get(new java.math.BigInteger("3").shortValueExact())}

OR

${map.get(new java.math.BigInteger("3").shortValue())}

Upvotes: 0

Related Questions