use of net.sourceforge.pmd.lang.java.ast.ASTIfStatement in project pmd by pmd.
the class InsufficientStringBufferDeclarationRule method storeBlockStatistics.
/**
* This rule is concerned with IF and Switch blocks. Process the block into
* a local Map, from which we can later determine which is the longest block
* inside
*
* @param blocks
* The map of blocks in the method being investigated
* @param thisSize
* The size of the current block
* @param block
* The block in question
*/
private void storeBlockStatistics(Map<Node, Map<Node, Integer>> blocks, int thisSize, Node block) {
Node statement = block.jjtGetParent();
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 instanceof ASTIfStatement) {
statement = possibleStatement;
possibleStatement = possibleStatement.getFirstParentOfType(ASTIfStatement.class);
}
}
Map<Node, Integer> thisBranch = blocks.get(statement);
if (thisBranch == null) {
thisBranch = new HashMap<>();
blocks.put(statement, thisBranch);
}
Integer x = thisBranch.get(block);
if (x != null) {
thisSize += x;
}
thisBranch.put(statement, thisSize);
}
use of net.sourceforge.pmd.lang.java.ast.ASTIfStatement in project pmd by pmd.
the class DoubleCheckedLockingRule method visit.
@Override
public Object visit(ASTMethodDeclaration node, Object data) {
if (node.getResultType().isVoid()) {
return super.visit(node, data);
}
ASTType typeNode = (ASTType) node.getResultType().jjtGetChild(0);
if (typeNode.jjtGetNumChildren() == 0 || !(typeNode.jjtGetChild(0) instanceof ASTReferenceType)) {
return super.visit(node, data);
}
List<ASTReturnStatement> rsl = node.findDescendantsOfType(ASTReturnStatement.class);
if (rsl.size() != 1) {
return super.visit(node, data);
}
ASTReturnStatement rs = rsl.get(0);
List<ASTPrimaryExpression> pel = rs.findDescendantsOfType(ASTPrimaryExpression.class);
ASTPrimaryExpression ape = pel.get(0);
Node lastChild = ape.jjtGetChild(ape.jjtGetNumChildren() - 1);
String returnVariableName = null;
if (lastChild instanceof ASTPrimaryPrefix) {
returnVariableName = getNameFromPrimaryPrefix((ASTPrimaryPrefix) lastChild);
}
// With Java5 and volatile keyword, DCL is no longer an issue
if (returnVariableName == null || this.volatileFields.contains(returnVariableName)) {
return super.visit(node, data);
}
// field, then it's ok, too
if (checkLocalVariableUsage(node, returnVariableName)) {
return super.visit(node, data);
}
List<ASTIfStatement> isl = node.findDescendantsOfType(ASTIfStatement.class);
if (isl.size() == 2) {
ASTIfStatement is = isl.get(0);
if (ifVerify(is, returnVariableName)) {
// find synchronized
List<ASTSynchronizedStatement> ssl = is.findDescendantsOfType(ASTSynchronizedStatement.class);
if (ssl.size() == 1) {
ASTSynchronizedStatement ss = ssl.get(0);
isl = ss.findDescendantsOfType(ASTIfStatement.class);
if (isl.size() == 1) {
ASTIfStatement is2 = isl.get(0);
if (ifVerify(is2, returnVariableName)) {
List<ASTStatementExpression> sel = is2.findDescendantsOfType(ASTStatementExpression.class);
if (sel.size() == 1) {
ASTStatementExpression se = sel.get(0);
if (se.jjtGetNumChildren() == 3) {
// primaryExpression, AssignmentOperator, Expression
if (se.jjtGetChild(0) instanceof ASTPrimaryExpression) {
ASTPrimaryExpression pe = (ASTPrimaryExpression) se.jjtGetChild(0);
if (matchName(pe, returnVariableName)) {
if (se.jjtGetChild(1) instanceof ASTAssignmentOperator) {
addViolation(data, node);
}
}
}
}
}
}
}
}
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTIfStatement in project pmd by pmd.
the class GuardLogStatementRule method hasGuard.
private boolean hasGuard(ASTPrimaryExpression node, String methodCall, String logLevel) {
ASTIfStatement ifStatement = node.getFirstParentOfType(ASTIfStatement.class);
if (ifStatement == null) {
return false;
}
// an if statement always has an expression
ASTExpression expr = ifStatement.getFirstChildOfType(ASTExpression.class);
List<ASTPrimaryPrefix> guardCalls = expr.findDescendantsOfType(ASTPrimaryPrefix.class);
if (guardCalls.isEmpty()) {
return false;
}
boolean foundGuard = false;
// check all conditions in the if expression
for (ASTPrimaryPrefix guardCall : guardCalls) {
if (guardCall.jjtGetNumChildren() < 1 || guardCall.jjtGetChild(0).getImage() == null) {
continue;
}
String guardMethodCall = getLastPartOfName(guardCall.jjtGetChild(0).getImage());
boolean guardMethodCallMatches = guardStmtByLogLevel.get(methodCall).contains(guardMethodCall);
boolean hasArguments = guardCall.jjtGetParent().hasDescendantOfType(ASTArgumentList.class);
if (guardMethodCallMatches && !hasArguments) {
// simple case: guard method without arguments found
foundGuard = true;
} else if (guardMethodCallMatches && hasArguments) {
// java.util.logging: guard method with argument. Verify the log level
String guardArgLogLevel = getLogLevelName(guardCall.jjtGetParent(), guardMethodCall);
foundGuard = logLevel.equals(guardArgLogLevel);
}
if (foundGuard) {
break;
}
}
return foundGuard;
}
use of net.sourceforge.pmd.lang.java.ast.ASTIfStatement in project pmd by pmd.
the class StatementAndBraceFinder method visit.
// ----------------------------------------------------------------------------
// BRANCH OUT
@Override
public Object visit(ASTStatement node, Object data) {
if (!(data instanceof Structure)) {
return data;
}
Structure dataFlow = (Structure) data;
if (node.jjtGetParent() instanceof ASTForStatement) {
this.addForExpressionNode(node, dataFlow);
dataFlow.pushOnStack(NodeType.FOR_BEFORE_FIRST_STATEMENT, dataFlow.getLast());
tryToLog(NodeType.FOR_BEFORE_FIRST_STATEMENT, node);
} else if (node.jjtGetParent() instanceof ASTDoStatement) {
dataFlow.pushOnStack(NodeType.DO_BEFORE_FIRST_STATEMENT, dataFlow.getLast());
dataFlow.createNewNode(node.jjtGetParent());
tryToLog(NodeType.DO_BEFORE_FIRST_STATEMENT, node);
}
super.visit(node, data);
if (node.jjtGetParent() instanceof ASTIfStatement) {
ASTIfStatement st = (ASTIfStatement) node.jjtGetParent();
if (!st.hasElse()) {
dataFlow.pushOnStack(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE, dataFlow.getLast());
tryToLog(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE, node);
} else if (st.hasElse() && !st.jjtGetChild(1).equals(node)) {
dataFlow.pushOnStack(NodeType.ELSE_LAST_STATEMENT, dataFlow.getLast());
tryToLog(NodeType.ELSE_LAST_STATEMENT, node);
} else {
dataFlow.pushOnStack(NodeType.IF_LAST_STATEMENT, dataFlow.getLast());
tryToLog(NodeType.IF_LAST_STATEMENT, node);
}
} else if (node.jjtGetParent() instanceof ASTWhileStatement) {
dataFlow.pushOnStack(NodeType.WHILE_LAST_STATEMENT, dataFlow.getLast());
tryToLog(NodeType.WHILE_LAST_STATEMENT, node);
} else if (node.jjtGetParent() instanceof ASTForStatement) {
dataFlow.pushOnStack(NodeType.FOR_END, dataFlow.getLast());
tryToLog(NodeType.FOR_END, node);
} else if (node.jjtGetParent() instanceof ASTLabeledStatement) {
dataFlow.pushOnStack(NodeType.LABEL_LAST_STATEMENT, dataFlow.getLast());
tryToLog(NodeType.LABEL_LAST_STATEMENT, node);
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTIfStatement in project pmd by pmd.
the class StatementAndBraceFinder method visit.
@Override
public Object visit(ASTExpression node, Object data) {
if (!(data instanceof Structure)) {
return data;
}
Structure dataFlow = (Structure) data;
String loggerTag = "parent";
Node parent = node.jjtGetParent();
// TODO what about throw stmts?
if (parent instanceof ASTIfStatement) {
// START IF
dataFlow.createNewNode(node);
dataFlow.pushOnStack(NodeType.IF_EXPR, dataFlow.getLast());
tryToLog(loggerTag, NodeType.IF_EXPR, node);
} else if (parent instanceof ASTWhileStatement) {
// START WHILE
dataFlow.createNewNode(node);
dataFlow.pushOnStack(NodeType.WHILE_EXPR, dataFlow.getLast());
tryToLog(loggerTag, NodeType.WHILE_EXPR, node);
} else if (parent instanceof ASTSwitchStatement) {
// START SWITCH
dataFlow.createNewNode(node);
dataFlow.pushOnStack(NodeType.SWITCH_START, dataFlow.getLast());
tryToLog(loggerTag, NodeType.SWITCH_START, node);
} else if (parent instanceof ASTForStatement) {
// FOR EXPR
dataFlow.createNewNode(node);
dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast());
tryToLog(loggerTag, NodeType.FOR_EXPR, node);
} else if (parent instanceof ASTDoStatement) {
// DO EXPR
dataFlow.createNewNode(node);
dataFlow.pushOnStack(NodeType.DO_EXPR, dataFlow.getLast());
tryToLog(loggerTag, NodeType.DO_EXPR, node);
} else if (parent instanceof ASTAssertStatement) {
dataFlow.createNewNode(node);
dataFlow.pushOnStack(NodeType.ASSERT_STATEMENT, dataFlow.getLast());
tryToLog(loggerTag, NodeType.ASSERT_STATEMENT, node);
}
return super.visit(node, data);
}
Aggregations