mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 22:51:17 +07:00
29 lines
1.2 KiB
HTML
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 < 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> |