Search in sources :

Example 6 with NameOccurrence

use of net.sourceforge.pmd.lang.symboltable.NameOccurrence 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 7 with NameOccurrence

use of net.sourceforge.pmd.lang.symboltable.NameOccurrence 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<NameDeclaration, List<NameOccurrence>>> variableDeclarations = collectDeclarations(inode);
    for (Map<NameDeclaration, List<NameOccurrence>> declarations : variableDeclarations) {
        for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : declarations.entrySet()) {
            NameDeclaration vnd = entry.getKey();
            if (vnd.getNode().jjtGetParent() instanceof ASTFormalParameter) {
                // no definition/undefinition/references for parameters
                continue;
            } else if (vnd.getNode().jjtGetParent().getFirstDescendantOfType(ASTVariableOrConstantInitializer.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(occurrence, inode);
            }
        }
    }
    return undefinitions;
}
Also used : VariableAccess(net.sourceforge.pmd.lang.dfa.VariableAccess) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) ASTFormalParameter(net.sourceforge.pmd.lang.plsql.ast.ASTFormalParameter) Map(java.util.Map) PLSQLNameOccurrence(net.sourceforge.pmd.lang.plsql.symboltable.PLSQLNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 8 with NameOccurrence

use of net.sourceforge.pmd.lang.symboltable.NameOccurrence in project pmd by pmd.

the class LocalScope method addNameOccurrence.

@Override
public Set<NameDeclaration> addNameOccurrence(NameOccurrence occ) {
    PLSQLNameOccurrence occurrence = (PLSQLNameOccurrence) occ;
    Set<NameDeclaration> declarations = findVariableHere(occurrence);
    if (!declarations.isEmpty() && !occurrence.isThisOrSuper()) {
        for (NameDeclaration decl : declarations) {
            List<NameOccurrence> nameOccurrences = getVariableDeclarations().get(decl);
            nameOccurrences.add(occurrence);
            Node n = occurrence.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.plsql.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 9 with NameOccurrence

use of net.sourceforge.pmd.lang.symboltable.NameOccurrence in project pmd by pmd.

the class NameFinder method toString.

@Override
public String toString() {
    StringBuilder result = new StringBuilder();
    for (NameOccurrence occ : names) {
        result.append(occ);
        result.append(PMD.EOL);
    }
    return result.toString();
}
Also used : NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 10 with NameOccurrence

use of net.sourceforge.pmd.lang.symboltable.NameOccurrence 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)

Aggregations

NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)40 Node (net.sourceforge.pmd.lang.ast.Node)20 List (java.util.List)19 Map (java.util.Map)15 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)13 JavaNameOccurrence (net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence)10 Test (org.junit.Test)10 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)9 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)8 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)8 ArrayList (java.util.ArrayList)7 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)7 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)7 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)4 Scope (net.sourceforge.pmd.lang.symboltable.Scope)4 HashSet (java.util.HashSet)3 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)3 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)3 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)3 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)3