Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |

file stats: | LOC: | 93 |  | Methods: | 5
| NCLOC: | 46 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL
SimplifyBooleanReturns.java | 78.6% | 84.2% | 100% | 84.2% |

1 |  | /**
2 |  | * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 |  | */
4 |  | package net.sourceforge.pmd.rules;
5 |  |
6 |  | import net.sourceforge.pmd.AbstractRule;
7 |  | import net.sourceforge.pmd.ast.ASTBlock;
8 |  | import net.sourceforge.pmd.ast.ASTBlockStatement;
9 |  | import net.sourceforge.pmd.ast.ASTBooleanLiteral;
10 |  | import net.sourceforge.pmd.ast.ASTIfStatement;
11 |  | import net.sourceforge.pmd.ast.ASTReturnStatement;
12 |  | import net.sourceforge.pmd.ast.ASTStatement;
13 |  | import net.sourceforge.pmd.ast.SimpleNode;
14 |  |
15 |  | public class SimplifyBooleanReturns extends AbstractRule {
16 |  |
17 | 3 | public Object visit(ASTIfStatement node, Object data) {
18 |  | // only deal with if..then..else stmts
19 | 3 | if (node.jjtGetNumChildren() != 3) {
20 | 0 | return super.visit(node, data);
21 |  | }
22 |  |
23 |  | // don't bother if either the if or the else block is empty
24 | 3 | if (node.jjtGetChild(1).jjtGetNumChildren() == 0 || node.jjtGetChild(2).jjtGetNumChildren() == 0) {
25 | 0 | return super.visit(node, data);
26 |  | }
27 |  |
28 |  | // first case:
29 |  | // If
30 |  | // Expr
31 |  | // Statement
32 |  | // ReturnStatement
33 |  | // Statement
34 |  | // ReturnStatement
35 |  | // i.e.,
36 |  | // if (foo)
37 |  | // return true;
38 |  | // else
39 |  | // return false;
40 |  |
41 |  | // second case
42 |  | // If
43 |  | // Expr
44 |  | // Statement
45 |  | // Block
46 |  | // BlockStatement
47 |  | // Statement
48 |  | // ReturnStatement
49 |  | // Statement
50 |  | // Block
51 |  | // BlockStatement
52 |  | // Statement
53 |  | // ReturnStatement
54 |  | // i.e.,
55 |  | // if (foo) {
56 |  | // return true;
57 |  | // } else {
58 |  | // return false;
59 |  | // }
60 | 3 | if (node.jjtGetChild(1).jjtGetChild(0) instanceof ASTReturnStatement && node.jjtGetChild(2).jjtGetChild(0) instanceof ASTReturnStatement && terminatesInBooleanLiteral((SimpleNode) node.jjtGetChild(1).jjtGetChild(0)) && terminatesInBooleanLiteral((SimpleNode) node.jjtGetChild(2).jjtGetChild(0))) {
61 | 1 | addViolation(data, node);
62 | 2 | } else if (hasOneBlockStmt((SimpleNode) node.jjtGetChild(1)) && hasOneBlockStmt((SimpleNode) node.jjtGetChild(2)) && terminatesInBooleanLiteral((SimpleNode) node.jjtGetChild(1).jjtGetChild(0)) && terminatesInBooleanLiteral((SimpleNode) node.jjtGetChild(2).jjtGetChild(0))) {
63 | 1 | addViolation(data, node);
64 |  | }
65 |  |
66 | 3 | return super.visit(node, data);
67 |  | }
68 |  |
69 | 4 | private boolean hasOneBlockStmt(SimpleNode node) {
70 | 4 | return node.jjtGetChild(0) instanceof ASTBlock && node.jjtGetChild(0).jjtGetNumChildren() == 1 && node.jjtGetChild(0).jjtGetChild(0) instanceof ASTBlockStatement && node.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0) instanceof ASTStatement && node.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0) instanceof ASTReturnStatement;
71 |  | }
72 |  |
73 | 4 | private boolean terminatesInBooleanLiteral(SimpleNode node) {
74 | 4 | return eachNodeHasOneChild(node) && (getLastChild(node) instanceof ASTBooleanLiteral);
75 |  | }
76 |  |
77 | 30 | private boolean eachNodeHasOneChild(SimpleNode node) {
78 | 30 | if (node.jjtGetNumChildren() > 1) {
79 | 0 | return false;
80 |  | }
81 | 30 | if (node.jjtGetNumChildren() == 0) {
82 | 4 | return true;
83 |  | }
84 | 26 | return eachNodeHasOneChild((SimpleNode) node.jjtGetChild(0));
85 |  | }
86 |  |
87 | 30 | private SimpleNode getLastChild(SimpleNode node) {
88 | 30 | if (node.jjtGetNumChildren() == 0) {
89 | 4 | return node;
90 |  | }
91 | 26 | return getLastChild((SimpleNode) node.jjtGetChild(0));
92 |  | }
93 |  | }