replaced getImage().equals with utility method hasImageEqualTo (cleaner & handles nulls)

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4692 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Brian Remedios
2006-10-17 00:48:10 +00:00
parent 56a8619c4e
commit e4a9e4128b
15 changed files with 20 additions and 20 deletions

View File

@ -37,7 +37,7 @@ public class IdempotentOperations extends AbstractRule {
return super.visit(node, data);
}
if (!lhs.getImage().equals(rhs.getImage())) {
if (!lhs.hasImageEqualTo(rhs.getImage())) {
return super.visit(node, data);
}

View File

@ -13,7 +13,7 @@ public class MethodWithSameNameAsEnclosingClass extends AbstractRule {
List methods = node.findChildrenOfType(ASTMethodDeclarator.class);
for (Iterator i = methods.iterator(); i.hasNext();) {
ASTMethodDeclarator m = (ASTMethodDeclarator) i.next();
if (m.getImage().equals(node.getImage())) {
if (m.hasImageEqualTo(node.getImage())) {
addViolation(data, m);
}
}

View File

@ -53,7 +53,7 @@ public class UnnecessaryConversionTemporary extends AbstractRule implements Rule
public Object visit(ASTPrimarySuffix node, Object data) {
if (inPrimaryExpressionContext && usingPrimitiveWrapperAllocation) {
if (node.getImage() != null && node.getImage().equals("toString")) {
if (node.hasImageEqualTo("toString")) {
if (node.jjtGetParent() == primary) {
addViolation(data, node);
}

View File

@ -91,6 +91,6 @@ public class UnusedPrivateMethodRule extends AbstractRule {
private boolean privateAndNotExcluded(MethodNameDeclaration mnd) {
ASTMethodDeclarator node = (ASTMethodDeclarator) mnd.getNode();
return ((AccessNode) node.jjtGetParent()).isPrivate() && !node.getImage().equals("readObject") && !node.getImage().equals("writeObject") && !node.getImage().equals("readResolve") && !node.getImage().equals("writeReplace");
return ((AccessNode) node.jjtGetParent()).isPrivate() && !node.hasImageEqualTo("readObject") && !node.hasImageEqualTo("writeObject") && !node.hasImageEqualTo("readResolve") && !node.hasImageEqualTo("writeReplace");
}
}

View File

@ -71,7 +71,7 @@ public class UselessOverridingMethod extends AbstractRule {
return super.visit(node, data);
ASTMethodDeclarator methodDeclarator = (ASTMethodDeclarator) findFirstDegreeChildrenOfType(node, ASTMethodDeclarator.class).get(0);
if (!primaryPrefix.getImage().equals(methodDeclarator.getImage()))
if (!primaryPrefix.hasImageEqualTo(methodDeclarator.getImage()))
return super.visit(node, data);
//Process arguments
@ -103,7 +103,7 @@ public class UselessOverridingMethod extends AbstractRule {
ASTName argumentName = (ASTName) argumentPrimaryPrefixChild;
ASTFormalParameter formalParameter = (ASTFormalParameter) formalParameters.jjtGetChild(i);
ASTVariableDeclaratorId variableId = (ASTVariableDeclaratorId) findFirstDegreeChildrenOfType(formalParameter, ASTVariableDeclaratorId.class).get(0);
if (!argumentName.getImage().equals(variableId.getImage())) {
if (!argumentName.hasImageEqualTo(variableId.getImage())) {
return super.visit(node, data); //The arguments are not simply passed through
}

View File

@ -45,7 +45,7 @@ public class PreserveStackTrace extends AbstractRule {
child = (SimpleNode) child.jjtGetChild(0);
}
if (child != null){
if( child.getClass().equals(ASTName.class) && (!target.equals(child.getImage()) && !child.getImage().equals(target + ".fillInStackTrace"))) {
if( child.getClass().equals(ASTName.class) && (!target.equals(child.getImage()) && !child.hasImageEqualTo(target + ".fillInStackTrace"))) {
Map vars = ((ASTName) child).getScope().getVariableDeclarations();
for (Iterator i = vars.keySet().iterator(); i.hasNext();) {
VariableNameDeclaration decl = (VariableNameDeclaration) i.next();

View File

@ -60,7 +60,7 @@ public class UseSingleton extends AbstractRule {
if (decl.getMethodName().equals("suite")) {
ASTResultType res = (ASTResultType) decl.getFirstChildOfType(ASTResultType.class);
ASTClassOrInterfaceType c = (ASTClassOrInterfaceType) res.getFirstChildOfType(ASTClassOrInterfaceType.class);
if (c != null && c.getImage().equals("Test")) {
if (c != null && c.hasImageEqualTo("Test")) {
isOK = true;
}
}

View File

@ -49,7 +49,7 @@ public class JUnitAssertionsShouldIncludeMessage extends AbstractRule {
ASTPrimaryExpression primary = (ASTPrimaryExpression) node.jjtGetParent().jjtGetParent();
if (primary.jjtGetChild(0) instanceof ASTPrimaryPrefix && primary.jjtGetChild(0).jjtGetNumChildren() > 0 && primary.jjtGetChild(0).jjtGetChild(0) instanceof ASTName) {
ASTName name = (ASTName) primary.jjtGetChild(0).jjtGetChild(0);
if (name.getImage().equals(targetMethodName)) {
if (name.hasImageEqualTo(targetMethodName)) {
addViolation(ctx, name);
}
}

View File

@ -57,7 +57,7 @@ public class AbstractOptimizationRule extends AbstractRule implements Rule {
if (preinc != null && !preinc.isEmpty()) {
for (Iterator it = preinc.iterator(); it.hasNext();) {
ASTPreIncrementExpression ie = (ASTPreIncrementExpression) it.next();
if (((ASTName) ie.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0)).getImage().equals(varName)) {
if (((ASTName) ie.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0)).hasImageEqualTo(varName)) {
return true;
}
}
@ -68,7 +68,7 @@ public class AbstractOptimizationRule extends AbstractRule implements Rule {
if (predec != null && !predec.isEmpty()) {
for (Iterator it = predec.iterator(); it.hasNext();) {
ASTPreDecrementExpression de = (ASTPreDecrementExpression) it.next();
if (((ASTName) de.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0)).getImage().equals(varName)) {
if (((ASTName) de.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0)).hasImageEqualTo(varName)) {
return true;
}
}
@ -79,14 +79,14 @@ public class AbstractOptimizationRule extends AbstractRule implements Rule {
for (Iterator it = pf.iterator(); it.hasNext();) {
ASTPostfixExpression pe = (ASTPostfixExpression) it.next();
if ((pe.getImage().equals("++") || pe.getImage().equals("--"))) {
if ((pe.hasImageEqualTo("++") || pe.hasImageEqualTo("--"))) {
SimpleNode first = (SimpleNode) pe.jjtGetChild(0);
SimpleNode second = (SimpleNode) first.jjtGetChild(0);
if (second.jjtGetNumChildren() == 0 || !(second.jjtGetChild(0) instanceof ASTName)) {
continue;
}
ASTName name = (ASTName) second.jjtGetChild(0);
if (name.getImage().equals(varName)) {
if (name.hasImageEqualTo(varName)) {
return true;
}
}
@ -109,7 +109,7 @@ public class AbstractOptimizationRule extends AbstractRule implements Rule {
continue;
}
ASTName n = (ASTName) otherChild.jjtGetChild(0);
if (n.getImage().equals(varName)) {
if (n.hasImageEqualTo(varName)) {
return true;
}
}

View File

@ -17,7 +17,7 @@ public class AvoidCatchingThrowable extends AbstractRule {
public Object visit(ASTCatchStatement node, Object data) {
ASTType type = (ASTType) node.findChildrenOfType(ASTType.class).get(0);
ASTClassOrInterfaceType name = (ASTClassOrInterfaceType) type.findChildrenOfType(ASTClassOrInterfaceType.class).get(0);
if (name.getImage().equals("Throwable")) {
if (name.hasImageEqualTo("Throwable")) {
addViolation(data, name);
}
return super.visit(node, data);

View File

@ -87,7 +87,7 @@ public class ExceptionSignatureDeclaration extends AbstractRule {
* @return true if <code>Exception</code> is declared and has proper parents
*/
private boolean hasDeclaredExceptionInSignature(ASTName exception) {
return exception.getImage().equals("Exception") && isParentSignatureDeclaration(exception);
return exception.hasImageEqualTo("Exception") && isParentSignatureDeclaration(exception);
}
/**

View File

@ -84,7 +84,7 @@ public class ConsecutiveLiteralAppends extends AbstractRule {
if (!isStringBuffer(node)) {
return data;
}
threshold = getIntProperty("threshold");
threshold = getIntProperty(thresholdDescriptor);
int concurrentCount = checkConstructor(node, data);
Node lastBlock = getFirstParentBlock(node);

View File

@ -20,7 +20,7 @@ public class StringInstantiation extends AbstractRule {
}
ASTClassOrInterfaceType clz = (ASTClassOrInterfaceType) node.jjtGetChild(0);
if (!clz.getImage().equals("String")) {
if (!clz.hasImageEqualTo("String")) {
return data;
}

View File

@ -60,7 +60,7 @@ public class UnnecessaryCaseChange extends AbstractRule {
}
ASTPrimarySuffix suffix = (ASTPrimarySuffix) exp.jjtGetChild(2);
if (suffix.getImage() == null || !(suffix.getImage().equals("equals") || suffix.getImage().equals("equalsIgnoreCase"))) {
if (suffix.getImage() == null || !(suffix.hasImageEqualTo("equals") || suffix.hasImageEqualTo("equalsIgnoreCase"))) {
return null;
}
return suffix.getImage();

View File

@ -56,7 +56,7 @@ public class MethodReturnsInternalArray extends AbstractSunSecureRule {
final ASTPrimaryPrefix pp = (ASTPrimaryPrefix) ret.getFirstChildOfType(ASTPrimaryPrefix.class);
if (pp != null && pp.usesThisModifier()) {
final ASTPrimarySuffix ps = (ASTPrimarySuffix) ret.getFirstChildOfType(ASTPrimarySuffix.class);
if (ps.getImage().equals(vn)) {
if (ps.hasImageEqualTo(vn)) {
addViolation(data, ret, vn);
}
}