use of net.sourceforge.pmd.lang.java.ast.ASTReturnStatement in project pmd by pmd.
the class SimplifyBooleanReturnsRule method visit.
public Object visit(ASTIfStatement node, Object data) {
// that's the case: if..then..return; return;
if (!node.hasElse() && isIfJustReturnsBoolean(node) && isJustReturnsBooleanAfter(node)) {
addViolation(data, node);
return super.visit(node, data);
}
// only deal with if..then..else stmts
if (node.jjtGetNumChildren() != 3) {
return super.visit(node, data);
}
// don't bother if either the if or the else block is empty
if (node.jjtGetChild(1).jjtGetNumChildren() == 0 || node.jjtGetChild(2).jjtGetNumChildren() == 0) {
return super.visit(node, data);
}
Node returnStatement1 = node.jjtGetChild(1).jjtGetChild(0);
Node returnStatement2 = node.jjtGetChild(2).jjtGetChild(0);
if (returnStatement1 instanceof ASTReturnStatement && returnStatement2 instanceof ASTReturnStatement) {
Node expression1 = returnStatement1.jjtGetChild(0).jjtGetChild(0);
Node expression2 = returnStatement2.jjtGetChild(0).jjtGetChild(0);
if (terminatesInBooleanLiteral(returnStatement1) && terminatesInBooleanLiteral(returnStatement2)) {
addViolation(data, node);
} else if (expression1 instanceof ASTUnaryExpressionNotPlusMinus ^ expression2 instanceof ASTUnaryExpressionNotPlusMinus) {
// If they are the same => error
if (isNodesEqualWithUnaryExpression(expression1, expression2)) {
// second case:
// If
// Expr
// Statement
// ReturnStatement
// UnaryExpressionNotPlusMinus '!'
// Expression E
// Statement
// ReturnStatement
// Expression E
// i.e.,
// if (foo)
// return !a;
// else
// return a;
addViolation(data, node);
}
}
} else if (hasOneBlockStmt(node.jjtGetChild(1)) && hasOneBlockStmt(node.jjtGetChild(2))) {
// We have blocks so we must go down three levels (BlockStatement,
// Statement, ReturnStatement)
returnStatement1 = returnStatement1.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
returnStatement2 = returnStatement2.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
// if we have 2 return;
if (isSimpleReturn(returnStatement1) && isSimpleReturn(returnStatement2)) {
// third case
// If
// Expr
// Statement
// Block
// BlockStatement
// Statement
// ReturnStatement
// Statement
// Block
// BlockStatement
// Statement
// ReturnStatement
// i.e.,
// if (foo) {
// return true;
// } else {
// return false;
// }
addViolation(data, node);
} else {
Node expression1 = getDescendant(returnStatement1, 4);
Node expression2 = getDescendant(returnStatement2, 4);
if (terminatesInBooleanLiteral(node.jjtGetChild(1).jjtGetChild(0)) && terminatesInBooleanLiteral(node.jjtGetChild(2).jjtGetChild(0))) {
addViolation(data, node);
} else if (expression1 instanceof ASTUnaryExpressionNotPlusMinus ^ expression2 instanceof ASTUnaryExpressionNotPlusMinus) {
// If they are the same => error
if (isNodesEqualWithUnaryExpression(expression1, expression2)) {
// forth case
// If
// Expr
// Statement
// Block
// BlockStatement
// Statement
// ReturnStatement
// UnaryExpressionNotPlusMinus '!'
// Expression E
// Statement
// Block
// BlockStatement
// Statement
// ReturnStatement
// Expression E
// i.e.,
// if (foo) {
// return !a;
// } else {
// return a;
// }
addViolation(data, node);
}
}
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTReturnStatement in project pmd by pmd.
the class OnlyOneReturnRule method visit.
@Override
public Object visit(ASTMethodDeclaration node, Object data) {
if (node.isAbstract()) {
return data;
}
List<ASTReturnStatement> returnNodes = node.findDescendantsOfType(ASTReturnStatement.class);
if (returnNodes.size() > 1) {
for (Iterator<ASTReturnStatement> i = returnNodes.iterator(); i.hasNext(); ) {
Node problem = i.next();
// skip the last one, it's OK
if (!i.hasNext()) {
continue;
}
addViolation(data, problem);
}
}
return data;
}
Aggregations