使用 JDK 17 运行此测试时出现此错误:
java.lang.reflect.InaccessibleObjectException: Unable to make field final transient java.lang.Class java.util.EnumSet.elementType accessible: module java.base does not "opens java.util" to unnamed module @60addb54
@Test
public void testThatDeepCopyCopiesEmptySet() {
SetOfEnumUserType setOfEnumUserType = createSetOfEnumUserType();
EnumSet<PaymentMethodType> src = EnumSet.noneOf(PaymentMethodType.class);
EnumSet<?> dest = (EnumSet<?>) setOfEnumUserType.deepCopy(src);
assertThat(dest, (is(src)));
assertThat(dest, not(isSameInstanceAs(src)));
Class<?> srcType = (Class<?>) ReflectionTestUtils.getField(src, "elementType");
Class<?> destType = (Class<?>) ReflectionTestUtils.getField(dest, "elementType");
assertThat(srcType, (is(destType)));
}
我尝试根据其他答案将此添加到我的 pom.xml 中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArgs>
<arg>--add-opens java.base/java.lang=ALL-UNNAMED</arg>
<arg>--add-opens java.base/java.util=ALL-UNNAMED</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<argLine>--add-opens java.base/java.lang=ALL-UNNAMED</argLine>
<argLine>--add-opens java.base/java.util=ALL-UNNAMED</argLine>
</configuration>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
<inherited />
</execution>
</executions>
</plugin>
但是当我构建时,我现在得到这个错误:
Fatal error compiling: error: invalid flag: --add-opens java.base/java.lang=ALL-UNNAMED