use of net.sourceforge.pmd.lang.java.ast.ASTStatementExpression 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.ASTStatementExpression in project pmd by pmd.
the class SingularFieldRule method isInAssignment.
private boolean isInAssignment(Node potentialStatement) {
if (potentialStatement instanceof ASTStatementExpression) {
ASTStatementExpression statement = (ASTStatementExpression) potentialStatement;
List<ASTAssignmentOperator> assignments = statement.findDescendantsOfType(ASTAssignmentOperator.class);
return !assignments.isEmpty() && "=".equals(assignments.get(0).getImage());
} else {
return false;
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTStatementExpression 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;
}
use of net.sourceforge.pmd.lang.java.ast.ASTStatementExpression in project pmd by pmd.
the class UseStringBufferForStringAppendsRule method visit.
@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
if (!TypeHelper.isA(node, String.class) || node.isArray()) {
return data;
}
Node parent = node.jjtGetParent().jjtGetParent();
if (!(parent instanceof ASTLocalVariableDeclaration)) {
return data;
}
for (NameOccurrence no : node.getUsages()) {
Node name = no.getLocation();
ASTStatementExpression statement = name.getFirstParentOfType(ASTStatementExpression.class);
if (statement == null) {
continue;
}
ASTArgumentList argList = name.getFirstParentOfType(ASTArgumentList.class);
if (argList != null && argList.getFirstParentOfType(ASTStatementExpression.class) == statement) {
// used in method call
continue;
}
ASTEqualityExpression equality = name.getFirstParentOfType(ASTEqualityExpression.class);
if (equality != null && equality.getFirstParentOfType(ASTStatementExpression.class) == statement) {
// used in condition
continue;
}
ASTConditionalExpression conditional = name.getFirstParentOfType(ASTConditionalExpression.class);
if (conditional != null) {
Node thirdParent = name.getNthParent(3);
Node fourthParent = name.getNthParent(4);
if ((Objects.equals(thirdParent, conditional) || Objects.equals(fourthParent, conditional)) && conditional.getFirstParentOfType(ASTStatementExpression.class) == statement) {
// string)
continue;
}
}
if (statement.jjtGetNumChildren() > 0 && statement.jjtGetChild(0) instanceof ASTPrimaryExpression) {
ASTName astName = statement.jjtGetChild(0).getFirstDescendantOfType(ASTName.class);
if (astName != null) {
if (astName.equals(name)) {
ASTAssignmentOperator assignmentOperator = statement.getFirstDescendantOfType(ASTAssignmentOperator.class);
if (assignmentOperator != null && assignmentOperator.isCompound()) {
addViolation(data, assignmentOperator);
}
} else if (astName.getImage().equals(name.getImage())) {
ASTAssignmentOperator assignmentOperator = statement.getFirstDescendantOfType(ASTAssignmentOperator.class);
if (assignmentOperator != null && !assignmentOperator.isCompound()) {
addViolation(data, astName);
}
}
}
}
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTStatementExpression 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);
}
Aggregations