use of net.sourceforge.pmd.lang.java.ast.ASTBlockStatement in project pmd by pmd.
the class CloseResourceRule method ensureClosed.
private void ensureClosed(ASTLocalVariableDeclaration var, ASTVariableDeclaratorId id, Object data) {
// What are the chances of a Connection being instantiated in a
// for-loop init block? Anyway, I'm lazy!
String variableToClose = id.getImage();
Node n = var;
while (!(n instanceof ASTBlock) && !(n instanceof ASTConstructorDeclaration)) {
n = n.jjtGetParent();
}
Node top = n;
List<ASTTryStatement> tryblocks = top.findDescendantsOfType(ASTTryStatement.class);
boolean closed = false;
ASTBlockStatement parentBlock = id.getFirstParentOfType(ASTBlockStatement.class);
// block.
for (ASTTryStatement t : tryblocks) {
// verifies that there are no critical statements between the
// variable declaration and
// the beginning of the try block.
ASTBlockStatement tryBlock = t.getFirstParentOfType(ASTBlockStatement.class);
// the variable has been initialized with null
if (!hasNullInitializer(var) && parentBlock.jjtGetParent() == tryBlock.jjtGetParent()) {
List<ASTBlockStatement> blocks = parentBlock.jjtGetParent().findChildrenOfType(ASTBlockStatement.class);
int parentBlockIndex = blocks.indexOf(parentBlock);
int tryBlockIndex = blocks.indexOf(tryBlock);
boolean criticalStatements = false;
for (int i = parentBlockIndex + 1; i < tryBlockIndex; i++) {
// assume variable declarations are not critical
ASTLocalVariableDeclaration varDecl = blocks.get(i).getFirstDescendantOfType(ASTLocalVariableDeclaration.class);
if (varDecl == null) {
criticalStatements = true;
break;
}
}
if (criticalStatements) {
break;
}
}
if (t.getBeginLine() > id.getBeginLine() && t.hasFinally()) {
ASTBlock f = (ASTBlock) t.getFinally().jjtGetChild(0);
List<ASTName> names = f.findDescendantsOfType(ASTName.class);
for (ASTName oName : names) {
String name = oName.getImage();
if (name != null && name.contains(".")) {
String[] parts = name.split("\\.");
if (parts.length == 2) {
String methodName = parts[1];
String varName = parts[0];
if (varName.equals(variableToClose) && closeTargets.contains(methodName) && nullCheckIfCondition(f, oName, varName)) {
closed = true;
break;
}
}
}
}
if (closed) {
break;
}
List<ASTStatementExpression> exprs = new ArrayList<>();
f.findDescendantsOfType(ASTStatementExpression.class, exprs, true);
for (ASTStatementExpression stmt : exprs) {
ASTPrimaryExpression expr = stmt.getFirstChildOfType(ASTPrimaryExpression.class);
if (expr != null) {
ASTPrimaryPrefix prefix = expr.getFirstChildOfType(ASTPrimaryPrefix.class);
ASTPrimarySuffix suffix = expr.getFirstChildOfType(ASTPrimarySuffix.class);
if (prefix != null && suffix != null) {
if (prefix.getImage() == null) {
ASTName prefixName = prefix.getFirstChildOfType(ASTName.class);
if (prefixName != null && closeTargets.contains(prefixName.getImage())) {
// Found a call to a "close target" that is
// a direct
// method call without a "ClassName."
// prefix.
closed = variableIsPassedToMethod(expr, variableToClose);
if (closed) {
break;
}
}
} else if (suffix.getImage() != null) {
String prefixPlusSuffix = prefix.getImage() + "." + suffix.getImage();
if (closeTargets.contains(prefixPlusSuffix)) {
// Found a call to a "close target" that is
// a method call
// in the form "ClassName.methodName".
closed = variableIsPassedToMethod(expr, variableToClose);
if (closed) {
break;
}
}
}
// really check it.
if (!closed) {
List<ASTPrimarySuffix> suffixes = new ArrayList<>();
expr.findDescendantsOfType(ASTPrimarySuffix.class, suffixes, true);
for (ASTPrimarySuffix oSuffix : suffixes) {
String suff = oSuffix.getImage();
if (closeTargets.contains(suff)) {
closed = variableIsPassedToMethod(expr, variableToClose);
if (closed) {
break;
}
}
}
}
}
}
}
if (closed) {
break;
}
}
}
if (!closed) {
// See if the variable is returned by the method, which means the
// method is a utility for creating the db resource, which means of
// course it can't be closed by the method, so it isn't an error.
List<ASTReturnStatement> returns = new ArrayList<>();
top.findDescendantsOfType(ASTReturnStatement.class, returns, true);
for (ASTReturnStatement returnStatement : returns) {
ASTName name = returnStatement.getFirstDescendantOfType(ASTName.class);
if (name != null && name.getImage().equals(variableToClose)) {
closed = true;
break;
}
}
}
// if all is not well, complain
if (!closed) {
ASTType type = var.getFirstChildOfType(ASTType.class);
ASTReferenceType ref = (ASTReferenceType) type.jjtGetChild(0);
ASTClassOrInterfaceType clazz = (ASTClassOrInterfaceType) ref.jjtGetChild(0);
addViolation(data, id, clazz.getImage());
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTBlockStatement in project pmd by pmd.
the class StdCyclomaticComplexityRule method visit.
@Override
public Object visit(ASTSwitchStatement node, Object data) {
Entry entry = entryStack.peek();
int childCount = node.jjtGetNumChildren();
int lastIndex = childCount - 1;
for (int n = 0; n < lastIndex; n++) {
Node childNode = node.jjtGetChild(n);
if (childNode instanceof ASTSwitchLabel) {
// default is generally not considered a decision (same as
// "else")
ASTSwitchLabel sl = (ASTSwitchLabel) childNode;
if (!sl.isDefault()) {
childNode = node.jjtGetChild(n + 1);
if (childNode instanceof ASTBlockStatement) {
entry.bumpDecisionPoints();
}
}
}
}
super.visit(node, data);
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTBlockStatement in project pmd by pmd.
the class SingletonClassReturningNewInstanceRule method visit.
@Override
public Object visit(ASTMethodDeclaration node, Object data) {
boolean violation = false;
String localVarName = null;
String returnVariableName = null;
if (node.getResultType().isVoid()) {
return super.visit(node, data);
}
if ("getInstance".equals(node.getMethodName())) {
List<ASTReturnStatement> rsl = node.findDescendantsOfType(ASTReturnStatement.class);
if (rsl.isEmpty()) {
return super.visit(node, data);
} else {
for (ASTReturnStatement rs : rsl) {
List<ASTPrimaryExpression> pel = rs.findDescendantsOfType(ASTPrimaryExpression.class);
ASTPrimaryExpression ape = pel.get(0);
if (ape.getFirstDescendantOfType(ASTAllocationExpression.class) != null) {
violation = true;
break;
}
}
}
/*
* public class Singleton {
*
* private static Singleton m_instance=null;
*
* public static Singleton getInstance() {
*
* Singleton m_instance=null;
*
* if ( m_instance == null ) { synchronized(Singleton.class) {
* if(m_instance == null) { m_instance = new Singleton(); } } }
* return m_instance; } }
*/
List<ASTBlockStatement> astBlockStatements = node.findDescendantsOfType(ASTBlockStatement.class);
returnVariableName = getReturnVariableName(node);
if (!astBlockStatements.isEmpty()) {
for (ASTBlockStatement blockStatement : astBlockStatements) {
if (blockStatement.hasDescendantOfType(ASTLocalVariableDeclaration.class)) {
List<ASTLocalVariableDeclaration> lVarList = blockStatement.findDescendantsOfType(ASTLocalVariableDeclaration.class);
if (!lVarList.isEmpty()) {
for (ASTLocalVariableDeclaration localVar : lVarList) {
localVarName = localVar.getVariableName();
if (returnVariableName != null && returnVariableName.equals(localVarName)) {
violation = true;
break;
}
}
}
}
}
}
}
if (violation) {
addViolation(data, node);
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTBlockStatement in project pmd by pmd.
the class ScopeAndDeclarationFinder method visit.
@Override
public Object visit(ASTConstructorDeclaration node, Object data) {
/*
* Local variables declared inside the constructor need to be in a
* different scope so special handling is needed
*/
createMethodScope(node);
Scope methodScope = node.getScope();
Node formalParameters = node.jjtGetChild(0);
int i = 1;
int n = node.jjtGetNumChildren();
if (!(formalParameters instanceof ASTFormalParameters)) {
visit((ASTTypeParameters) formalParameters, data);
formalParameters = node.jjtGetChild(1);
i++;
}
visit((ASTFormalParameters) formalParameters, data);
Scope localScope = null;
for (; i < n; i++) {
JavaNode b = (JavaNode) node.jjtGetChild(i);
if (b instanceof ASTBlockStatement) {
if (localScope == null) {
createLocalScope(node);
localScope = node.getScope();
}
b.setScope(localScope);
visit(b, data);
} else {
visit(b, data);
}
}
if (localScope != null) {
// pop the local scope
scopes.pop();
// reset the correct scope for the constructor
node.setScope(methodScope);
}
// pop the method scope
scopes.pop();
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTBlockStatement in project pmd by pmd.
the class ArrayIsStoredDirectlyRule method checkForDirectAssignment.
/**
* Checks if the variable designed in parameter is written to a field (not
* local variable) in the statements.
*/
private boolean checkForDirectAssignment(Object ctx, final ASTFormalParameter parameter, final List<ASTBlockStatement> bs) {
final ASTVariableDeclaratorId vid = parameter.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
final String varName = vid.getImage();
for (ASTBlockStatement b : bs) {
if (b.hasDescendantOfType(ASTAssignmentOperator.class)) {
final ASTStatementExpression se = b.getFirstDescendantOfType(ASTStatementExpression.class);
if (se == null || !(se.jjtGetChild(0) instanceof ASTPrimaryExpression)) {
continue;
}
String assignedVar = getExpressionVarName(se);
if (assignedVar == null) {
continue;
}
ASTPrimaryExpression pe = (ASTPrimaryExpression) se.jjtGetChild(0);
Node n = pe.getFirstParentOfType(ASTMethodDeclaration.class);
if (n == null) {
n = pe.getFirstParentOfType(ASTConstructorDeclaration.class);
if (n == null) {
continue;
}
}
if (!isLocalVariable(assignedVar, n)) {
// something
if (se.jjtGetNumChildren() < 3) {
continue;
}
ASTExpression e = (ASTExpression) se.jjtGetChild(2);
if (e.hasDescendantOfType(ASTEqualityExpression.class)) {
continue;
}
String val = getExpressionVarName(e);
if (val == null) {
continue;
}
ASTPrimarySuffix foo = e.getFirstDescendantOfType(ASTPrimarySuffix.class);
if (foo != null && foo.isArrayDereference()) {
continue;
}
if (val.equals(varName)) {
Node md = parameter.getFirstParentOfType(ASTMethodDeclaration.class);
if (md == null) {
md = pe.getFirstParentOfType(ASTConstructorDeclaration.class);
}
if (!isLocalVariable(varName, md)) {
addViolation(ctx, parameter, varName);
}
}
}
}
}
return false;
}
Aggregations