原错误:
java.util.ImmutableCollections$Set12.contains(java.lang.Object) accessible: module java.base does not “opens java.util” to unnamed module
网上的答案是需要修改jvm参数,比如--add-opens java.base/java.util=ALL-UNNAMED
,但按照官方文档的描述。
–add-opens
Some libraries do deep reflection, meaning setAccessible(true) , so they can access all members, including private ones. You can grant this access using the --add-opens option on the java command line. No warning messages are generated as a result of using this option.
一些库会进行深度反射,这意味着setAccessible(true),因此它们可以访问所有成员,包括私有成员。您可以使用 java 命令行上的 --add-opens 选项授予此访问权限。使用此选项不会生成任何警告消息。
建议使用--add-opens
授予权限并不是一个好的选择。我发现ImmutableCollections
一般来源与java17的of语法。比如我的错误就源于:
Set<Integer> ids = Set.of(1);
这是Set.of
的源码:
static <E> Set<E> of(E e1) {
return new ImmutableCollections.Set12<>(e1);
}
ImmutableCollections
里面的Set12
等类型并非被public修饰,因此如果需配合mybatis的test中的语法:
<if test="ids.contains(6)">
<!-- ... -->
</if>
可考虑不适用java的of语法,而显示使用set用public修饰的子类,比如HashSet
:
Set<Integer> ids = new HashSet<>(1);
ids.add(1);