mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-04 17:20:55 +07:00
42 lines
1.1 KiB
HTML
42 lines
1.1 KiB
HTML
<html>
|
|
<body>
|
|
Reports access to a non-<code>final</code> field inside a <code>compareTo()</code> implementation.
|
|
<p>
|
|
Such access may result in <code>compareTo()</code>
|
|
returning different results at different points in the object's lifecycle, which may in turn cause problems when
|
|
using the standard collections classes, for example <code>java.util.TreeSet</code>.
|
|
</p>
|
|
<p>
|
|
A quick-fix to make the field <code>final</code> is available
|
|
only when there is no write access to the field, otherwise no fixes are suggested.
|
|
</p>
|
|
<p><b>Example:</b></p>
|
|
<pre><code>
|
|
class Foo implements Comparable<Foo>{
|
|
private int index;
|
|
Foo(int idx) {
|
|
index = idx;
|
|
}
|
|
@Override
|
|
public int compareTo(Foo foo) {
|
|
return Integer.compare(this.index, foo.index);
|
|
}
|
|
}
|
|
</code></pre>
|
|
<p>After the quick-fix is applied:</p>
|
|
<pre><code>
|
|
class Foo implements Comparable<Foo>{
|
|
private final int index;
|
|
Foo(int idx) {
|
|
index = idx;
|
|
}
|
|
@Override
|
|
public int compareTo(Foo foo) {
|
|
return Integer.compare(this.index, foo.index);
|
|
}
|
|
}
|
|
</code></pre>
|
|
<!-- tooltip end -->
|
|
|
|
</body>
|
|
</html> |