[英]Issue while updating java project from JDK11 to JDK17
I have a java project using jdk11.我有一个使用 jdk11 的 java 项目。 I need to migrate the project to java 17 but integration tests have started failing.
我需要将项目迁移到 java 17 但集成测试已经开始失败。 As soon as I upgraded to JDK17 I started getting below error:
一旦我升级到 JDK17,我就开始出现以下错误:
java.lang.reflect.InaccessibleObjectException: Unable to make private sun.reflect.generics.repository.FieldRepository java.lang.reflect.Field.getGenericInfo() accessible: module java.base does not "opens java.lang.reflect" to unnamed module @5702b3b1
I was also getting error for java.util but that is fixed using below command line code.我也收到了 java.util 的错误,但这已使用下面的命令行代码修复。
I have tried adding the command line options as below but the tests are still failing我已尝试添加如下命令行选项,但测试仍然失败
<argLine>
--illegal-access=permit
--add-opens java.base/java.lang=ALL-UNNAMED
--add-opens java.base/sun.reflect=ALL-UNNAMED
--add-opens java.base/java.util=ALL-UNNAMED
</argLine>
Any help is appreciated.任何帮助表示赞赏。
You can pass your method in to this:您可以将您的方法传递给此:
public static <T extends AccessibleObject> T forceAccessible(T o) {
try {
Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
unsafeField.setAccessible(true);
Unsafe unsafe = (Unsafe) unsafeField.get(null);
Method m = Class.class.getDeclaredMethod("getDeclaredFields0", boolean.class);
m.setAccessible(true);
Field override = ((Field[]) m.invoke(AccessibleObject.class, false))[0];
if (!o.isAccessible()) {
try {
o.setAccessible(true);
} catch (InaccessibleObjectException e) {
unsafe.putBoolean(o, unsafe.objectFieldOffset(override), true);
}
}
} catch (Exception e) {}
return o;
}
I made a library for this kind of stuff:我为这种东西制作了一个库:
https://github.com/TheOneAndOnlyDanSan/Reflection https://github.com/TheOneAndOnlyDanSan/Reflection
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.