use of net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer in project pmd by pmd.
the class DoubleCheckedLockingRule method checkLocalVariableUsage.
private boolean checkLocalVariableUsage(ASTMethodDeclaration node, String returnVariableName) {
List<ASTLocalVariableDeclaration> locals = node.findDescendantsOfType(ASTLocalVariableDeclaration.class);
ASTVariableInitializer initializer = null;
for (ASTLocalVariableDeclaration l : locals) {
ASTVariableDeclaratorId id = l.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
if (id != null && id.hasImageEqualTo(returnVariableName)) {
initializer = l.getFirstDescendantOfType(ASTVariableInitializer.class);
break;
}
}
// the return variable name doesn't seem to be a local variable
if (initializer == null) {
return false;
}
// verify the value with which the local variable is initialized
if (initializer.jjtGetNumChildren() > 0 && initializer.jjtGetChild(0) instanceof ASTExpression && initializer.jjtGetChild(0).jjtGetNumChildren() > 0 && initializer.jjtGetChild(0).jjtGetChild(0) instanceof ASTPrimaryExpression && initializer.jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() > 0 && initializer.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0) instanceof ASTPrimaryPrefix && initializer.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() > 0 && initializer.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0) instanceof ASTName) {
ASTName name = (ASTName) initializer.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
if (name == null || !volatileFields.contains(name.getImage())) {
return false;
}
} else {
// not a simple assignment
return false;
}
// now check every usage/assignment of the variable
List<ASTName> names = node.findDescendantsOfType(ASTName.class);
for (ASTName n : names) {
if (!n.hasImageEqualTo(returnVariableName)) {
continue;
}
Node expression = n.getNthParent(3);
if (expression instanceof ASTEqualityExpression) {
continue;
}
if (expression instanceof ASTStatementExpression) {
if (expression.jjtGetNumChildren() > 2 && expression.jjtGetChild(1) instanceof ASTAssignmentOperator) {
ASTName value = expression.jjtGetChild(2).getFirstDescendantOfType(ASTName.class);
if (value == null || !volatileFields.contains(value.getImage())) {
return false;
}
}
}
}
return true;
}
use of net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer in project pmd by pmd.
the class UnnecessaryLocalBeforeReturnRule method isInitDataModifiedAfterInit.
private boolean isInitDataModifiedAfterInit(final VariableNameDeclaration variableDeclaration, final ASTReturnStatement rtn) {
final ASTVariableInitializer initializer = variableDeclaration.getAccessNodeParent().getFirstDescendantOfType(ASTVariableInitializer.class);
if (initializer != null) {
final List<ASTName> referencedNames = initializer.findDescendantsOfType(ASTName.class);
for (final ASTName refName : referencedNames) {
// TODO : Shouldn't the scope allow us to search for a var name occurrences directly, moving up through parent scopes?
Scope scope = refName.getScope();
do {
final Map<VariableNameDeclaration, List<NameOccurrence>> declarations = scope.getDeclarations(VariableNameDeclaration.class);
for (final Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : declarations.entrySet()) {
if (entry.getKey().getName().equals(refName.getImage())) {
// Variable found! Check usage locations
for (final NameOccurrence occ : entry.getValue()) {
final ScopedNode location = occ.getLocation();
// Is it used after initializing our "unnecessary" local but before the return statement?
if (isAfter(location, initializer) && isAfter(rtn, location)) {
return true;
}
}
return false;
}
}
scope = scope.getParent();
} while (scope != null);
}
}
return false;
}
use of net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer in project pmd by pmd.
the class ConsecutiveLiteralAppendsRule method checkInitializerExpressions.
/**
* Determine if during the variable initializer calls to ".append" are done.
*
* @param node
* @return
*/
private int checkInitializerExpressions(ASTVariableDeclaratorId node) {
ASTVariableInitializer initializer = node.jjtGetParent().getFirstChildOfType(ASTVariableInitializer.class);
ASTPrimaryExpression primary = initializer.getFirstDescendantOfType(ASTPrimaryExpression.class);
int result = 0;
boolean previousWasAppend = false;
for (int i = 0; i < primary.jjtGetNumChildren(); i++) {
Node child = primary.jjtGetChild(i);
if (child.jjtGetNumChildren() > 0 && child.jjtGetChild(0) instanceof ASTAllocationExpression) {
// skip the constructor call, that has already been checked
continue;
}
if (child instanceof ASTPrimarySuffix) {
ASTPrimarySuffix suffix = (ASTPrimarySuffix) child;
if (suffix.jjtGetNumChildren() == 0 && suffix.hasImageEqualTo("append")) {
previousWasAppend = true;
} else if (suffix.jjtGetNumChildren() > 0 && previousWasAppend) {
previousWasAppend = false;
ASTLiteral literal = suffix.getFirstDescendantOfType(ASTLiteral.class);
if (literal != null && literal.isStringLiteral()) {
result++;
} else {
// checking the remainder of the initializer
break;
}
}
}
}
return result;
}
use of net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer in project pmd by pmd.
the class PreserveStackTraceRule method visit.
@Override
public Object visit(ASTCatchStatement catchStmt, Object data) {
String target = catchStmt.jjtGetChild(0).findChildrenOfType(ASTVariableDeclaratorId.class).get(0).getImage();
// Inspect all the throw stmt inside the catch stmt
List<ASTThrowStatement> lstThrowStatements = catchStmt.findDescendantsOfType(ASTThrowStatement.class);
for (ASTThrowStatement throwStatement : lstThrowStatements) {
Node n = throwStatement.jjtGetChild(0).jjtGetChild(0);
if (n instanceof ASTCastExpression) {
ASTPrimaryExpression expr = (ASTPrimaryExpression) n.jjtGetChild(1);
if (expr.jjtGetNumChildren() > 1 && expr.jjtGetChild(1) instanceof ASTPrimaryPrefix) {
RuleContext ctx = (RuleContext) data;
addViolation(ctx, throwStatement);
}
continue;
}
// Retrieve all argument for the throw exception (to see if the
// original exception is preserved)
ASTArgumentList args = throwStatement.getFirstDescendantOfType(ASTArgumentList.class);
if (args != null) {
Node parent = args.jjtGetParent().jjtGetParent();
if (parent instanceof ASTAllocationExpression) {
// maybe it is used inside a anonymous class
ck(data, target, throwStatement, parent);
} else {
// Check all arguments used in the throw statement
ck(data, target, throwStatement, throwStatement);
}
} else {
Node child = throwStatement.jjtGetChild(0);
while (child != null && child.jjtGetNumChildren() > 0 && !(child instanceof ASTName)) {
child = child.jjtGetChild(0);
}
if (child != null) {
if (child instanceof ASTName && !target.equals(child.getImage()) && !child.hasImageEqualTo(target + FILL_IN_STACKTRACE)) {
Map<VariableNameDeclaration, List<NameOccurrence>> vars = ((ASTName) child).getScope().getDeclarations(VariableNameDeclaration.class);
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
VariableNameDeclaration decl = entry.getKey();
List<NameOccurrence> occurrences = entry.getValue();
if (decl.getImage().equals(child.getImage())) {
if (!isInitCauseCalled(target, occurrences)) {
// Check how the variable is initialized
ASTVariableInitializer initializer = decl.getNode().jjtGetParent().getFirstDescendantOfType(ASTVariableInitializer.class);
if (initializer != null) {
args = initializer.getFirstDescendantOfType(ASTArgumentList.class);
if (args != null) {
// constructor with args?
ck(data, target, throwStatement, args);
} else if (!isFillInStackTraceCalled(target, initializer)) {
addViolation(data, throwStatement);
}
}
}
}
}
} else if (child instanceof ASTClassOrInterfaceType) {
addViolation(data, throwStatement);
}
}
}
}
return super.visit(catchStmt, data);
}
Aggregations