Search in sources :

Example 6 with ASTAssignmentOperator

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

the class NonThreadSafeSingletonRule method visit.

@Override
public Object visit(ASTMethodDeclaration node, Object data) {
    if (checkNonStaticMethods && !node.isStatic() || node.isSynchronized()) {
        return super.visit(node, data);
    }
    List<ASTIfStatement> ifStatements = node.findDescendantsOfType(ASTIfStatement.class);
    for (ASTIfStatement ifStatement : ifStatements) {
        if (ifStatement.getFirstParentOfType(ASTSynchronizedStatement.class) == null) {
            if (!ifStatement.hasDescendantOfType(ASTNullLiteral.class)) {
                continue;
            }
            ASTName n = ifStatement.getFirstDescendantOfType(ASTName.class);
            if (n == null || !fieldDecls.containsKey(n.getImage())) {
                continue;
            }
            List<ASTAssignmentOperator> assigmnents = ifStatement.findDescendantsOfType(ASTAssignmentOperator.class);
            boolean violation = false;
            for (int ix = 0; ix < assigmnents.size(); ix++) {
                ASTAssignmentOperator oper = assigmnents.get(ix);
                if (!(oper.jjtGetParent() instanceof ASTStatementExpression)) {
                    continue;
                }
                ASTStatementExpression expr = (ASTStatementExpression) oper.jjtGetParent();
                if (expr.jjtGetChild(0) instanceof ASTPrimaryExpression && ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetNumChildren() == 1 && ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetChild(0) instanceof ASTPrimaryPrefix) {
                    ASTPrimaryPrefix pp = (ASTPrimaryPrefix) ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetChild(0);
                    String name = null;
                    if (pp.usesThisModifier()) {
                        ASTPrimarySuffix priSuf = expr.getFirstDescendantOfType(ASTPrimarySuffix.class);
                        name = priSuf.getImage();
                    } else {
                        ASTName astName = (ASTName) pp.jjtGetChild(0);
                        name = astName.getImage();
                    }
                    if (fieldDecls.containsKey(name)) {
                        violation = true;
                    }
                }
            }
            if (violation) {
                addViolation(data, ifStatement);
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTAssignmentOperator(net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTSynchronizedStatement(net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement) ASTNullLiteral(net.sourceforge.pmd.lang.java.ast.ASTNullLiteral) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName)

Aggregations

ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)6 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)5 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)5 Node (net.sourceforge.pmd.lang.ast.Node)4 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)4 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)3 ASTEqualityExpression (net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression)2 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)2 ASTIfStatement (net.sourceforge.pmd.lang.java.ast.ASTIfStatement)2 ASTLocalVariableDeclaration (net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration)2 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)2 ASTSynchronizedStatement (net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)1 ASTConditionalExpression (net.sourceforge.pmd.lang.java.ast.ASTConditionalExpression)1 ASTNullLiteral (net.sourceforge.pmd.lang.java.ast.ASTNullLiteral)1 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)1 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)1 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)1 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)1 ASTVariableInitializer (net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer)1