KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > rules > basic > BooleanInstantiation


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     public Object JavaDoc visit(ASTAllocationExpression node, Object JavaDoc data) {
29
30         if (node.findChildrenOfType(ASTArrayDimsAndInits.class).size() > 0) {
31             return super.visit(node, data);
32         }
33         String JavaDoc 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 JavaDoc visit(ASTPrimaryPrefix node, Object JavaDoc 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