JDK9 immutable factory methods and rules about mutable fields/members


I didn’t find anything on this topic, but this is bugging me a bit. Since Java 9 the java.util package provides immutable static factory methods for collections. But to me it seems that these are not (completely?) integrated in the following rules

For example, it raises the first warning with

public static final Set<String> SOME_WORDS = Set.of(“some”, “words”);

but not with

public static final Set<String> SOME_WORDS = Collections.unmodifiableSet(Set.of(“some”, “words”));

even though Set.of already provides an immutable set.

I think these ‘new’ factory methods should be taken into account with these rules.

Hi, I was having a look at the sonar-java repository on github earlier and noticed a pull request for the new factory methods. There is already a Jira ticket [1], which was closed today and the pull request was merged. As per description both S2384 and S2386 were updated.

[1] https://jira.sonarsource.com/browse/SONARJAVA-3086