Search in sources :

Example 6 with ASTPrimaryExpression

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

the class DoubleCheckedLockingRule method visit.

@Override
public Object visit(ASTMethodDeclaration node, Object data) {
    if (node.getResultType().isVoid()) {
        return super.visit(node, data);
    }
    ASTType typeNode = (ASTType) node.getResultType().jjtGetChild(0);
    if (typeNode.jjtGetNumChildren() == 0 || !(typeNode.jjtGetChild(0) instanceof ASTReferenceType)) {
        return super.visit(node, data);
    }
    List<ASTReturnStatement> rsl = node.findDescendantsOfType(ASTReturnStatement.class);
    if (rsl.size() != 1) {
        return super.visit(node, data);
    }
    ASTReturnStatement rs = rsl.get(0);
    List<ASTPrimaryExpression> pel = rs.findDescendantsOfType(ASTPrimaryExpression.class);
    ASTPrimaryExpression ape = pel.get(0);
    Node lastChild = ape.jjtGetChild(ape.jjtGetNumChildren() - 1);
    String returnVariableName = null;
    if (lastChild instanceof ASTPrimaryPrefix) {
        returnVariableName = getNameFromPrimaryPrefix((ASTPrimaryPrefix) lastChild);
    }
    // With Java5 and volatile keyword, DCL is no longer an issue
    if (returnVariableName == null || this.volatileFields.contains(returnVariableName)) {
        return super.visit(node, data);
    }
    // field, then it's ok, too
    if (checkLocalVariableUsage(node, returnVariableName)) {
        return super.visit(node, data);
    }
    List<ASTIfStatement> isl = node.findDescendantsOfType(ASTIfStatement.class);
    if (isl.size() == 2) {
        ASTIfStatement is = isl.get(0);
        if (ifVerify(is, returnVariableName)) {
            // find synchronized
            List<ASTSynchronizedStatement> ssl = is.findDescendantsOfType(ASTSynchronizedStatement.class);
            if (ssl.size() == 1) {
                ASTSynchronizedStatement ss = ssl.get(0);
                isl = ss.findDescendantsOfType(ASTIfStatement.class);
                if (isl.size() == 1) {
                    ASTIfStatement is2 = isl.get(0);
                    if (ifVerify(is2, returnVariableName)) {
                        List<ASTStatementExpression> sel = is2.findDescendantsOfType(ASTStatementExpression.class);
                        if (sel.size() == 1) {
                            ASTStatementExpression se = sel.get(0);
                            if (se.jjtGetNumChildren() == 3) {
                                // primaryExpression, AssignmentOperator, Expression
                                if (se.jjtGetChild(0) instanceof ASTPrimaryExpression) {
                                    ASTPrimaryExpression pe = (ASTPrimaryExpression) se.jjtGetChild(0);
                                    if (matchName(pe, returnVariableName)) {
                                        if (se.jjtGetChild(1) instanceof ASTAssignmentOperator) {
                                            addViolation(data, node);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTAssignmentOperator(net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator) ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) Node(net.sourceforge.pmd.lang.ast.Node) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTSynchronizedStatement(net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTReturnStatement(net.sourceforge.pmd.lang.java.ast.ASTReturnStatement) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType)

Example 7 with ASTPrimaryExpression

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

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

the class SingletonClassReturningNewInstanceRule method getReturnVariableName.

private String getReturnVariableName(ASTMethodDeclaration node) {
    List<ASTReturnStatement> rsl = node.findDescendantsOfType(ASTReturnStatement.class);
    ASTReturnStatement rs = rsl.get(0);
    List<ASTPrimaryExpression> pel = rs.findDescendantsOfType(ASTPrimaryExpression.class);
    ASTPrimaryExpression ape = pel.get(0);
    Node lastChild = ape.jjtGetChild(0);
    String returnVariableName = null;
    if (lastChild instanceof ASTPrimaryPrefix) {
        returnVariableName = getNameFromPrimaryPrefix((ASTPrimaryPrefix) lastChild);
    }
    /*
         * if(lastChild instanceof ASTPrimarySuffix){ returnVariableName =
         * getNameFromPrimarySuffix((ASTPrimarySuffix) lastChild); }
         */
    return returnVariableName;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTReturnStatement(net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)

Example 9 with ASTPrimaryExpression

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

the class SingletonClassReturningNewInstanceRule method visit.

@Override
public Object visit(ASTMethodDeclaration node, Object data) {
    boolean violation = false;
    String localVarName = null;
    String returnVariableName = null;
    if (node.getResultType().isVoid()) {
        return super.visit(node, data);
    }
    if ("getInstance".equals(node.getMethodName())) {
        List<ASTReturnStatement> rsl = node.findDescendantsOfType(ASTReturnStatement.class);
        if (rsl.isEmpty()) {
            return super.visit(node, data);
        } else {
            for (ASTReturnStatement rs : rsl) {
                List<ASTPrimaryExpression> pel = rs.findDescendantsOfType(ASTPrimaryExpression.class);
                ASTPrimaryExpression ape = pel.get(0);
                if (ape.getFirstDescendantOfType(ASTAllocationExpression.class) != null) {
                    violation = true;
                    break;
                }
            }
        }
        /*
             * public class Singleton {
             * 
             * private static Singleton m_instance=null;
             * 
             * public static Singleton getInstance() {
             * 
             * Singleton m_instance=null;
             * 
             * if ( m_instance == null ) { synchronized(Singleton.class) {
             * if(m_instance == null) { m_instance = new Singleton(); } } }
             * return m_instance; } }
             */
        List<ASTBlockStatement> astBlockStatements = node.findDescendantsOfType(ASTBlockStatement.class);
        returnVariableName = getReturnVariableName(node);
        if (!astBlockStatements.isEmpty()) {
            for (ASTBlockStatement blockStatement : astBlockStatements) {
                if (blockStatement.hasDescendantOfType(ASTLocalVariableDeclaration.class)) {
                    List<ASTLocalVariableDeclaration> lVarList = blockStatement.findDescendantsOfType(ASTLocalVariableDeclaration.class);
                    if (!lVarList.isEmpty()) {
                        for (ASTLocalVariableDeclaration localVar : lVarList) {
                            localVarName = localVar.getVariableName();
                            if (returnVariableName != null && returnVariableName.equals(localVarName)) {
                                violation = true;
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    if (violation) {
        addViolation(data, node);
    }
    return super.visit(node, data);
}
Also used : ASTLocalVariableDeclaration(net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) ASTReturnStatement(net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)

Example 10 with ASTPrimaryExpression

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

the class UselessStringValueOfRule method visit.

@Override
public Object visit(ASTPrimaryPrefix node, Object data) {
    if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) {
        return super.visit(node, data);
    }
    String image = ((ASTName) node.jjtGetChild(0)).getImage();
    if ("String.valueOf".equals(image)) {
        Node parent = node.jjtGetParent();
        if (parent.jjtGetNumChildren() != 2) {
            return super.visit(node, data);
        }
        // skip String.valueOf(anyarraytype[])
        ASTArgumentList args = parent.getFirstDescendantOfType(ASTArgumentList.class);
        if (args != null) {
            ASTName arg = args.getFirstDescendantOfType(ASTName.class);
            if (arg != null) {
                NameDeclaration declaration = arg.getNameDeclaration();
                if (declaration != null) {
                    ASTType argType = declaration.getNode().jjtGetParent().jjtGetParent().getFirstDescendantOfType(ASTType.class);
                    if (argType != null && argType.jjtGetChild(0) instanceof ASTReferenceType && ((ASTReferenceType) argType.jjtGetChild(0)).isArray()) {
                        return super.visit(node, data);
                    }
                }
            }
        }
        Node gp = parent.jjtGetParent();
        if (parent instanceof ASTPrimaryExpression && gp instanceof ASTAdditiveExpression && "+".equals(gp.getImage())) {
            boolean ok = false;
            if (gp.jjtGetChild(0) == parent) {
                ok = !isPrimitive(gp.jjtGetChild(1));
            } else {
                for (int i = 0; !ok && gp.jjtGetChild(i) != parent; i++) {
                    ok = !isPrimitive(gp.jjtGetChild(i));
                }
            }
            if (ok) {
                super.addViolation(data, node);
                return data;
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTAdditiveExpression(net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList)

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