01: package net.sourceforge.pmd.rules.strings;
02:
03: import net.sourceforge.pmd.AbstractRule;
04: import net.sourceforge.pmd.ast.ASTAdditiveExpression;
05: import net.sourceforge.pmd.ast.ASTName;
06: import net.sourceforge.pmd.ast.ASTPrimaryExpression;
07: import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
08: import net.sourceforge.pmd.ast.Node;
09: import net.sourceforge.pmd.ast.SimpleJavaNode;
10: import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
11:
12: public class UselessStringValueOf extends AbstractRule {
13:
14: public Object visit(ASTPrimaryPrefix node, Object data) {
15: if (node.jjtGetNumChildren() == 0
16: || !(node.jjtGetChild(0) instanceof ASTName)) {
17: return super .visit(node, data);
18: }
19:
20: String image = ((ASTName) node.jjtGetChild(0)).getImage();
21:
22: if ("String.valueOf".equals(image)) {
23: Node parent = node.jjtGetParent();
24: if (parent.jjtGetNumChildren() != 2) {
25: return super .visit(node, data);
26: }
27: SimpleJavaNode gp = (SimpleJavaNode) parent.jjtGetParent();
28: if (parent instanceof ASTPrimaryExpression
29: && gp instanceof ASTAdditiveExpression
30: && "+".equals(gp.getImage())) {
31: boolean ok = false;
32: if (gp.jjtGetChild(0) == parent) {
33: ok = !isPrimitive(gp.jjtGetChild(1));
34: } else {
35: for (int i = 0; !ok && gp.jjtGetChild(i) != parent; i++) {
36: ok = !isPrimitive(gp.jjtGetChild(i));
37: }
38: }
39: if (ok) {
40: super .addViolation(data, node);
41: return data;
42: }
43: }
44: }
45: return super .visit(node, data);
46: }
47:
48: private static boolean isPrimitive(Node parent) {
49: boolean result = false;
50: 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: ASTName name = (ASTName) parent.jjtGetChild(0).jjtGetChild(
56: 0);
57: if (name.getNameDeclaration() instanceof VariableNameDeclaration) {
58: VariableNameDeclaration nd = (VariableNameDeclaration) name
59: .getNameDeclaration();
60: if (nd.isPrimitiveType()) {
61: result = true;
62: }
63: }
64: }
65: return result;
66: }
67:
68: }
|