如何修复 Guice 错误,"An illegal reflective access operation has occurred"

How to fix Guice error, "An illegal reflective access operation has occurred"

我有一个使用 Guice 进行 DI 的 Kotlin 项目,最近更新为 JDK 8 -> 11。它现在在运行时发出以下错误:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.google.inject.internal.cglib.core.$ReflectUtils (file:/Users/matt/.gradle/caches/modules-2/files-2.1/com.google.inject/guice/4.2.2/6dacbe18e5eaa7f6c9c36db33b42e7985e94ce77/guice-4.2.2.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of com.google.inject.internal.cglib.core.$ReflectUtils
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

应如何处理此警告?

问题是由于 Guice 内部访问 Java 对象的方式,这在新的 (Java 9+) Java 模块系统下是不安全的。

此外,从 Java 16 开始,此警告变为错误,必须手动指定执行标志 --illegal-access=permit 才能重现 Java 9-15 的行为。

您无能为力。最好的选择是升级到 Guice 5.0.1,它已经打了补丁以避免非法访问。您的警告将消失,您的应用程序将以默认的 JVM 行为在 Java 16+ 上运行。

如前所述,您最好只升级到 Guice 5,它已更新。

但是...以防万一您在升级之前无法使用 Guice 4,只需使用这些额外参数开始 java:

java --illegal-access=permit --add-opens java.base/java.lang=ALL-UNNAMED ...

一切顺利!!