Search in sources :

Example 6 with ASTExpression

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;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 7 with ASTExpression

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;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTAssignmentOperator(net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator) ASTVariableInitializer(net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer) ASTLocalVariableDeclaration(net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression) ASTEqualityExpression(net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName)

Example 8 with ASTExpression

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));
}
Also used : DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression) Test(org.junit.Test)

Example 9 with ASTExpression

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;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 10 with ASTExpression

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;
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Aggregations

ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)26 Test (org.junit.Test)9 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)8 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)8 Node (net.sourceforge.pmd.lang.ast.Node)7 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)6 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)6 DataFlowNode (net.sourceforge.pmd.lang.dfa.DataFlowNode)3 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)3 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)3 ArrayList (java.util.ArrayList)2 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)2 ASTForInit (net.sourceforge.pmd.lang.java.ast.ASTForInit)2 ASTForUpdate (net.sourceforge.pmd.lang.java.ast.ASTForUpdate)2 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)2 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)2 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)2 ResolutionFailedException (net.sourceforge.pmd.lang.java.typeresolution.typeinference.TypeInferenceResolver.ResolutionFailedException)2 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)2 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)2