-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathChainedInstanceof.ql
More file actions
33 lines (30 loc) · 890 Bytes
/
ChainedInstanceof.ql
File metadata and controls
33 lines (30 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* @name Chain of 'instanceof' tests
* @description Long sequences of type tests on a variable are difficult to maintain.
* @kind problem
* @problem.severity recommendation
* @precision high
* @id java/chained-type-tests
* @tags maintainability
* complexity
* language-features
*/
import java
int instanceofCountForIfChain(IfStmt is) {
exists(int rest |
(
if is.getElse() instanceof IfStmt
then rest = instanceofCountForIfChain(is.getElse())
else rest = 0
) and
(if is.getCondition() instanceof InstanceOfExpr then result = 1 + rest else result = rest)
)
}
from IfStmt is, int n
where
n = instanceofCountForIfChain(is) and
n > 5 and
not exists(IfStmt other | is = other.getElse())
select is,
"This if block performs a chain of " + n +
" type tests - consider alternatives, e.g. polymorphism or the visitor pattern."