Search in sources :

Example 11 with ASTPrimarySuffix

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

the class MethodReturnsInternalArrayRule method visit.

@Override
public Object visit(ASTMethodDeclaration method, Object data) {
    if (!method.getResultType().returnsArray() || method.isPrivate()) {
        return data;
    }
    List<ASTReturnStatement> returns = method.findDescendantsOfType(ASTReturnStatement.class);
    ASTTypeDeclaration td = method.getFirstParentOfType(ASTTypeDeclaration.class);
    for (ASTReturnStatement ret : returns) {
        final String vn = getReturnedVariableName(ret);
        if (!isField(vn, td)) {
            continue;
        }
        if (ret.findDescendantsOfType(ASTPrimarySuffix.class).size() > 2) {
            continue;
        }
        if (ret.hasDescendantOfType(ASTAllocationExpression.class)) {
            continue;
        }
        if (hasArraysCopyOf(ret)) {
            continue;
        }
        if (hasClone(ret, vn)) {
            continue;
        }
        if (isEmptyArray(vn, td)) {
            continue;
        }
        if (!isLocalVariable(vn, method)) {
            addViolation(data, ret, vn);
        } else {
            // This is to handle field hiding
            final ASTPrimaryPrefix pp = ret.getFirstDescendantOfType(ASTPrimaryPrefix.class);
            if (pp != null && pp.usesThisModifier()) {
                final ASTPrimarySuffix ps = ret.getFirstDescendantOfType(ASTPrimarySuffix.class);
                if (ps.hasImageEqualTo(vn)) {
                    addViolation(data, ret, vn);
                }
            }
        }
    }
    return data;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTTypeDeclaration(net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration) ASTReturnStatement(net.sourceforge.pmd.lang.java.ast.ASTReturnStatement) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Example 12 with ASTPrimarySuffix

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

the class AbstractSunSecureRule method getReturnedVariableName.

/**
 * Gets the name of the variable returned. Some examples: <br>
 * for this.foo returns foo <br>
 * for foo returns foo <br>
 * for foo.bar returns foo.bar
 *
 * @param ret
 *            a return statement to evaluate
 * @return the name of the variable associated or <code>null</code> if it
 *         cannot be detected
 */
protected final String getReturnedVariableName(ASTReturnStatement ret) {
    if (hasTernaryCondition(ret) && hasTernaryNullCheck(ret)) {
        return ret.getFirstDescendantOfType(ASTConditionalExpression.class).jjtGetChild(0).getFirstDescendantOfType(ASTName.class).getImage();
    }
    final ASTName n = ret.getFirstDescendantOfType(ASTName.class);
    if (n != null) {
        return n.getImage();
    }
    final ASTPrimarySuffix ps = ret.getFirstDescendantOfType(ASTPrimarySuffix.class);
    if (ps != null) {
        return ps.getImage();
    }
    return null;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTConditionalExpression(net.sourceforge.pmd.lang.java.ast.ASTConditionalExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Example 13 with ASTPrimarySuffix

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

the class ArrayIsStoredDirectlyRule method getExpressionVarName.

private String getExpressionVarName(Node e) {
    String assignedVar = getFirstNameImage(e);
    if (assignedVar == null) {
        ASTPrimarySuffix suffix = e.getFirstDescendantOfType(ASTPrimarySuffix.class);
        if (suffix != null) {
            assignedVar = suffix.getImage();
            ASTPrimaryPrefix prefix = e.getFirstDescendantOfType(ASTPrimaryPrefix.class);
            if (prefix != null) {
                if (prefix.usesThisModifier()) {
                    assignedVar = "this." + assignedVar;
                } else if (prefix.usesSuperModifier()) {
                    assignedVar = "super." + assignedVar;
                }
            }
        }
    }
    return assignedVar;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Example 14 with ASTPrimarySuffix

use of net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix 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 15 with ASTPrimarySuffix

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

the class TccAttributeAccessCollector method getVariableName.

private String getVariableName(ASTPrimaryExpression node) {
    ASTPrimaryPrefix prefix = node.getFirstDescendantOfType(ASTPrimaryPrefix.class);
    if (prefix.usesThisModifier()) {
        List<ASTPrimarySuffix> suffixes = node.findChildrenOfType(ASTPrimarySuffix.class);
        if (suffixes.size() > 1) {
            if (!suffixes.get(1).isArguments()) {
                // not a method call
                return suffixes.get(0).getImage();
            }
        }
    }
    ASTName name = prefix.getFirstDescendantOfType(ASTName.class);
    String variableName = null;
    if (name != null) {
        int dotIndex = name.getImage().indexOf(".");
        if (dotIndex == -1) {
            variableName = name.getImage();
        } else {
            variableName = name.getImage().substring(0, dotIndex);
        }
    }
    return variableName;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Aggregations

ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)24 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)14 Node (net.sourceforge.pmd.lang.ast.Node)12 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)11 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)10 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)4 ArrayList (java.util.ArrayList)3 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)3 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)3 List (java.util.List)2 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)2 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)2 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)2 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)2 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)2 ASTClassOrInterfaceBodyDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration)2 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)2 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)2 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)2 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)2