Search in sources :

Example 36 with ASTName

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

the class LocalScope method addNameOccurrence.

public Set<NameDeclaration> addNameOccurrence(NameOccurrence occurrence) {
    JavaNameOccurrence javaOccurrence = (JavaNameOccurrence) occurrence;
    Set<NameDeclaration> declarations = findVariableHere(javaOccurrence);
    if (!declarations.isEmpty() && !javaOccurrence.isThisOrSuper()) {
        for (NameDeclaration decl : declarations) {
            List<NameOccurrence> nameOccurrences = getVariableDeclarations().get(decl);
            nameOccurrences.add(javaOccurrence);
            Node n = javaOccurrence.getLocation();
            if (n instanceof ASTName) {
                ((ASTName) n).setNameDeclaration(decl);
            }
        // TODO what to do with PrimarySuffix case?
        }
    }
    return declarations;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 37 with ASTName

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

the class InefficientStringBufferingRule method visit.

@Override
public Object visit(ASTAdditiveExpression node, Object data) {
    ASTBlockStatement bs = node.getFirstParentOfType(ASTBlockStatement.class);
    if (bs == null) {
        return data;
    }
    int immediateLiterals = 0;
    int immediateStringLiterals = 0;
    List<ASTLiteral> nodes = node.findDescendantsOfType(ASTLiteral.class);
    for (ASTLiteral literal : nodes) {
        if (literal.getNthParent(3) instanceof ASTAdditiveExpression) {
            immediateLiterals++;
            if (literal.isStringLiteral()) {
                immediateStringLiterals++;
            }
        }
        if (literal.isIntLiteral() || literal.isFloatLiteral() || literal.isDoubleLiteral() || literal.isLongLiteral()) {
            return data;
        }
    }
    if (immediateLiterals > 1) {
        return data;
    }
    // if literal + public static final, return
    List<ASTName> nameNodes = node.findDescendantsOfType(ASTName.class);
    for (ASTName name : nameNodes) {
        if (name.getNameDeclaration() != null && name.getNameDeclaration() instanceof VariableNameDeclaration) {
            VariableNameDeclaration vnd = (VariableNameDeclaration) name.getNameDeclaration();
            AccessNode accessNodeParent = vnd.getAccessNodeParent();
            if (accessNodeParent.isFinal() && accessNodeParent.isStatic()) {
                return data;
            }
        }
    }
    // if literal primitive type and not strings variables, then return
    boolean stringFound = false;
    for (ASTName name : nameNodes) {
        if (!isPrimitiveType(name) && isStringType(name)) {
            stringFound = true;
            break;
        }
    }
    if (!stringFound && immediateStringLiterals == 0) {
        return data;
    }
    if (bs.isAllocation()) {
        for (Iterator<ASTName> iterator = nameNodes.iterator(); iterator.hasNext(); ) {
            ASTName name = iterator.next();
            if (!name.getImage().endsWith("length")) {
                break;
            } else if (!iterator.hasNext()) {
                // All names end with length
                return data;
            }
        }
        if (isAllocatedStringBuffer(node)) {
            addViolation(data, node);
        }
    } else if (isInStringBufferOperation(node, 6, "append")) {
        addViolation(data, node);
    }
    return data;
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTAdditiveExpression(net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 38 with ASTName

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

the class InsufficientStringBufferDeclarationRule method getConstructorLength.

private int getConstructorLength(Node node, int constructorLength) {
    int iConstructorLength = constructorLength;
    Node block = node.getFirstParentOfType(ASTBlockStatement.class);
    if (block == null) {
        block = node.getFirstParentOfType(ASTFieldDeclaration.class);
    }
    if (block == null) {
        block = node.getFirstParentOfType(ASTFormalParameter.class);
        if (block != null) {
            iConstructorLength = -1;
        } else {
            return DEFAULT_BUFFER_SIZE;
        }
    }
    // if there is any addition/subtraction going on then just use the
    // default.
    ASTAdditiveExpression exp = block.getFirstDescendantOfType(ASTAdditiveExpression.class);
    if (exp != null) {
        return DEFAULT_BUFFER_SIZE;
    }
    ASTMultiplicativeExpression mult = block.getFirstDescendantOfType(ASTMultiplicativeExpression.class);
    if (mult != null) {
        return DEFAULT_BUFFER_SIZE;
    }
    List<ASTLiteral> literals;
    ASTAllocationExpression constructorCall = block.getFirstDescendantOfType(ASTAllocationExpression.class);
    if (constructorCall != null) {
        // if this is a constructor call, only consider the literals within
        // it.
        literals = constructorCall.findDescendantsOfType(ASTLiteral.class);
    } else {
        // otherwise it might be a setLength call...
        literals = block.findDescendantsOfType(ASTLiteral.class);
    }
    if (literals.isEmpty()) {
        List<ASTName> name = block.findDescendantsOfType(ASTName.class);
        if (!name.isEmpty()) {
            iConstructorLength = -1;
        }
    } else if (literals.size() == 1) {
        ASTLiteral literal = literals.get(0);
        String str = literal.getImage();
        if (str == null) {
            iConstructorLength = 0;
        } else if (isStringOrCharLiteral(literal)) {
            // since it's not taken into account
            // anywhere. only count the extra 16
            // characters
            // don't add the constructor's length
            iConstructorLength = 14 + str.length();
        } else if (literal.isIntLiteral()) {
            iConstructorLength = literal.getValueAsInt();
        }
    } else {
        iConstructorLength = -1;
    }
    if (iConstructorLength == 0) {
        if (constructorLength == -1) {
            iConstructorLength = DEFAULT_BUFFER_SIZE;
        } else {
            iConstructorLength = constructorLength;
        }
    }
    return iConstructorLength;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTMultiplicativeExpression(net.sourceforge.pmd.lang.java.ast.ASTMultiplicativeExpression) ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration) ASTAdditiveExpression(net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 39 with ASTName

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

the class UseStringBufferForStringAppendsRule method visit.

@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
    if (!TypeHelper.isA(node, String.class) || node.isArray()) {
        return data;
    }
    Node parent = node.jjtGetParent().jjtGetParent();
    if (!(parent instanceof ASTLocalVariableDeclaration)) {
        return data;
    }
    for (NameOccurrence no : node.getUsages()) {
        Node name = no.getLocation();
        ASTStatementExpression statement = name.getFirstParentOfType(ASTStatementExpression.class);
        if (statement == null) {
            continue;
        }
        ASTArgumentList argList = name.getFirstParentOfType(ASTArgumentList.class);
        if (argList != null && argList.getFirstParentOfType(ASTStatementExpression.class) == statement) {
            // used in method call
            continue;
        }
        ASTEqualityExpression equality = name.getFirstParentOfType(ASTEqualityExpression.class);
        if (equality != null && equality.getFirstParentOfType(ASTStatementExpression.class) == statement) {
            // used in condition
            continue;
        }
        ASTConditionalExpression conditional = name.getFirstParentOfType(ASTConditionalExpression.class);
        if (conditional != null) {
            Node thirdParent = name.getNthParent(3);
            Node fourthParent = name.getNthParent(4);
            if ((Objects.equals(thirdParent, conditional) || Objects.equals(fourthParent, conditional)) && conditional.getFirstParentOfType(ASTStatementExpression.class) == statement) {
                // string)
                continue;
            }
        }
        if (statement.jjtGetNumChildren() > 0 && statement.jjtGetChild(0) instanceof ASTPrimaryExpression) {
            ASTName astName = statement.jjtGetChild(0).getFirstDescendantOfType(ASTName.class);
            if (astName != null) {
                if (astName.equals(name)) {
                    ASTAssignmentOperator assignmentOperator = statement.getFirstDescendantOfType(ASTAssignmentOperator.class);
                    if (assignmentOperator != null && assignmentOperator.isCompound()) {
                        addViolation(data, assignmentOperator);
                    }
                } else if (astName.getImage().equals(name.getImage())) {
                    ASTAssignmentOperator assignmentOperator = statement.getFirstDescendantOfType(ASTAssignmentOperator.class);
                    if (assignmentOperator != null && !assignmentOperator.isCompound()) {
                        addViolation(data, astName);
                    }
                }
            }
        }
    }
    return data;
}
Also used : ASTEqualityExpression(net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression) ASTAssignmentOperator(net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator) ASTLocalVariableDeclaration(net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTConditionalExpression(net.sourceforge.pmd.lang.java.ast.ASTConditionalExpression) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 40 with ASTName

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

ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)53 Node (net.sourceforge.pmd.lang.ast.Node)25 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)17 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)14 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)11 ArrayList (java.util.ArrayList)10 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)10 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)8 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)8 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)7 List (java.util.List)6 ASTAnnotation (net.sourceforge.pmd.lang.java.ast.ASTAnnotation)6 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)6 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)6 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)5 Map (java.util.Map)4 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)4 ASTClassOrInterfaceBodyDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration)4 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)4 ASTAdditiveExpression (net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression)3