View Javadoc

1   package net.sourceforge.pmd.rules.strings;
2   
3   import net.sourceforge.pmd.AbstractRule;
4   import net.sourceforge.pmd.ast.ASTAdditiveExpression;
5   import net.sourceforge.pmd.ast.ASTLiteral;
6   import net.sourceforge.pmd.ast.ASTName;
7   import net.sourceforge.pmd.ast.ASTPrimaryExpression;
8   import net.sourceforge.pmd.ast.ASTPrimaryPrefix;
9   import net.sourceforge.pmd.ast.Node;
10  import net.sourceforge.pmd.ast.SimpleJavaNode;
11  import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
12  
13  public class UselessStringValueOf extends AbstractRule {
14  
15      public Object visit(ASTPrimaryPrefix node, Object data) {
16          if (node.jjtGetNumChildren() == 0 ||
17              !(node.jjtGetChild(0) instanceof ASTName)) {
18              return super.visit(node, data);
19          }
20  
21          String image = ((ASTName) node.jjtGetChild(0)).getImage();
22  
23          if ("String.valueOf".equals(image)) {
24              Node parent = node.jjtGetParent();
25              if (parent.jjtGetNumChildren() != 2) {
26                  return super.visit(node, data);
27              }
28              SimpleJavaNode gp = (SimpleJavaNode) parent.jjtGetParent();
29              if (parent instanceof ASTPrimaryExpression &&
30                      gp instanceof ASTAdditiveExpression &&
31                      "+".equals(gp.getImage())) {
32                  boolean ok = false;
33                  if (gp.jjtGetChild(0) == parent) {
34                      ok = !isPrimitive(gp.jjtGetChild(1));
35                  } else  {
36                      for (int i = 0; !ok && gp.jjtGetChild(i) != parent; i++) {
37                          ok = !isPrimitive(gp.jjtGetChild(i));
38                      }
39                  }
40                  if (ok) {
41                      super.addViolation(data, node);
42                      return data;
43                  }
44              }
45          }
46          return super.visit(node, data);
47      }
48  
49      private static boolean isPrimitive(Node parent) {
50          boolean result = false;
51          if (parent instanceof ASTPrimaryExpression && parent.jjtGetNumChildren() == 1) {
52              Node child = parent.jjtGetChild(0);
53              if (child instanceof ASTPrimaryPrefix && child.jjtGetNumChildren() == 1) {
54                  Node gc = child.jjtGetChild(0);
55                  if (gc instanceof ASTName) {
56                      ASTName name = (ASTName) gc;
57                      if (name.getNameDeclaration() instanceof VariableNameDeclaration) {
58                          VariableNameDeclaration nd = (VariableNameDeclaration) name.getNameDeclaration();
59                          if (nd.isPrimitiveType()) {
60                              result = true;
61                          }
62                      }
63                  } else if (gc instanceof ASTLiteral) {
64                      result = !((ASTLiteral) gc).isStringLiteral();
65                  }
66              }
67          }
68          return result;
69      }
70      
71  }