I get this error when I run this test using 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)));
}
I tried adding this to my pom.xml based on other answers:
<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>
But when I build, I now get this error:
Fatal error compiling: error: invalid flag: --add-opens java.base/java.lang=ALL-UNNAMED
java-9
but you have<source|target>1.8</source|target>
in your POM? Where did you get--add-opens
from? – AnaemicargLine
based approach is very easy to get wrong. Personally I preferjvm.config
to set JVM parameters where you set all JVM arguments simply in a text file and Maven will pick it up automatically. See maven.apache.org/configure.html#mvn-jvm-config-file. – Magenta<source>1.17</source>
or<source>17</source>
is a thing. – Scrim--add-opens
controls the runtime behavior, therefore, there is no point in trying to specify it to the compiler. Just remove it from from the<compilerArgs>
. Maybe, the<argLine>
argument would do its job if you didn’t try to specify the argument to the compiler as well. But still, the point of this test is unclear. There is no sense in applying a deep-copy operation to anEnumSet
. UseEnumSet<?> dest = src.clone();
and you’ll know that you’ll get a correct result without the need for such a testcase. – Fir