Clover coverage report - PMD - 3.9
Coverage timestamp: Tue Dec 19 2006 09:38:44 EST
file stats: LOC: 66   Methods: 2
NCLOC: 59   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
UselessStringValueOf.java 85% 96.2% 100% 91.7%
coverage coverage
 1    package net.sourceforge.pmd.rules.strings;
 2   
 3    import net.sourceforge.pmd.AbstractRule;
 4    import net.sourceforge.pmd.ast.ASTAdditiveExpression;
 5    import net.sourceforge.pmd.ast.ASTName;
 6    import net.sourceforge.pmd.ast.ASTPrimaryExpression;
 7    import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
 8    import net.sourceforge.pmd.ast.Node;
 9    import net.sourceforge.pmd.ast.SimpleJavaNode;
 10    import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
 11   
 12    public class UselessStringValueOf extends AbstractRule {
 13   
 14  19 public Object visit(ASTPrimaryPrefix node, Object data) {
 15  19 if (node.jjtGetNumChildren() == 0 ||
 16    !(node.jjtGetChild(0) instanceof ASTName)) {
 17  4 return super.visit(node, data);
 18    }
 19   
 20  15 String image = ((ASTName) node.jjtGetChild(0)).getImage();
 21   
 22  15 if ("String.valueOf".equals(image)) {
 23  6 Node parent = node.jjtGetParent();
 24  6 if (parent.jjtGetNumChildren() != 2) {
 25  0 return super.visit(node, data);
 26    }
 27  6 SimpleJavaNode gp = (SimpleJavaNode) parent.jjtGetParent();
 28  6 if (parent instanceof ASTPrimaryExpression &&
 29    gp instanceof ASTAdditiveExpression &&
 30    "+".equals(gp.getImage())) {
 31  5 boolean ok = false;
 32  5 if (gp.jjtGetChild(0) == parent) {
 33  2 ok = !isPrimitive(gp.jjtGetChild(1));
 34    } else {
 35  3 for (int i = 0; !ok && gp.jjtGetChild(i) != parent; i++) {
 36  3 ok = !isPrimitive(gp.jjtGetChild(i));
 37    }
 38    }
 39  5 if (ok) {
 40  2 super.addViolation(data, node);
 41  2 return data;
 42    }
 43    }
 44    }
 45  13 return super.visit(node, data);
 46    }
 47   
 48  5 private static boolean isPrimitive(Node parent) {
 49  5 boolean result = false;
 50  5 if (parent instanceof ASTPrimaryExpression &&
 51    parent.jjtGetNumChildren() == 1 &&
 52    parent.jjtGetChild(0) instanceof ASTPrimaryPrefix &&
 53    parent.jjtGetChild(0).jjtGetNumChildren() == 1 &&
 54    parent.jjtGetChild(0).jjtGetChild(0) instanceof ASTName) {
 55  3 ASTName name = (ASTName) parent.jjtGetChild(0).jjtGetChild(0);
 56  3 if (name.getNameDeclaration() instanceof VariableNameDeclaration) {
 57  3 VariableNameDeclaration nd = (VariableNameDeclaration) name.getNameDeclaration();
 58  3 if (nd.isPrimitiveType()) {
 59  3 result = true;
 60    }
 61    }
 62    }
 63  5 return result;
 64    }
 65   
 66    }