Clover coverage report - PMD - 3.9 Coverage timestamp: Tue Dec 19 2006 09:38:44 EST FRAMES NO FRAMES |
file stats: | LOC: | 42 |  | Methods: | 4
| NCLOC: | 33 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
AvoidInstantiatingObjectsInLoops.java | 87.5% | 91.7% | 100% | 91.7% |

1 |  | /**
2 |  | * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 |  | */
4 |  | package net.sourceforge.pmd.rules.optimization;
5 |  |
6 |  | import net.sourceforge.pmd.ast.ASTAllocationExpression;
7 |  | import net.sourceforge.pmd.ast.ASTDoStatement;
8 |  | import net.sourceforge.pmd.ast.ASTForStatement;
9 |  | import net.sourceforge.pmd.ast.ASTReturnStatement;
10 |  | import net.sourceforge.pmd.ast.ASTThrowStatement;
11 |  | import net.sourceforge.pmd.ast.ASTWhileStatement;
12 |  |
13 |  | public class AvoidInstantiatingObjectsInLoops extends AbstractOptimizationRule {
14 |  |
15 | 7 | public Object visit(ASTAllocationExpression node, Object data) {
16 | 7 | if (insideLoop(node) && fourthParentNotThrow(node) && fourthParentNotReturn(node)) {
17 | 5 | addViolation(data, node);
18 |  | }
19 | 7 | return data;
20 |  | }
21 |  |
22 | 7 | private boolean fourthParentNotThrow(ASTAllocationExpression node) {
23 | 7 | return !(node.jjtGetParent().jjtGetParent().jjtGetParent().jjtGetParent() instanceof ASTThrowStatement);
24 |  | }
25 |  |
26 | 6 | private boolean fourthParentNotReturn(ASTAllocationExpression node) {
27 | 6 | return !(node.jjtGetParent().jjtGetParent().jjtGetParent().jjtGetParent() instanceof ASTReturnStatement);
28 |  | }
29 |  |
30 | 7 | private boolean insideLoop(ASTAllocationExpression node) {
31 | 7 | if (node.getFirstParentOfType(ASTDoStatement.class) != null) {
32 | 3 | return true;
33 |  | }
34 | 4 | if (node.getFirstParentOfType(ASTWhileStatement.class) != null) {
35 | 1 | return true;
36 |  | }
37 | 3 | if (node.getFirstParentOfType(ASTForStatement.class) != null) {
38 | 3 | return true;
39 |  | }
40 | 0 | return false;
41 |  | }
42 |  | }