Clover coverage report - PMD - 3.3
Coverage timestamp: Thu Sep 15 2005 17:59:57 EDT
file stats: LOC: 253   Methods: 13
NCLOC: 207   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
StatementAndBraceFinder.java 81.4% 86.3% 92.3% 85%
coverage coverage
 1    /*
 2    * Created on 11.07.2004
 3    */
 4    package net.sourceforge.pmd.dfa;
 5   
 6    import net.sourceforge.pmd.ast.ASTBreakStatement;
 7    import net.sourceforge.pmd.ast.ASTConstructorDeclaration;
 8    import net.sourceforge.pmd.ast.ASTContinueStatement;
 9    import net.sourceforge.pmd.ast.ASTDoStatement;
 10    import net.sourceforge.pmd.ast.ASTExpression;
 11    import net.sourceforge.pmd.ast.ASTForInit;
 12    import net.sourceforge.pmd.ast.ASTForStatement;
 13    import net.sourceforge.pmd.ast.ASTForUpdate;
 14    import net.sourceforge.pmd.ast.ASTIfStatement;
 15    import net.sourceforge.pmd.ast.ASTMethodDeclaration;
 16    import net.sourceforge.pmd.ast.ASTReturnStatement;
 17    import net.sourceforge.pmd.ast.ASTStatement;
 18    import net.sourceforge.pmd.ast.ASTStatementExpression;
 19    import net.sourceforge.pmd.ast.ASTSwitchLabel;
 20    import net.sourceforge.pmd.ast.ASTSwitchStatement;
 21    import net.sourceforge.pmd.ast.ASTVariableDeclarator;
 22    import net.sourceforge.pmd.ast.ASTWhileStatement;
 23    import net.sourceforge.pmd.ast.JavaParserVisitorAdapter;
 24    import net.sourceforge.pmd.ast.SimpleNode;
 25   
 26    /**
 27    * @author raik
 28    * <p/>
 29    * Sublayer of DataFlowFacade. Finds all data flow nodes and stores the
 30    * type information (@see StackObject). At last it uses this information to
 31    * link the nodes.
 32    */
 33    public class StatementAndBraceFinder extends JavaParserVisitorAdapter {
 34   
 35    private Structure dataFlow;
 36   
 37  41 public void buildDataFlowFor(SimpleNode node) {
 38  41 if (!(node instanceof ASTMethodDeclaration) && !(node instanceof ASTConstructorDeclaration)) {
 39  1 throw new RuntimeException("Can't build a data flow for anything other than a method or a constructor");
 40    }
 41   
 42  40 this.dataFlow = new Structure();
 43  40 this.dataFlow.createStartNode(node.getBeginLine());
 44  40 this.dataFlow.createNewNode(node);
 45   
 46  40 node.jjtAccept(this, dataFlow);
 47   
 48  40 this.dataFlow.createEndNode(node.getEndLine());
 49   
 50  40 Linker linker = new Linker(dataFlow.getBraceStack(), dataFlow.getContinueBreakReturnStack());
 51  40 try {
 52  40 linker.computePaths();
 53    } catch (LinkerException e) {
 54  0 e.printStackTrace();
 55    } catch (SequenceException e) {
 56  0 e.printStackTrace();
 57    }
 58    }
 59   
 60  67 public Object visit(ASTStatementExpression node, Object data) {
 61  67 if (!(data instanceof Structure)) {
 62  0 return data;
 63    }
 64  67 Structure dataFlow = (Structure) data;
 65  67 dataFlow.createNewNode(node);
 66  67 return super.visit(node, data);
 67    }
 68   
 69  36 public Object visit(ASTVariableDeclarator node, Object data) {
 70  36 if (!(data instanceof Structure)) {
 71  0 return data;
 72    }
 73  36 Structure dataFlow = (Structure) data;
 74  36 dataFlow.createNewNode(node);
 75  36 return super.visit(node, data);
 76    }
 77   
 78  121 public Object visit(ASTExpression node, Object data) {
 79  121 if (!(data instanceof Structure)) {
 80  0 return data;
 81    }
 82  121 Structure dataFlow = (Structure) data;
 83   
 84    // TODO what about throw stmts?
 85  121 if (node.jjtGetParent() instanceof ASTIfStatement) {
 86  24 dataFlow.createNewNode(node); // START IF
 87  24 dataFlow.pushOnStack(NodeType.IF_EXPR, dataFlow.getLast());
 88  97 } else if (node.jjtGetParent() instanceof ASTWhileStatement) {
 89  3 dataFlow.createNewNode(node); // START WHILE
 90  3 dataFlow.pushOnStack(NodeType.WHILE_EXPR, dataFlow.getLast());
 91  94 } else if (node.jjtGetParent() instanceof ASTSwitchStatement) {
 92  3 dataFlow.createNewNode(node); // START SWITCH
 93  3 dataFlow.pushOnStack(NodeType.SWITCH_START, dataFlow.getLast());
 94  91 } else if (node.jjtGetParent() instanceof ASTForStatement) {
 95  20 dataFlow.createNewNode(node); // FOR EXPR
 96  20 dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast());
 97  71 } else if (node.jjtGetParent() instanceof ASTDoStatement) {
 98  3 dataFlow.createNewNode(node); // DO EXPR
 99  3 dataFlow.pushOnStack(NodeType.DO_EXPR, dataFlow.getLast());
 100    }
 101   
 102  121 return super.visit(node, data);
 103    }
 104   
 105  20 public Object visit(ASTForInit node, Object data) {
 106  20 if (!(data instanceof Structure)) {
 107  0 return data;
 108    }
 109  20 Structure dataFlow = (Structure) data;
 110  20 super.visit(node, data);
 111  20 dataFlow.pushOnStack(NodeType.FOR_INIT, dataFlow.getLast());
 112  20 this.addForExpressionNode(node, dataFlow);
 113  20 return data;
 114    }
 115   
 116  20 public Object visit(ASTForUpdate node, Object data) {
 117  20 if (!(data instanceof Structure)) {
 118  0 return data;
 119    }
 120  20 Structure dataFlow = (Structure) data;
 121  20 this.addForExpressionNode(node, dataFlow);
 122  20 super.visit(node, data);
 123  20 dataFlow.pushOnStack(NodeType.FOR_UPDATE, dataFlow.getLast());
 124  20 return data;
 125    }
 126   
 127    // ----------------------------------------------------------------------------
 128    // BRANCH OUT
 129   
 130  168 public Object visit(ASTStatement node, Object data) {
 131  168 if (!(data instanceof Structure)) {
 132  0 return data;
 133    }
 134  168 Structure dataFlow = (Structure) data;
 135   
 136  168 if (node.jjtGetParent() instanceof ASTForStatement) {
 137  25 this.addForExpressionNode(node, dataFlow);
 138  25 dataFlow.pushOnStack(NodeType.FOR_BEFORE_FIRST_STATEMENT, dataFlow.getLast());
 139  143 } else if (node.jjtGetParent() instanceof ASTDoStatement) {
 140  3 dataFlow.pushOnStack(NodeType.DO_BEFORE_FIRST_STATEMENT, dataFlow.getLast());
 141  3 dataFlow.createNewNode((SimpleNode) node.jjtGetParent());
 142    }
 143   
 144  168 super.visit(node, data);
 145   
 146  168 if (node.jjtGetParent() instanceof ASTIfStatement) {
 147  35 ASTIfStatement st = (ASTIfStatement) node.jjtGetParent();
 148  35 if (!st.hasElse()) {
 149  13 dataFlow.pushOnStack(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE, dataFlow.getLast());
 150  22 } else if (st.hasElse() && !st.jjtGetChild(1).equals(node)) {
 151  11 dataFlow.pushOnStack(NodeType.ELSE_LAST_STATEMENT, dataFlow.getLast());
 152    } else {
 153  11 dataFlow.pushOnStack(NodeType.IF_LAST_STATEMENT, dataFlow.getLast());
 154    }
 155  133 } else if (node.jjtGetParent() instanceof ASTWhileStatement) {
 156  3 dataFlow.pushOnStack(NodeType.WHILE_LAST_STATEMENT, dataFlow.getLast());
 157  130 } else if (node.jjtGetParent() instanceof ASTForStatement) {
 158  25 dataFlow.pushOnStack(NodeType.FOR_END, dataFlow.getLast());
 159    }
 160  168 return data;
 161    }
 162   
 163  3 public Object visit(ASTSwitchStatement node, Object data) {
 164  3 if (!(data instanceof Structure)) {
 165  0 return data;
 166    }
 167  3 Structure dataFlow = (Structure) data;
 168  3 super.visit(node, data);
 169  3 dataFlow.pushOnStack(NodeType.SWITCH_END, dataFlow.getLast());
 170  3 return data;
 171    }
 172   
 173  5 public Object visit(ASTSwitchLabel node, Object data) {
 174  5 if (!(data instanceof Structure)) {
 175  0 return data;
 176    }
 177  5 Structure dataFlow = (Structure) data;
 178    //super.visit(node, data);
 179  5 if (node.jjtGetNumChildren() == 0) {
 180  2 dataFlow.pushOnStack(NodeType.SWITCH_LAST_DEFAULT_STATEMENT, dataFlow.getLast());
 181    } else {
 182  3 dataFlow.pushOnStack(NodeType.CASE_LAST_STATEMENT, dataFlow.getLast());
 183    }
 184  5 return data;
 185    }
 186   
 187  5 public Object visit(ASTBreakStatement node, Object data) {
 188  5 if (!(data instanceof Structure)) {
 189  0 return data;
 190    }
 191  5 Structure dataFlow = (Structure) data;
 192  5 dataFlow.createNewNode(node);
 193  5 dataFlow.pushOnStack(NodeType.BREAK_STATEMENT, dataFlow.getLast());
 194  5 return super.visit(node, data);
 195    }
 196   
 197   
 198  1 public Object visit(ASTContinueStatement node, Object data) {
 199  1 if (!(data instanceof Structure)) {
 200  0 return data;
 201    }
 202  1 Structure dataFlow = (Structure) data;
 203  1 dataFlow.createNewNode(node);
 204  1 dataFlow.pushOnStack(NodeType.CONTINUE_STATEMENT, dataFlow.getLast());
 205  1 return super.visit(node, data);
 206    }
 207   
 208  0 public Object visit(ASTReturnStatement node, Object data) {
 209  0 if (!(data instanceof Structure)) {
 210  0 return data;
 211    }
 212  0 Structure dataFlow = (Structure) data;
 213  0 dataFlow.createNewNode(node);
 214  0 dataFlow.pushOnStack(NodeType.RETURN_STATEMENT, dataFlow.getLast());
 215  0 return super.visit(node, data);
 216    }
 217   
 218    /*
 219    * The method handles the special "for" loop. It creates always an
 220    * expression node even if the loop looks like for(;;).
 221    * */
 222  65 private void addForExpressionNode(SimpleNode node, Structure dataFlow) {
 223  65 ASTForStatement parent = (ASTForStatement) node.jjtGetParent();
 224  65 boolean hasExpressionChild = false;
 225  65 boolean hasForInitNode = false;
 226  65 boolean hasForUpdateNode = false;
 227   
 228  65 for (int i = 0; i < parent.jjtGetNumChildren(); i++) {
 229  237 if (parent.jjtGetChild(i) instanceof ASTExpression)
 230  56 hasExpressionChild = true;
 231  181 else if (parent.jjtGetChild(i) instanceof ASTForUpdate)
 232  58 hasForUpdateNode = true;
 233  123 else if (parent.jjtGetChild(i) instanceof ASTForInit)
 234  58 hasForInitNode = true;
 235    }
 236  65 if (!hasExpressionChild) {
 237  9 if (node instanceof ASTForInit) {
 238  2 dataFlow.createNewNode(node);
 239  2 dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast());
 240  7 } else if (node instanceof ASTForUpdate) {
 241  2 if (!hasForInitNode) {
 242  1 dataFlow.createNewNode(node);
 243  1 dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast());
 244    }
 245  5 } else if (node instanceof ASTStatement) {
 246  5 if (!hasForInitNode && !hasForUpdateNode) {
 247  2 dataFlow.createNewNode(node);
 248  2 dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast());
 249    }
 250    }
 251    }
 252    }
 253    }