Search in sources :

Example 11 with ASTPrimaryExpression

use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression 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)

Example 12 with ASTPrimaryExpression

use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.

the class JUnitTestsShouldIncludeAssertRule method isExpectStatement.

private boolean isExpectStatement(ASTStatementExpression expression, Map<String, List<NameOccurrence>> expectables) {
    if (expression != null) {
        ASTPrimaryExpression pe = expression.getFirstChildOfType(ASTPrimaryExpression.class);
        if (pe != null) {
            Node name = pe.getFirstDescendantOfType(ASTName.class);
            // case of an AllocationExpression
            if (name == null) {
                return false;
            }
            String img = name.getImage();
            if (img.indexOf(".") == -1) {
                return false;
            }
            String varname = img.split("\\.")[0];
            if (!expectables.containsKey(varname)) {
                return false;
            }
            for (NameOccurrence occ : expectables.get(varname)) {
                if (occ.getLocation() == name && img.startsWith(varname + ".expect")) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 13 with ASTPrimaryExpression

use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.

the class UselessStringValueOfRule method isPrimitive.

private static boolean isPrimitive(Node parent) {
    boolean result = false;
    if (parent instanceof ASTPrimaryExpression && parent.jjtGetNumChildren() == 1) {
        Node child = parent.jjtGetChild(0);
        if (child instanceof ASTPrimaryPrefix && child.jjtGetNumChildren() == 1) {
            Node gc = child.jjtGetChild(0);
            if (gc instanceof ASTName) {
                ASTName name = (ASTName) gc;
                NameDeclaration nd = name.getNameDeclaration();
                if (nd instanceof VariableNameDeclaration && ((VariableNameDeclaration) nd).isPrimitiveType()) {
                    result = true;
                }
            } else if (gc instanceof ASTLiteral) {
                result = !((ASTLiteral) gc).isStringLiteral();
            }
        }
    }
    return result;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 14 with ASTPrimaryExpression

use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.

the class JavaNameOccurrence method isSelfAssignment.

/**
 * Assert it the occurrence is a self assignment such as:
 * <code>i += 3;</code>
 *
 * @return true, if the occurrence is self-assignment, false, otherwise.
 */
@SuppressWarnings("PMD.AvoidBranchingStatementAsLastInLoop")
public boolean isSelfAssignment() {
    Node l = location;
    while (true) {
        Node p = l.jjtGetParent();
        Node gp = p.jjtGetParent();
        Node node = gp.jjtGetParent();
        if (node instanceof ASTPreDecrementExpression || node instanceof ASTPreIncrementExpression || node instanceof ASTPostfixExpression) {
            return true;
        }
        if (hasAssignmentOperator(gp)) {
            return isCompoundAssignment(gp);
        }
        if (hasAssignmentOperator(node)) {
            return isCompoundAssignment(node);
        }
        // deal with extra parenthesis: "(i)++"
        if (p instanceof ASTPrimaryPrefix && p.jjtGetNumChildren() == 1 && gp instanceof ASTPrimaryExpression && gp.jjtGetNumChildren() == 1 && node instanceof ASTExpression && node.jjtGetNumChildren() == 1 && node.jjtGetParent() instanceof ASTPrimaryPrefix && node.jjtGetParent().jjtGetNumChildren() == 1) {
            l = node;
            continue;
        }
        // catch this.i++ or ++this.i
        return gp instanceof ASTPreDecrementExpression || gp instanceof ASTPreIncrementExpression || gp instanceof ASTPostfixExpression;
    }
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTPostfixExpression(net.sourceforge.pmd.lang.java.ast.ASTPostfixExpression) Node(net.sourceforge.pmd.lang.ast.Node) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTPreDecrementExpression(net.sourceforge.pmd.lang.java.ast.ASTPreDecrementExpression) ASTPreIncrementExpression(net.sourceforge.pmd.lang.java.ast.ASTPreIncrementExpression) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 15 with ASTPrimaryExpression

use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.

the class JavaNameOccurrence method useThisOrSuper.

/**
 * Simply return if the image start with keyword 'this' or 'super'.
 *
 * @return true, if keyword is used, false otherwise.
 */
public boolean useThisOrSuper() {
    Node node = location.jjtGetParent();
    if (node instanceof ASTPrimaryExpression) {
        ASTPrimaryExpression primaryExpression = (ASTPrimaryExpression) node;
        ASTPrimaryPrefix prefix = (ASTPrimaryPrefix) primaryExpression.jjtGetChild(0);
        if (prefix != null) {
            return prefix.usesSuperModifier() || prefix.usesThisModifier();
        }
    }
    return image.startsWith(THIS_DOT) || image.startsWith(SUPER_DOT);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) Node(net.sourceforge.pmd.lang.ast.Node) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)

Aggregations

ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)31 Node (net.sourceforge.pmd.lang.ast.Node)23 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)16 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)11 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)10 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)8 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)8 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)7 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)6 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)5 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)5 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)4 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)4 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)4 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)4 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)3 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)3 ASTEqualityExpression (net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression)3 ASTIfStatement (net.sourceforge.pmd.lang.java.ast.ASTIfStatement)3 ASTLocalVariableDeclaration (net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration)3