Search in sources :

Example 1 with JavaNameOccurrence

use of net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence in project pmd by pmd.

the class VariableAccessVisitor method markUsages.

private List<VariableAccess> markUsages(DataFlowNode inode) {
    // undefinitions was once a field... seems like it works fine as a local
    List<VariableAccess> undefinitions = new ArrayList<>();
    Set<Map<VariableNameDeclaration, List<NameOccurrence>>> variableDeclarations = collectDeclarations(inode);
    for (Map<VariableNameDeclaration, List<NameOccurrence>> declarations : variableDeclarations) {
        for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : declarations.entrySet()) {
            VariableNameDeclaration vnd = entry.getKey();
            if (vnd.getAccessNodeParent() instanceof ASTFormalParameter) {
                // no definition/undefinition/references for parameters
                continue;
            } else if (vnd.getAccessNodeParent().getFirstDescendantOfType(ASTVariableInitializer.class) != null) {
                // add definition for initialized variables
                addVariableAccess(vnd.getNode(), new VariableAccess(VariableAccess.DEFINITION, vnd.getImage()), inode.getFlow());
            }
            undefinitions.add(new VariableAccess(VariableAccess.UNDEFINITION, vnd.getImage()));
            for (NameOccurrence occurrence : entry.getValue()) {
                addAccess((JavaNameOccurrence) occurrence, inode);
            }
        }
    }
    return undefinitions;
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) VariableAccess(net.sourceforge.pmd.lang.dfa.VariableAccess) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) Map(java.util.Map) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 2 with JavaNameOccurrence

use of net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence in project pmd by pmd.

the class CheckSkipResultRule method visit.

@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
    ASTType typeNode = node.getTypeNode();
    if (typeNode == null || !TypeHelper.isA(typeNode, InputStream.class)) {
        return data;
    }
    for (NameOccurrence occ : node.getUsages()) {
        JavaNameOccurrence jocc = (JavaNameOccurrence) occ;
        NameOccurrence qualifier = jocc.getNameForWhichThisIsAQualifier();
        if (qualifier != null && "skip".equals(qualifier.getImage())) {
            Node loc = jocc.getLocation();
            if (loc != null) {
                ASTPrimaryExpression exp = loc.getFirstParentOfType(ASTPrimaryExpression.class);
                while (exp != null) {
                    if (exp.jjtGetParent() instanceof ASTStatementExpression) {
                        // if exp is in a bare statement,
                        // the returned value is not used
                        addViolation(data, occ.getLocation());
                        break;
                    } else if (exp.jjtGetParent() instanceof ASTExpression && exp.jjtGetParent().jjtGetParent() instanceof ASTPrimaryPrefix) {
                        // if exp is enclosed in a pair of parenthesis
                        // let's have a look at the enclosing expression
                        // we'll see if it's in a bare statement
                        exp = exp.getFirstParentOfType(ASTPrimaryExpression.class);
                    } else {
                        // or assignement so the returned value is used
                        break;
                    }
                }
            }
        }
    }
    return data;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 3 with JavaNameOccurrence

use of net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence in project pmd by pmd.

the class ImmutableFieldRule method initializedInConstructor.

private FieldImmutabilityType initializedInConstructor(List<NameOccurrence> usages, Set<ASTConstructorDeclaration> allConstructors) {
    FieldImmutabilityType result = FieldImmutabilityType.MUTABLE;
    int methodInitCount = 0;
    int lambdaUsage = 0;
    // set of constructors accessing the field
    Set<ASTConstructorDeclaration> consSet = new HashSet<>();
    for (NameOccurrence occ : usages) {
        JavaNameOccurrence jocc = (JavaNameOccurrence) occ;
        if (jocc.isOnLeftHandSide() || jocc.isSelfAssignment()) {
            Node node = jocc.getLocation();
            ASTConstructorDeclaration constructor = node.getFirstParentOfType(ASTConstructorDeclaration.class);
            if (constructor != null) {
                if (inLoopOrTry(node)) {
                    continue;
                }
                // in one constructor only
                if (node.getFirstParentOfType(ASTIfStatement.class) != null) {
                    methodInitCount++;
                }
                if (inAnonymousInnerClass(node)) {
                    methodInitCount++;
                } else if (node.getFirstParentOfType(ASTLambdaExpression.class) != null) {
                    lambdaUsage++;
                } else {
                    consSet.add(constructor);
                }
            } else {
                if (node.getFirstParentOfType(ASTMethodDeclaration.class) != null) {
                    methodInitCount++;
                } else if (node.getFirstParentOfType(ASTLambdaExpression.class) != null) {
                    lambdaUsage++;
                }
            }
        }
    }
    if (usages.isEmpty() || methodInitCount == 0 && lambdaUsage == 0 && consSet.isEmpty()) {
        result = FieldImmutabilityType.CHECKDECL;
    } else {
        allConstructors.removeAll(consSet);
        if (allConstructors.isEmpty() && methodInitCount == 0 && lambdaUsage == 0) {
            result = FieldImmutabilityType.IMMUTABLE;
        }
    }
    return result;
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) Node(net.sourceforge.pmd.lang.ast.Node) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) HashSet(java.util.HashSet) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 4 with JavaNameOccurrence

use of net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence in project pmd by pmd.

the class AvoidReassigningParametersRule method lookForViolation.

private void lookForViolation(Map<VariableNameDeclaration, List<NameOccurrence>> params, Object data) {
    for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : params.entrySet()) {
        VariableNameDeclaration decl = entry.getKey();
        List<NameOccurrence> usages = entry.getValue();
        for (NameOccurrence occ : usages) {
            JavaNameOccurrence jocc = (JavaNameOccurrence) occ;
            if ((jocc.isOnLeftHandSide() || jocc.isSelfAssignment()) && jocc.getNameForWhichThisIsAQualifier() == null && !jocc.useThisOrSuper() && !decl.isVarargs() && (!decl.isArray() || jocc.getLocation().jjtGetParent().jjtGetParent().jjtGetNumChildren() == 1)) {
                // not an array or no primary suffix to access the array
                // values
                addViolation(data, decl.getNode(), decl.getImage());
            }
        }
    }
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) List(java.util.List) Map(java.util.Map) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 5 with JavaNameOccurrence

use of net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence in project pmd by pmd.

the class AbstractInefficientZeroCheck method visit.

@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
    Node nameNode = node.getTypeNameNode();
    if (nameNode == null || nameNode instanceof ASTPrimitiveType || !appliesToClassName(node.getNameDeclaration().getTypeImage())) {
        return data;
    }
    List<NameOccurrence> declars = node.getUsages();
    for (NameOccurrence occ : declars) {
        JavaNameOccurrence jocc = (JavaNameOccurrence) occ;
        if (!isTargetMethod(jocc)) {
            continue;
        }
        Node expr = jocc.getLocation().jjtGetParent().jjtGetParent().jjtGetParent();
        checkNodeAndReport(data, jocc.getLocation(), expr);
    }
    return data;
}
Also used : ASTPrimitiveType(net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) Node(net.sourceforge.pmd.lang.ast.Node) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Aggregations

JavaNameOccurrence (net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence)9 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)9 Node (net.sourceforge.pmd.lang.ast.Node)6 List (java.util.List)3 Map (java.util.Map)3 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)3 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)3 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)2 AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 VariableAccess (net.sourceforge.pmd.lang.dfa.VariableAccess)1 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)1 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)1 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)1 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)1 ASTIfStatement (net.sourceforge.pmd.lang.java.ast.ASTIfStatement)1 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)1 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)1