use of net.sourceforge.pmd.lang.java.ast.ASTExpression in project pmd by pmd.
the class CheckSkipResultRule method visit.
@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
ASTType typeNode = node.getTypeNode();
if (typeNode == null || !TypeHelper.isA(typeNode, InputStream.class)) {
return data;
}
for (NameOccurrence occ : node.getUsages()) {
JavaNameOccurrence jocc = (JavaNameOccurrence) occ;
NameOccurrence qualifier = jocc.getNameForWhichThisIsAQualifier();
if (qualifier != null && "skip".equals(qualifier.getImage())) {
Node loc = jocc.getLocation();
if (loc != null) {
ASTPrimaryExpression exp = loc.getFirstParentOfType(ASTPrimaryExpression.class);
while (exp != null) {
if (exp.jjtGetParent() instanceof ASTStatementExpression) {
// if exp is in a bare statement,
// the returned value is not used
addViolation(data, occ.getLocation());
break;
} else if (exp.jjtGetParent() instanceof ASTExpression && exp.jjtGetParent().jjtGetParent() instanceof ASTPrimaryPrefix) {
// if exp is enclosed in a pair of parenthesis
// let's have a look at the enclosing expression
// we'll see if it's in a bare statement
exp = exp.getFirstParentOfType(ASTPrimaryExpression.class);
} else {
// or assignement so the returned value is used
break;
}
}
}
}
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTExpression 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.ASTExpression in project pmd by pmd.
the class StatementAndBraceFinderTest method testWhileStmtHasCorrectTypes.
@Test
public void testWhileStmtHasCorrectTypes() {
ASTExpression exp = getOrderedNodes(ASTExpression.class, TEST4).get(0);
DataFlowNode dfn = exp.getDataFlowNode().getFlow().get(2);
assertTrue(dfn.isType(NodeType.WHILE_EXPR));
assertTrue(dfn.isType(NodeType.WHILE_LAST_STATEMENT));
}
use of net.sourceforge.pmd.lang.java.ast.ASTExpression 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.ASTExpression 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