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 26 public class BooleanInstantiation extends AbstractRule { 27 28 public Object visit(ASTAllocationExpression node, Object data) { 29 30 if (node.findChildrenOfType(ASTArrayDimsAndInits.class).size() > 0) { 31 return super.visit(node, data); 32 } 33 String typeName = ((ASTClassOrInterfaceType) node.jjtGetChild(0)).getImage(); 34 if ("Boolean".equals(typeName) || "java.lang.Boolean".equals(typeName)) { 35 super.addViolation(data, node); 36 return data; 37 } 38 return super.visit(node, data); 39 } 40 41 public Object visit(ASTPrimaryPrefix node, Object data) { 42 43 if (node.jjtGetNumChildren() == 0 || !node.jjtGetChild(0).getClass().equals(ASTName.class)) { 44 return super.visit(node, data); 45 } 46 47 if ("Boolean.valueOf".equals(((ASTName) node.jjtGetChild(0)).getImage()) 48 || "java.lang.Boolean.valueOf".equals(((ASTName) node.jjtGetChild(0)).getImage())) { 49 ASTPrimaryExpression parent = (ASTPrimaryExpression) node.jjtGetParent(); 50 ASTPrimarySuffix suffix = (ASTPrimarySuffix) parent.getFirstChildOfType(ASTPrimarySuffix.class); 51 if (suffix == null) { 52 return super.visit(node, data); 53 } 54 ASTPrimaryPrefix prefix = (ASTPrimaryPrefix) suffix.getFirstChildOfType(ASTPrimaryPrefix.class); 55 if (prefix == null) { 56 return super.visit(node, data); 57 } 58 59 if (prefix.getFirstChildOfType(ASTBooleanLiteral.class) != null) { 60 super.addViolation(data, node); 61 return data; 62 } 63 ASTLiteral literal = (ASTLiteral) prefix.getFirstChildOfType(ASTLiteral.class); 64 if (literal != null && ("\"true\"".equals(literal.getImage()) || "\"false\"".equals(literal.getImage()))) { 65 super.addViolation(data, node); 66 return data; 67 } 68 } 69 return super.visit(node, data); 70 } 71 } 72 | Popular Tags |