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

29 lines
1.2 KiB
HTML

<html>
<body>
Reports loops that can be replaced with a single <code>String.repeat()</code> method (available since Java 11).
<p><b>Example:</b></p>
<pre><code>
void append(StringBuilder sb, int count, Object obj) {
for (int i = 0; i &lt; count; i++) {
sb.append(obj);
}
}
</code></pre>
<p>After the quick-fix is applied:</p>
<pre><code>
void append(StringBuilder sb, int count, Object obj) {
sb.append(String.valueOf(obj).repeat(Math.max(0, count)));
}
</code></pre>
<!-- tooltip end -->
<p>
By default, the inspection may wrap <code>count</code> with <code>Math.max(0, count)</code> if it cannot prove statically that <code>count</code> is
not negative. This is done to prevent possible semantics change, as <code>String.repeat()</code> rejects negative numbers.
Use the <b>Add Math.max(0,count) to avoid possible semantics change</b> option to disable this behavior if required.</p>
<p>Similarly, a string you want to repeat can be wrapped in
<code>String.valueOf</code> to prevent possible <code>NullPointerException</code> if it's unknown whether it can be <code>null</code>.
</p>
<p>This inspection only reports if the language level of the project or module is 11 or higher.</p>
<p><small>New in 2019.1</small></p>
</body>
</html>