Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant access to java.util.HashMap$Entry with modifiers "public final"

My problem is, that my app works fine run locally on Tomcat server, but throws errors on server with installed glassfish. Whole problem is that i'm iterate looking through HashMap in JSTL. Server throws an stack as below:

Servlet.service() for servlet jsp threw exception java.lang.IllegalAccessException:
Class javax.el.BeanELResolver can not access a member of class java.util.HashMap$Entry with modifiers "public final" 
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:95) 
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:261) 
at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:253) 

problem is caused by code:

<c:forEach items="${element.getPreparedParameters()}" var="parametr" varStatus="j">
    documents["${i.index}"]["param"]=new Array();
    documents["${i.index}"]["param"]["key"] = "${parametr.getKey()}";
    documents["${i.index}"]["param"]["value"] = "${parametr.getValue()}";
</c:forEach>

Where element.getPreparedParameters() returns HashMap.

How can i make it work?

like image 445
T.G Avatar asked Aug 20 '12 12:08

T.G


1 Answers

Check out this decades-old bug reported to Sun against Java 1.2 (fixed in Java 11). I remember seeing this error before and the message is misleading: the problem lies not with the method modifiers, but with the modifiers on the owning class. Namely, Map.Entry is a public interface, but the implementing class in HashMap is private. Reflection doesn't allow you to access the methods of the class even though you are accessing methods that implement a public interface.

I'd suggest going for a cheap workaround: don't iterate over the entrySet, but over the keySet and use map.get(key) instead of entry.getValue().

Alternative would be updating to Java 11 or newer.

like image 199
Marko Topolnik Avatar answered Nov 03 '22 00:11

Marko Topolnik