Jan. 12, 2017, 4:19 p.m.
IT | Rants

This Pisses Me Off

 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

In specific, all I want to do is use a JSTL variable as index to a variable someMap as follows:

Map<Integer, SomeObject> someMap = new HashMap<>();
...
<c:forEach begin="0" end="10" var="level">
    <c:out value="${someMap[level]}" />
</c:forEach>

That works.

But pass that variable to another page as follows:

<jsp:include page="<%=somePageContent%>">
   <jsp:param name="level" value="${level}"/>
</jsp:include>

and in that page...

<c:set var="level" value="${param.level}" />
<c:forEach var="entry" items="${someMap[level]}">
    <c:out value="${entry}" />
</c:forEach>

You are greeted with:

org.apache.jasper.JasperException: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

Stupid Java decided to convert this parameter to a String. Ok fine so lets use a stupid trick to force it to be integer:

<c:set var="level" value="${param.level + 0}" />
<c:forEach var="entry" items="${someMap[level]}">
    <c:out value="${entry}" />
</c:forEach>

Then you are greeted with:

org.apache.jasper.JasperException: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

Now Java wants to make that variable a Long. And for some stupid reason, a Long object is not coercible into an Integer object. I get that they are different classes, but as long as the value is within range of the data type, they are directly comparable. A 0 is a 0 whether it is a Short, Integer, Long or anything else.

Java should not convert values willy nilly into arbitrary types. This makes the code very convoluted. Sometimes I wish I could just move to Django / Python as stupid things like this do not cause you to drink more than you should. Though that said, I hate interpreted languages. It is just as dumb too. SF.