use of net.sourceforge.pmd.lang.java.ast.ASTIfStatement in project pmd by pmd.
the class InsufficientStringBufferDeclarationRule method getFirstParentBlock.
/**
* Locate the block that the given node is in, if any
*
* @param node
* The node we're looking for a parent of
* @return Node - The node that corresponds to any block that may be a
* parent of this object
*/
private Node getFirstParentBlock(Node node) {
Node parentNode = node.jjtGetParent();
Node lastNode = node;
while (parentNode != null && !BLOCK_PARENTS.contains(parentNode.getClass())) {
lastNode = parentNode;
parentNode = parentNode.jjtGetParent();
}
if (parentNode instanceof ASTIfStatement) {
parentNode = lastNode;
} else if (parentNode instanceof ASTSwitchStatement) {
parentNode = getSwitchParent(parentNode, lastNode);
}
return parentNode;
}
use of net.sourceforge.pmd.lang.java.ast.ASTIfStatement in project pmd by pmd.
the class NonThreadSafeSingletonRule method visit.
@Override
public Object visit(ASTMethodDeclaration node, Object data) {
if (checkNonStaticMethods && !node.isStatic() || node.isSynchronized()) {
return super.visit(node, data);
}
List<ASTIfStatement> ifStatements = node.findDescendantsOfType(ASTIfStatement.class);
for (ASTIfStatement ifStatement : ifStatements) {
if (ifStatement.getFirstParentOfType(ASTSynchronizedStatement.class) == null) {
if (!ifStatement.hasDescendantOfType(ASTNullLiteral.class)) {
continue;
}
ASTName n = ifStatement.getFirstDescendantOfType(ASTName.class);
if (n == null || !fieldDecls.containsKey(n.getImage())) {
continue;
}
List<ASTAssignmentOperator> assigmnents = ifStatement.findDescendantsOfType(ASTAssignmentOperator.class);
boolean violation = false;
for (int ix = 0; ix < assigmnents.size(); ix++) {
ASTAssignmentOperator oper = assigmnents.get(ix);
if (!(oper.jjtGetParent() instanceof ASTStatementExpression)) {
continue;
}
ASTStatementExpression expr = (ASTStatementExpression) oper.jjtGetParent();
if (expr.jjtGetChild(0) instanceof ASTPrimaryExpression && ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetNumChildren() == 1 && ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetChild(0) instanceof ASTPrimaryPrefix) {
ASTPrimaryPrefix pp = (ASTPrimaryPrefix) ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetChild(0);
String name = null;
if (pp.usesThisModifier()) {
ASTPrimarySuffix priSuf = expr.getFirstDescendantOfType(ASTPrimarySuffix.class);
name = priSuf.getImage();
} else {
ASTName astName = (ASTName) pp.jjtGetChild(0);
name = astName.getImage();
}
if (fieldDecls.containsKey(name)) {
violation = true;
}
}
}
if (violation) {
addViolation(data, ifStatement);
}
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTIfStatement in project pmd by pmd.
the class ConsecutiveLiteralAppendsRule method getFirstParentBlock.
/**
* Get the first parent. Keep track of the last node though. For If
* statements it's the only way we can differentiate between if's and else's
* For switches it's the only way we can differentiate between switches
*
* @param node
* The node to check
* @return The first parent block
*/
private Node getFirstParentBlock(Node node) {
Node parentNode = node.jjtGetParent();
Node lastNode = node;
while (parentNode != null && !BLOCK_PARENTS.contains(parentNode.getClass())) {
lastNode = parentNode;
parentNode = parentNode.jjtGetParent();
}
if (parentNode instanceof ASTIfStatement) {
parentNode = lastNode;
} else if (parentNode instanceof ASTSwitchStatement) {
parentNode = getSwitchParent(parentNode, lastNode);
}
return parentNode;
}
use of net.sourceforge.pmd.lang.java.ast.ASTIfStatement in project pmd by pmd.
the class CloseResourceRule method findIfStatement.
private ASTIfStatement findIfStatement(ASTBlock enclosingBlock, Node node) {
ASTIfStatement ifStatement = node.getFirstParentOfType(ASTIfStatement.class);
List<ASTIfStatement> allIfStatements = enclosingBlock.findDescendantsOfType(ASTIfStatement.class);
if (ifStatement != null && allIfStatements.contains(ifStatement)) {
return ifStatement;
}
return null;
}
use of net.sourceforge.pmd.lang.java.ast.ASTIfStatement in project pmd by pmd.
the class ScopeCreationVisitorTest method testScopesAreCreated.
@Test
public void testScopesAreCreated() {
parseCode(TEST1);
ASTIfStatement n = acu.findDescendantsOfType(ASTIfStatement.class).get(0);
assertTrue(n.getScope() instanceof LocalScope);
}
Aggregations