Java 9 中有很多关于非法反射访问的问题。
我发现了很多关于解决错误消息的讨论,但我很想知道非法反射访问实际上是什么。
所以我的问题是:
什么定义了非法反射访问以及什么情况会触发警告?
我已经收集到它与 Java 9 中引入的封装原则有关,但我无法找到关于它们如何结合在一起、什么触发警告以及在什么情况下的解释。
最佳答案
除了了解模块及其各自包之间的访问之外。我相信它的症结在于 Module System#Relaxed-strong-encapsulation 我会挑选其中的相关部分来尝试回答这个问题。
What defines an illegal reflective access and what circumstances trigger the warning?
为了帮助迁移到 Java-9,可以放松对模块的强封装。
在这种情况下,您实际上最终进行了“非法”的反射访问,因为在纯模块化世界中,您不应该进行此类访问。
How it all hangs together and what triggers the warning in what scenario?
这种封装的放松在运行时由一个新的启动器选项
--illegal-access
控制。在 Java9 中默认等于 permit
. permit
模式确保The first reflective-access operation to any such package causes a warning to be issued, but no warnings are issued after that point. This single warning describes how to enable further warnings. This warning cannot be suppressed.
模式可配置为值
debug
(每次此类访问的消息和堆栈跟踪),warn
(每个此类访问的消息)和deny
(禁用此类操作)。在应用程序上调试和修复的几件事是:-
--illegal-access=deny
运行它了解并避免在没有包含此类指令(opens
)的模块声明或显式使用 --add-opens
的情况下从一个模块打开包到另一个模块虚拟机参数。 jdeps
识别从编译代码到 JDK 内部 API 的静态引用。带有 --jdk-internals
的工具选项 The warning message issued when an illegal reflective-access operation is detected has the following form:
WARNING: Illegal reflective access by $PERPETRATOR to $VICTIM
where:
$PERPETRATOR
is the fully-qualified name of the type containing the code that invoked the reflective operation in question plus the code source (i.e., JAR-file path), if available, and
$VICTIM
is a string that describes the member being accessed, including the fully-qualified name of the enclosing type
此类示例警告的问题:= JDK9: An illegal reflective access operation has occurred. org.python.core.PySystemState
最后也是重要的一点,在尝试确保您不会面临此类警告并且 future 安全时,您需要做的就是确保您的模块不会进行那些非法的反射访问。 :)
关于java - 什么是非法反射访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52480427/