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.ASTCompilationUnit;
9 import net.sourceforge.pmd.ast.ASTImportDeclaration;
10 import net.sourceforge.pmd.ast.ASTLiteral;
11 import net.sourceforge.pmd.ast.ASTName;
12 import net.sourceforge.pmd.ast.ASTPrimaryExpression;
13 import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
14 import net.sourceforge.pmd.ast.ASTPrimarySuffix;
15 import net.sourceforge.pmd.typeresolution.TypeHelper;
16
17 /**
18 * Avoid instantiating Boolean objects; you can reference Boolean.TRUE,
19 * Boolean.FALSE, or call Boolean.valueOf() instead.
20 *
21 * <pre>
22 * public class Foo {
23 * Boolean bar = new Boolean("true"); // just do a Boolean
24 * bar = Boolean.TRUE; //ok
25 * Boolean buz = Boolean.valueOf(false); // just do a Boolean buz = Boolean.FALSE;
26 * }
27 * </pre>
28 */
29 public class BooleanInstantiation extends AbstractRule {
30
31
32
33
34
35
36 private boolean customBoolean;
37
38 public Object visit(ASTCompilationUnit decl,Object data) {
39
40 customBoolean = false;
41
42 return super.visit(decl, data);
43 }
44
45 public Object visit(ASTImportDeclaration decl,Object data) {
46
47 if ( decl.getImportedName().endsWith("Boolean") && ! decl.getImportedName().equals("java.lang"))
48 {
49 customBoolean = true;
50 }
51 return super.visit(decl, data);
52 }
53
54 public Object visit(ASTAllocationExpression node, Object data) {
55
56 if ( ! customBoolean ) {
57 if (node.findChildrenOfType(ASTArrayDimsAndInits.class).size() > 0) {
58 return super.visit(node, data);
59 }
60 if (TypeHelper.isA((ASTClassOrInterfaceType) node.jjtGetChild(0), Boolean.class)) {
61 super.addViolation(data, node);
62 return data;
63 }
64 }
65 return super.visit(node, data);
66 }
67
68 public Object visit(ASTPrimaryPrefix node, Object data) {
69
70 if ( ! customBoolean )
71 {
72 if (node.jjtGetNumChildren() == 0 || !node.jjtGetChild(0).getClass().equals(ASTName.class)) {
73 return super.visit(node, data);
74 }
75
76 if ("Boolean.valueOf".equals(((ASTName) node.jjtGetChild(0)).getImage())
77 || "java.lang.Boolean.valueOf".equals(((ASTName) node.jjtGetChild(0)).getImage())) {
78 ASTPrimaryExpression parent = (ASTPrimaryExpression) node.jjtGetParent();
79 ASTPrimarySuffix suffix = parent.getFirstChildOfType(ASTPrimarySuffix.class);
80 if (suffix == null) {
81 return super.visit(node, data);
82 }
83 ASTPrimaryPrefix prefix = suffix.getFirstChildOfType(ASTPrimaryPrefix.class);
84 if (prefix == null) {
85 return super.visit(node, data);
86 }
87
88 if (prefix.getFirstChildOfType(ASTBooleanLiteral.class) != null) {
89 super.addViolation(data, node);
90 return data;
91 }
92 ASTLiteral literal = prefix.getFirstChildOfType(ASTLiteral.class);
93 if (literal != null && ("\"true\"".equals(literal.getImage()) || "\"false\"".equals(literal.getImage()))) {
94 super.addViolation(data, node);
95 return data;
96 }
97 }
98 }
99 return super.visit(node, data);
100 }
101 }