Cant access to java.util.HashMap$Entry with modifiers quot;public finalquot;(无法使用修饰符“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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法使用修饰符“public final"访问 java.util.Ha


- 如何使用WebFilter实现授权头检查 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01