1 package net.sourceforge.pmd.rules; 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.ASTPrimaryExpression; 7 import net.sourceforge.pmd.ast.ASTVariableDeclaratorId; 8 import net.sourceforge.pmd.ast.SimpleNode; 9 import net.sourceforge.pmd.symboltable.NameOccurrence; 10 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 26 public abstract class AbstractPoorMethodCall extends AbstractRule { 27 28 29 33 protected abstract String targetTypename(); 34 35 41 protected abstract String [] methodNames(); 42 43 52 protected abstract boolean isViolationArgument(int argIndex, String arg); 53 54 61 private boolean isNotedMethod(NameOccurrence occurrence) { 62 63 if (occurrence == null) return false; 64 65 String methodCall = occurrence.getImage(); 66 String [] methodNames = methodNames(); 67 68 for (int i=0; i<methodNames.length; i++) { 69 if (methodCall.indexOf(methodNames[i]) != -1) return true; 70 } 71 return false; 72 } 73 74 80 public static boolean isSingleCharAsString(String value) { 81 return value.length() == 3 && value.charAt(0) == '\"'; 82 } 83 84 91 public Object visit(ASTVariableDeclaratorId node, Object data) { 92 93 if (!node.getNameDeclaration().getTypeImage().equals(targetTypename())) { 94 return data; 95 } 96 97 for (Iterator i = node.getUsages().iterator(); i.hasNext();) { 98 NameOccurrence occ = (NameOccurrence) i.next(); 99 if (isNotedMethod(occ.getNameForWhichThisIsAQualifier())) { 100 SimpleNode parent = (SimpleNode)occ.getLocation().jjtGetParent().jjtGetParent(); 101 if (parent instanceof ASTPrimaryExpression) { 102 List additives = parent.findChildrenOfType(ASTAdditiveExpression.class); 104 if (!additives.isEmpty()) { 105 return data; 106 } 107 List literals = parent.findChildrenOfType(ASTLiteral.class); 108 for (int l=0; l<literals.size(); l++) { 109 ASTLiteral literal = (ASTLiteral)literals.get(l); 110 if (isViolationArgument(l, literal.getImage())) { 111 addViolation(data, occ.getLocation()); 112 } 113 } 114 } 115 } 116 } 117 return data; 118 } 119 } 120 121 | Popular Tags |