什么是非法反思访问 [英] what is an illegal reflective access

查看:141
本文介绍了什么是非法反思访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Java 9中的非法反射访问存在很多问题。

There are a lot of questions around about illegal reflective access in Java 9.

现在我找不到因为所有谷歌都在努力工作围绕错误消息,实际上是非法反射访问。

Now what I can't find because all Google spews up is people trying to work around the error messages, is what an illegal reflective access actually is.

所以我的问题很简单:

什么定义了非法反射访问以及什么情况会触发警告?

What defines an illegal reflective access and what circumstances trigger the warning?

我已经收集到它与Java 9中引入的封装原则有关,但是它是如何挂在一起的,是什么触发警告在什么情况下我找不到解释。

I have gathered that it has something to do with the encapsulation principles that were introduced in Java 9, but how it all hangs together and what triggers the warning in what scenario I can't find an explanation to.

推荐答案

除了了解模块及其各自包之间的访问。我相信它的关键在于 模块系统#Sleeded-strong-封装 我会选择相关部分来尝试回答这个问题。

Apart from an understanding of the accesses amongst modules and their respective packages. I believe the crux of it lies in the Module System#Relaxed-strong-encapsulation and I would just cherry pick the relevant parts of it to try and answer the question.


什么定义了非法反射访问以及
触发警告的情况?

为了帮助迁移到Java9,模块的强大封装可以放宽。

To aid in the migration to Java9, the strong encapsulation of the modules could be relaxed.


  • 实现可以提供静态访问,即通过编译的字节码。

  • An implementation may provide static access, i.e. by compiled bytecode.

可以提供一种方法来调用其运行时系统,其中一个或多个模块的一个或多个包打开到所有未命名模块中的代码,即在类路径上编码。如果以这种方式调用运行时系统,并且通过这样做,一些反射API的调用就会成功,否则它们就会失败。

May provide a means to invoke its run-time system with one or more packages of one or more of its modules open to code in all unnamed modules, i.e. to code on the classpath. If the run-time system is invoked in this way, and if by doing so some invocations of the reflection APIs succeed where otherwise they would have failed.

在这种情况下,您实际上最终会进行反思性访问 非法因为在纯粹的模块化世界中你并不打算进行此类访问。

In such cases, you've actually ended up making a reflective access which is "illegal" since in a pure modular world you were not meant to do such accesses.


如何将它们挂在一起以及什么触发了
场景中的警告?

这种封装的放宽在运行时通过新的启动程序选项 - 非法访问进行控制,默认情况下在Java9中等于 permit permit 模式确保

This relaxation of the encapsulation is controlled at runtime by a new launcher option --illegal-access which by default in Java9 equals permit. The permit mode ensures


对任何此类包的第一次反射访问操作会导致发出
警告,但在此之后不会发出警告。
此单一警告描述了如何启用进一步的警告。无法抑制此
警告。

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 (消息以及每次此类访问的堆栈跟踪),警告(每个此类访问的消息)和拒绝(禁用此类操作)。

The modes are configurable with values debug(message as well as stacktrace for every such access), warn(message for each such access) and deny(disables such operations).

在应用程序上调试和修复的内容很少: -

Few things to debug and fix on applications would be:-


  • 使用 - 非法访问=拒绝运行它以了解并避免打开包从一个模块到另一个模块没有包含这样的指令的模块声明(打开)或显式使用 - 添加-opens VM arg。

  • 可以使用 jdeps来识别从编译代码到JDK内部API的静态引用的工具 - jdk-internals 选项

  • Run it with --illegal-access=deny to get to know about and avoid opening packages from one module to another without a module declaration including such a directive(opens) or an explicit use of --add-opens VM arg.
  • Static references from compiled code to JDK-internal APIs could be identified using the jdeps tool with the --jdk-internals option


检测到非法反射访问操作
时发出的警告消息具有以下形式:

The warning message issued when an illegal reflective-access operation is detected has the following form:

WARNING: Illegal reflective access by $PERPETRATOR to $VICTIM

其中:

$ PERPETRATOR 是包含类型的完全限定名称调用有问题的反射操作的
代码加上代码
source(即JAR文件路径)(如果可用)和

$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 是一个描述被访问成员的字符串,
包括封闭类型的完全限定名称

$VICTIM is a string that describes the member being accessed, including the fully-qualified name of the enclosing type


此类样本警告的问题:= JDK9:发生了非法的反射访问操作。 org.python.core.PySystemState

Questions for such a sample warning: = JDK9: An illegal reflective access operation has occurred. org.python.core.PySystemState

最后和一个重要的注意事项,在努力确保您不会面临此类警告并且未来安全的时候需要做的是确保你的模块没有进行那些非法的反射访问。 :)

Last and an important note, while trying to ensure that you do not face such warnings and are future safe, all you need to do is ensure your modules are not making those illegal reflective accesses. :)

这篇关于什么是非法反思访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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