Clover coverage report - PMD - 3.9
Coverage timestamp: Tue Dec 19 2006 09:38:44 EST
file stats: LOC: 71   Methods: 2
NCLOC: 51   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
BooleanInstantiation.java 75% 84% 100% 81.4%
coverage coverage
 1    package net.sourceforge.pmd.rules.basic;
 2   
 3    import net.sourceforge.pmd.AbstractRule;
 4    import net.sourceforge.pmd.ast.ASTAllocationExpression;
 5    import net.sourceforge.pmd.ast.ASTArrayDimsAndInits;
 6    import net.sourceforge.pmd.ast.ASTBooleanLiteral;
 7    import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
 8    import net.sourceforge.pmd.ast.ASTLiteral;
 9    import net.sourceforge.pmd.ast.ASTName;
 10    import net.sourceforge.pmd.ast.ASTPrimaryExpression;
 11    import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
 12    import net.sourceforge.pmd.ast.ASTPrimarySuffix;
 13   
 14    /**
 15    * Avoid instantiating Boolean objects; you can reference Boolean.TRUE,
 16    * Boolean.FALSE, or call Boolean.valueOf() instead.
 17    *
 18    * <pre>
 19    * public class Foo {
 20    * Boolean bar = new Boolean("true"); // just do a Boolean
 21    * bar = Boolean.TRUE; //ok
 22    * Boolean buz = Boolean.valueOf(false); // just do a Boolean buz = Boolean.FALSE;
 23    * }
 24    * </pre>
 25    */
 26    public class BooleanInstantiation extends AbstractRule {
 27   
 28  3 public Object visit(ASTAllocationExpression node, Object data) {
 29   
 30  3 if (node.findChildrenOfType(ASTArrayDimsAndInits.class).size() > 0) {
 31  0 return super.visit(node, data);
 32    }
 33  3 String typeName = ((ASTClassOrInterfaceType) node.jjtGetChild(0)).getImage();
 34  3 if ("Boolean".equals(typeName) || "java.lang.Boolean".equals(typeName)) {
 35  3 super.addViolation(data, node);
 36  3 return data;
 37    }
 38  0 return super.visit(node, data);
 39    }
 40   
 41  23 public Object visit(ASTPrimaryPrefix node, Object data) {
 42   
 43  23 if (node.jjtGetNumChildren() == 0 || !node.jjtGetChild(0).getClass().equals(ASTName.class)) {
 44  8 return super.visit(node, data);
 45    }
 46   
 47  15 if ("Boolean.valueOf".equals(((ASTName) node.jjtGetChild(0)).getImage())
 48    || "java.lang.Boolean.valueOf".equals(((ASTName) node.jjtGetChild(0)).getImage())) {
 49  6 ASTPrimaryExpression parent = (ASTPrimaryExpression) node.jjtGetParent();
 50  6 ASTPrimarySuffix suffix = (ASTPrimarySuffix) parent.getFirstChildOfType(ASTPrimarySuffix.class);
 51  6 if (suffix == null) {
 52  0 return super.visit(node, data);
 53    }
 54  6 ASTPrimaryPrefix prefix = (ASTPrimaryPrefix) suffix.getFirstChildOfType(ASTPrimaryPrefix.class);
 55  6 if (prefix == null) {
 56  0 return super.visit(node, data);
 57    }
 58   
 59  6 if (prefix.getFirstChildOfType(ASTBooleanLiteral.class) != null) {
 60  2 super.addViolation(data, node);
 61  2 return data;
 62    }
 63  4 ASTLiteral literal = (ASTLiteral) prefix.getFirstChildOfType(ASTLiteral.class);
 64  4 if (literal != null && ("\"true\"".equals(literal.getImage()) || "\"false\"".equals(literal.getImage()))) {
 65  3 super.addViolation(data, node);
 66  3 return data;
 67    }
 68    }
 69  10 return super.visit(node, data);
 70    }
 71    }