mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-04 17:20:55 +07:00
38 lines
765 B
HTML
38 lines
765 B
HTML
<html>
|
|
<body>
|
|
Reports usages of <code>instanceof</code> or <code>getClass() == SomeClass.class</code> in which a
|
|
<code>this</code> expression is checked.
|
|
<p>Such expressions indicate a failure of the object-oriented design, and should be replaced by
|
|
polymorphic constructions.</p>
|
|
<p>Example:</p>
|
|
<pre><code>
|
|
class Super {
|
|
void process() {
|
|
if (this instanceof Sub) { // warning
|
|
doSomething();
|
|
} else {
|
|
doSomethingElse();
|
|
}
|
|
}
|
|
}
|
|
|
|
class Sub extends Super {}
|
|
</code></pre>
|
|
<p>To fix the problem, use an overriding method:</p>
|
|
<pre><code>
|
|
class Super {
|
|
void process() {
|
|
doSomethingElse();
|
|
}
|
|
}
|
|
|
|
class Sub extends Super {
|
|
@Override
|
|
void process() {
|
|
doSomething();
|
|
}
|
|
}
|
|
</code></pre>
|
|
<!-- tooltip end -->
|
|
</body>
|
|
</html> |