无法使用修饰符“public final"访问 java.util.HashMap$Entry [英] Cant access to java.util.HashMap$Entry with modifiers "public final"

查看:27
本文介绍了无法使用修饰符“public final"访问 java.util.HashMap$Entry的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我的应用程序在 Tomcat 服务器上本地运行良好,但在安装 glassfish 的服务器上抛出错误.整个问题是我在 JSTL 中遍历 HashMap.服务器抛出如下堆栈:

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) 

问题是由代码引起的:

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

其中 element.getPreparedParameters() 返回 HashMap.

我怎样才能让它工作?

推荐答案

查看 这个几十年前的错误针对 Java 1.2 向 Sun 报告(已在 Java 11).我记得之前看到过这个错误,并且该消息具有误导性:问题不在于方法修饰符,而在于拥有类的修饰符.即Map.Entry是一个公共接口,而HashMap中的实现类是私有的.即使您正在访问实现公共接口的方法,反射也不允许您访问该类的方法.

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.

我建议采用一种廉价的解决方法:不要遍历 entrySet,而是遍历 keySet 并使用 map.get(key) 而不是 entry.getValue().

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

替代方案将更新到 Java 11 或更新版本.

Alternative would be updating to Java 11 or newer.

这篇关于无法使用修饰符“public final"访问 java.util.HashMap$Entry的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆