Files
codeql/java/ql/src/Language Abuse/DubiousDowncastOfThis.qhelp
2018-08-30 10:48:05 +01:00

30 lines
1.0 KiB
XML

<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
Downcasting <code>this</code> to a derived type creates a dependency cycle. Derived types already depend
on their parent type and the cast creates a dependency in the other direction.
</p>
<p>Dependency cycles should be avoided as they make code both difficult to read and difficult to test.
In addition, a type should not know about its specific descendants, even though it may impose some
constraints on them as a group (for example, abstract classes may require every derived type to implement
a method with a specific signature).</p>
</overview>
<recommendation>
<p>
The base and derived types should be redesigned so that there is no need for the base type to depend
on the types deriving from it.</p>
</recommendation>
<example>
<p>In this example, <code>BadBase</code> introduces a dependency cycle with <code>Derived</code> by
coercing the type of <code>this</code> to a derived type. </p>
<sample src="DubiousDowncastOfThis.java" />
</example>
</qhelp>