code cleanup: x.getClass().equals(ASTxxx.class) replaced by x instanceof ASTxxx.

I used the XPath expression below to detect most cases but I don't think it's
general enough to create a new rule...

//PrimaryExpression[
(
PrimaryPrefix[Name[ends-with(@Image, 'getClass')]] or
PrimarySuffix[@Image='getClass'])
and
PrimarySuffix[Arguments[count(ArgumentList)=0]]
and
PrimarySuffix[@Image='equals']
and
PrimarySuffix/Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix
/ResultType/Type/ReferenceType/ClassOrInterfaceType
]


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@6471 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch
2008-09-11 16:36:07 +00:00
parent 4119862e6d
commit 20ee70ae16
16 changed files with 39 additions and 40 deletions

View File

@ -47,7 +47,7 @@ public class JavaRuleViolation extends AbstractRuleViolation {
Scope scope = node.getScope();
// Source file does not have an enclosing class scope...
if (!SourceFileScope.class.equals(scope.getClass())) {
if (!(scope instanceof SourceFileScope)) {
className = scope.getEnclosingClassScope().getClassName() == null ? "" : scope.getEnclosingClassScope()
.getClassName();
}
@ -64,7 +64,7 @@ public class JavaRuleViolation extends AbstractRuleViolation {
packageName = scope.getEnclosingSourceFileScope().getPackageName() == null ? "" : scope
.getEnclosingSourceFileScope().getPackageName();
// Source file does not have an enclosing class scope...
if (!SourceFileScope.class.equals(scope.getClass())) {
if (!(scope instanceof SourceFileScope)) {
className = scope.getEnclosingClassScope().getClassName() == null ? "" : qualifiedName;
}
methodName = node.getFirstParentOfType(ASTMethodDeclaration.class) == null ? "" : scope

View File

@ -73,7 +73,7 @@ public class BooleanInstantiationRule extends AbstractJavaRule {
if ( ! customBoolean )
{
if (node.jjtGetNumChildren() == 0 || !node.jjtGetChild(0).getClass().equals(ASTName.class)) {
if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) {
return super.visit(node, data);
}

View File

@ -125,20 +125,20 @@ public class BrokenNullCheckRule extends AbstractJavaRule {
for (int i = 0; i < nullCompareVariable.jjtGetNumChildren(); i++) {
Node child = nullCompareVariable.jjtGetChild(i);
if (child.getClass().equals(ASTName.class)) { //Variable names and some method calls
if (child instanceof ASTName) { //Variable names and some method calls
results.add( ((ASTName)child).getImage() );
} else if (child.getClass().equals(ASTLiteral.class)) { //Array arguments
} else if (child instanceof ASTLiteral) { //Array arguments
String literalImage = ((ASTLiteral)child).getImage();
//Skip other null checks
if (literalImage != null) {
results.add( literalImage );
}
} else if (child.getClass().equals(ASTPrimarySuffix.class)) { //More method calls
} else if (child instanceof ASTPrimarySuffix) { //More method calls
String name = ((ASTPrimarySuffix)child).getImage();
if (name != null && !name.equals("")) {
results.add(name);
}
} else if (child.getClass().equals(ASTClassOrInterfaceType.class)) { //A class can be an argument too
} else if (child instanceof ASTClassOrInterfaceType) { //A class can be an argument too
String name = ((ASTClassOrInterfaceType)child).getImage();
results.add(name);
}

View File

@ -41,7 +41,7 @@ public class OverrideBothEqualsAndHashcodeRule extends AbstractJavaRule {
@Override
public Object visit(ASTImplementsList node, Object data) {
for (int ix = 0; ix < node.jjtGetNumChildren(); ix++) {
if (node.jjtGetChild(ix).getClass().equals(ASTClassOrInterfaceType.class)) {
if (node.jjtGetChild(ix) instanceof ASTClassOrInterfaceType) {
ASTClassOrInterfaceType cit = (ASTClassOrInterfaceType) node.jjtGetChild(ix);
Class<?> clazz = cit.getType();
if (clazz != null || node.jjtGetChild(ix).hasImageEqualTo("Comparable")) {
@ -63,7 +63,7 @@ public class OverrideBothEqualsAndHashcodeRule extends AbstractJavaRule {
String paramName = null;
for (int ix = 0; ix < node.jjtGetNumChildren(); ix++) {
Node sn = node.jjtGetChild(ix);
if (sn.getClass().equals(ASTFormalParameters.class)) {
if (sn instanceof ASTFormalParameters) {
List<ASTFormalParameter> allParams = ((ASTFormalParameters) sn)
.findChildrenOfType(ASTFormalParameter.class);
for (ASTFormalParameter formalParam : allParams) {

View File

@ -18,8 +18,8 @@ public class UnnecessaryReturnRule extends AbstractJavaRule {
}
public Object visit(ASTReturnStatement node, Object data) {
if (node.jjtGetParent().getClass().equals(ASTStatement.class) && node.jjtGetParent().jjtGetParent().getClass().equals(ASTBlockStatement.class) && node.jjtGetParent().jjtGetParent().jjtGetParent().getClass().equals(ASTBlock.class)
&& node.jjtGetParent().jjtGetParent().jjtGetParent().jjtGetParent().getClass().equals(ASTMethodDeclaration.class)) {
if (node.jjtGetParent() instanceof ASTStatement && node.getNthParent(2) instanceof ASTBlockStatement && node.getNthParent(3) instanceof ASTBlock
&& node.getNthParent(4) instanceof ASTMethodDeclaration) {
addViolation(data, node);
}
return data;

View File

@ -81,13 +81,12 @@ public class NonThreadSafeSingletonRule extends AbstractJavaRule {
boolean violation = false;
for (int ix = 0; ix < assigmnents.size(); ix++) {
ASTAssignmentOperator oper = assigmnents.get(ix);
if (!oper.jjtGetParent().getClass().equals(ASTStatementExpression.class)) {
if (!(oper.jjtGetParent() instanceof ASTStatementExpression)) {
continue;
}
ASTStatementExpression expr = (ASTStatementExpression) oper.jjtGetParent();
if (expr.jjtGetChild(0).getClass().equals(ASTPrimaryExpression.class)
&& ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetChild(0).getClass().equals(
ASTPrimaryPrefix.class)) {
if ((expr.jjtGetChild(0) instanceof ASTPrimaryExpression)
&& (((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetChild(0) instanceof ASTPrimaryPrefix)) {
ASTPrimaryPrefix pp = (ASTPrimaryPrefix) ((ASTPrimaryExpression) expr.jjtGetChild(0))
.jjtGetChild(0);
String name = null;

View File

@ -47,9 +47,9 @@ public class PreserveStackTraceRule extends AbstractJavaRule {
List<ASTThrowStatement> lstThrowStatements = catchStmt.findDescendantsOfType(ASTThrowStatement.class);
for (ASTThrowStatement throwStatement : lstThrowStatements) {
Node n = throwStatement.jjtGetChild(0).jjtGetChild(0);
if (n.getClass().equals(ASTCastExpression.class)) {
if (n instanceof ASTCastExpression) {
ASTPrimaryExpression expr = (ASTPrimaryExpression) n.jjtGetChild(1);
if (expr.jjtGetNumChildren() > 1 && expr.jjtGetChild(1).getClass().equals(ASTPrimaryPrefix.class)) {
if (expr.jjtGetNumChildren() > 1 && expr.jjtGetChild(1) instanceof ASTPrimaryPrefix) {
RuleContext ctx = (RuleContext) data;
addViolation(ctx, throwStatement);
}
@ -66,11 +66,11 @@ public class PreserveStackTraceRule extends AbstractJavaRule {
else {
Node child = throwStatement.jjtGetChild(0);
while (child != null && child.jjtGetNumChildren() > 0
&& !child.getClass().equals(ASTName.class)) {
&& !(child instanceof ASTName)) {
child = child.jjtGetChild(0);
}
if (child != null){
if( child.getClass().equals(ASTName.class) && !target.equals(child.getImage()) && !child.hasImageEqualTo(target + FILL_IN_STACKTRACE)) {
if ((child instanceof ASTName) && !target.equals(child.getImage()) && !child.hasImageEqualTo(target + FILL_IN_STACKTRACE)) {
Map<VariableNameDeclaration, List<NameOccurrence>> vars = ((ASTName) child).getScope().getVariableDeclarations();
for (VariableNameDeclaration decl: vars.keySet()) {
args = decl.getNode().jjtGetParent()
@ -79,7 +79,7 @@ public class PreserveStackTraceRule extends AbstractJavaRule {
ck(data, target, throwStatement, args);
}
}
} else if(child.getClass().equals(ASTClassOrInterfaceType.class)){
} else if (child instanceof ASTClassOrInterfaceType){
addViolation(data, throwStatement);
}
}

View File

@ -47,14 +47,14 @@ public class JUnitUseExpectedRule extends AbstractJUnitRule {
boolean inAnnotation = false;
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
Node child = node.jjtGetChild(i);
if (ASTAnnotation.class.equals(child.getClass())) {
if (child instanceof ASTAnnotation) {
ASTName annotationName = child.getFirstDescendantOfType(ASTName.class);
if ("Test".equals(annotationName.getImage())) {
inAnnotation = true;
continue;
}
}
if (ASTMethodDeclaration.class.equals(child.getClass())) {
if (child instanceof ASTMethodDeclaration) {
boolean isJUnitMethod = isJUnitMethod((ASTMethodDeclaration) child, data);
if (inAnnotation || isJUnitMethod) {
List<Node> found = new ArrayList<Node>();

View File

@ -68,7 +68,7 @@ public class UnnecessaryCastRule extends AbstractJavaRule {
for (NameOccurrence no: usages) {
ASTName name = (ASTName) no.getLocation();
Node n = name.jjtGetParent().jjtGetParent().jjtGetParent();
if (ASTCastExpression.class.equals(n.getClass())) {
if (n instanceof ASTCastExpression) {
addViolation(data, n);
}
}

View File

@ -35,7 +35,7 @@ public class UnnecessaryWrapperObjectCreationRule extends AbstractJavaRule {
});
public Object visit(ASTPrimaryPrefix node, Object data) {
if (node.jjtGetNumChildren() == 0 || !node.jjtGetChild(0).getClass().equals(ASTName.class)) {
if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) {
return super.visit(node, data);
}

View File

@ -20,7 +20,7 @@ public class UseStringBufferForStringAppendsRule extends AbstractJavaRule {
return data;
}
Node parent = node.jjtGetParent().jjtGetParent();
if (!parent.getClass().equals(ASTLocalVariableDeclaration.class)) {
if (!(parent instanceof ASTLocalVariableDeclaration)) {
return data;
}
for (NameOccurrence no: node.getUsages()) {
@ -34,7 +34,7 @@ public class UseStringBufferForStringAppendsRule extends AbstractJavaRule {
// used in method call
continue;
}
if (statement.jjtGetNumChildren() > 0 && statement.jjtGetChild(0).getClass().equals(ASTPrimaryExpression.class)) {
if (statement.jjtGetNumChildren() > 0 && statement.jjtGetChild(0) instanceof ASTPrimaryExpression) {
ASTName astName = statement.jjtGetChild(0).getFirstDescendantOfType(ASTName.class);
if(astName != null){
if (astName.equals(name)) {

View File

@ -240,9 +240,9 @@ public class ConsecutiveLiteralAppendsRule extends AbstractJavaRule {
lastNode = parentNode;
parentNode = parentNode.jjtGetParent();
}
if (parentNode != null && parentNode.getClass().equals(ASTIfStatement.class)) {
if (parentNode instanceof ASTIfStatement) {
parentNode = lastNode;
} else if (parentNode != null && parentNode.getClass().equals(ASTSwitchStatement.class)) {
} else if (parentNode instanceof ASTSwitchStatement) {
parentNode = getSwitchParent(parentNode, lastNode);
}
return parentNode;
@ -260,7 +260,7 @@ public class ConsecutiveLiteralAppendsRule extends AbstractJavaRule {
ASTSwitchLabel label = null;
for (int ix = 0; ix < allChildren; ix++) {
Node n = parentNode.jjtGetChild(ix);
if (n.getClass().equals(ASTSwitchLabel.class)) {
if (n instanceof ASTSwitchLabel) {
label = (ASTSwitchLabel) n;
} else if (n.equals(lastNode)) {
parentNode = label;
@ -283,10 +283,10 @@ public class ConsecutiveLiteralAppendsRule extends AbstractJavaRule {
private boolean isAppendingStringLiteral(Node node) {
Node n = node;
while (n.jjtGetNumChildren() != 0 && !n.getClass().equals(ASTLiteral.class)) {
while (n.jjtGetNumChildren() != 0 && !(n instanceof ASTLiteral)) {
n = n.jjtGetChild(0);
}
return n.getClass().equals(ASTLiteral.class);
return n instanceof ASTLiteral;
}
private static boolean isStringBuffer(ASTVariableDeclaratorId node) {

View File

@ -117,11 +117,11 @@ public class InsufficientStringBufferDeclarationRule extends AbstractJavaRule {
*/
private void storeBlockStatistics(Map<Node, Map<Node, Integer>> blocks, int thisSize, Node block) {
Node statement = block.jjtGetParent();
if (ASTIfStatement.class.equals(block.jjtGetParent().getClass())) {
if (block.jjtGetParent() instanceof ASTIfStatement) {
// Else Ifs are their own subnode in AST. So we have to
// look a little farther up the tree to find the IF statement
Node possibleStatement = statement.getFirstParentOfType(ASTIfStatement.class);
while(possibleStatement != null && possibleStatement.getClass().equals(ASTIfStatement.class)) {
while (possibleStatement instanceof ASTIfStatement) {
statement = possibleStatement;
possibleStatement = possibleStatement.getFirstParentOfType(ASTIfStatement.class);
}
@ -180,7 +180,7 @@ public class InsufficientStringBufferDeclarationRule extends AbstractJavaRule {
private int processNode(Node sn) {
int anticipatedLength = 0;
ASTPrimaryPrefix xn = sn.getFirstDescendantOfType(ASTPrimaryPrefix.class);
if (xn.jjtGetNumChildren() != 0 && xn.jjtGetChild(0).getClass().equals(ASTLiteral.class)) {
if (xn.jjtGetNumChildren() != 0 && xn.jjtGetChild(0) instanceof ASTLiteral) {
String str = xn.jjtGetChild(0).getImage();
if (str != null) {
if(isLiteral(str)){
@ -294,9 +294,9 @@ public class InsufficientStringBufferDeclarationRule extends AbstractJavaRule {
lastNode = parentNode;
parentNode = parentNode.jjtGetParent();
}
if (parentNode != null && ASTIfStatement.class.equals(parentNode.getClass())) {
if (parentNode instanceof ASTIfStatement) {
parentNode = lastNode;
} else if (parentNode != null && parentNode.getClass().equals(ASTSwitchStatement.class)) {
} else if (parentNode instanceof ASTSwitchStatement) {
parentNode = getSwitchParent(parentNode, lastNode);
}
return parentNode;
@ -316,7 +316,7 @@ public class InsufficientStringBufferDeclarationRule extends AbstractJavaRule {
ASTSwitchLabel label = null;
for (int ix = 0; ix < allChildren; ix++) {
Node n = parentNode.jjtGetChild(ix);
if (n.getClass().equals(ASTSwitchLabel.class)) {
if (n instanceof ASTSwitchLabel) {
label = (ASTSwitchLabel) n;
} else if (n.equals(lastNode)) {
parentNode = label;

View File

@ -21,7 +21,7 @@ public class StringToStringRule extends AbstractJavaRule {
if (qualifier != null) {
if (!isArray && qualifier.getImage().indexOf("toString") != -1) {
addViolation(data, occ.getLocation());
} else if (isArray && qualifier.getLocation() != null && !ASTName.class.equals(qualifier.getLocation().getClass()) && qualifier.getImage().equals("toString")) {
} else if (isArray && qualifier.getLocation() != null && !(qualifier.getLocation() instanceof ASTName) && qualifier.getImage().equals("toString")) {
addViolation(data, occ.getLocation());
}
}

View File

@ -48,7 +48,7 @@ public class CloneMethodMustImplementCloneable extends AbstractJavaRule {
}
}
}
if (node.jjtGetNumChildren() != 0 && node.jjtGetChild(0).getClass().equals(ASTExtendsList.class)) {
if (node.jjtGetNumChildren() != 0 && node.jjtGetChild(0) instanceof ASTExtendsList) {
ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) node.jjtGetChild(0).jjtGetChild(0);
Class clazz = type.getType();
if (clazz != null && clazz.equals(Cloneable.class)) {

View File

@ -57,7 +57,7 @@ public class SignatureDeclareThrowsException extends AbstractJavaRule {
}
}
}
if (node.jjtGetNumChildren() != 0 && node.jjtGetChild(0).getClass().equals(ASTExtendsList.class)) {
if (node.jjtGetNumChildren() != 0 && node.jjtGetChild(0) instanceof ASTExtendsList) {
ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) node.jjtGetChild(0).jjtGetChild(0);
if (isJUnitTest(type)) {
junitImported = true;