无法使字段最终瞬态 java.lang.Class java.util.EnumSet.elementType 可访问:模块 java.base 不"opens java.util"未命名



当我使用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

@Holger是对的,你不应该侵入JDK的内部。他还正确地解释了--add-opens不是一个有效的Maven编译器选项。因为他没有写回答,而是写评论,所以我写了一个。信用证是他的。

关于源版本和目标版本1.8的恼人注释,这当然不是问题的根本原因。在遵从级别1.8的JDK 17下编译是完全合法的。

这是一个MCVE——你应该在一开始就提供一个MCVE,而不是一个不可编译的代码片段和一个不完整的POM:

Maven POM:

请注意:

  • --add-opens现在只在它所属的Surefire配置中,不再在编译器配置中。
  • 我从配置中删除了非法选项<inherited />
  • 我依赖于默认的Surefire执行而不是指定另一个。在您的用例中,这可能不是您想要的,但对于MCVE来说,这是最简单的选择。
  • 而不是两个<argLine>标签,其中只有一个将被Surefire拾取,您必须在同一标签内指定两个--add-opens选项。您可以方便地使用换行符分隔它们。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SO_Java_AddOpens_69753263</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<surefire.plugin.version>2.22.2</surefire.plugin.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</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
--add-opens java.base/java.util=ALL-UNNAMED
</argLine>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

测试代码和helper enum:

package org.example;
public enum PaymentMethodType {
FOO, BAR, ZOT
}

我将您的测试用例简化如下:

package org.example;
import org.junit.Test;
import java.lang.reflect.Field;
import java.util.EnumSet;
import static org.junit.Assert.assertEquals;
public class MyTest {
@Test
public void testThatDeepCopyCopiesEmptySet() throws NoSuchFieldException, IllegalAccessException {
EnumSet<PaymentMethodType> src = EnumSet.noneOf(PaymentMethodType.class);
Field elementType = EnumSet.class.getDeclaredField("elementType");
elementType.setAccessible(true);
Class<?> srcType = (Class<?>) elementType.get(src);
assertEquals(PaymentMethodType.class, srcType);
}
}

相关内容

最新更新