Files
openide/java/java-impl/resources/inspectionDescriptions/Java9CollectionFactory.html
Leonid Shalupov 40795fe787 IJI-2422: community/java: move resources under resources root
GitOrigin-RevId: 8b2b63fc6db476ca0c2cfe5cadd84db6c4236d0f
2025-02-05 04:43:28 +00:00

38 lines
1.7 KiB
HTML

<html>
<body>
Reports <code>java.util.Collections</code> unmodifiable collection calls
that can be converted to newer collection factory methods.
These can be replaced with e.g. <code>List.of()</code> or <code>Set.of()</code> introduced in Java 9
or <code>List.copyOf()</code> introduced in Java 10.
<p>Note that in contrast to <code>java.util.Collections</code> methods, Java 9 collection factory methods:
<ul>
<li>Do not accept <code>null</code> values.
<li>Require unique set elements and map keys.
<li>Do not accept <code>null</code> arguments to query methods like <code>List.contains()</code> or <code>Map.get()</code> of the collections returned.
</ul>
<p>When these cases are violated, exceptions are thrown.
This can change the semantics of the code after the migration.</p>
<p>Example:
<pre><code>
List&lt;Integer> even = Collections.unmodifiableList(
Arrays.asList(2, 4, 6, 8, 10, 2));
List&lt;Integer> evenCopy = Collections.unmodifiableList(
new ArrayList&lt;>(list1));
</code></pre>
<p>After the quick-fix is applied:</p>
<pre><code>
List&lt;Integer> even = List.of(2, 4, 6, 8, 10, 2);
List&lt;Integer> evenCopy = List.copyOf(list);
</code></pre>
<!-- tooltip end -->
<p>
Use the <b>Do not warn when content is non-constant</b> option to report only in cases when the supplied arguments are compile-time constants.
This reduces the chances that the behavior changes,
because it's not always possible to statically check whether original elements are unique and not <code>null</code>.
<p>
Use the <b>Suggest 'Map.ofEntries'</b> option to suggest replacing unmodifiable maps with more than 10 entries with <code>Map.ofEntries()</code>.
<p><small>New in 2017.2</small></p>
</body>
</html>