Search in sources :

Example 11 with NameDeclaration

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

the class MethodNameDeclarationTest method testEquality.

@Test
public void testEquality() {
    // Verify proper number of nodes are not equal
    parseCode15(SIMILAR);
    ASTClassOrInterfaceDeclaration n = acu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class).get(0);
    Map<NameDeclaration, List<NameOccurrence>> m = ((ClassScope) n.getScope()).getDeclarations();
    Set<NameDeclaration> methodNameDeclarations = m.keySet();
    assertEquals("Wrong number of method name declarations", methodNameDeclarations.size(), 3);
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Test(org.junit.Test)

Example 12 with NameDeclaration

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

the class MethodScopeTest method testMethodParameterOccurrenceRecorded.

@Test
public void testMethodParameterOccurrenceRecorded() {
    parseCode(TEST1);
    Map<NameDeclaration, List<NameOccurrence>> m = acu.findDescendantsOfType(ASTMethodDeclaration.class).get(0).getScope().getDeclarations();
    NameDeclaration vnd = m.keySet().iterator().next();
    assertEquals("bar", vnd.getImage());
    List<NameOccurrence> occs = m.get(vnd);
    NameOccurrence occ = occs.get(0);
    assertEquals(3, occ.getLocation().getBeginLine());
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) Test(org.junit.Test)

Example 13 with NameDeclaration

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

the class StringInstantiationRule method visit.

@Override
public Object visit(ASTAllocationExpression node, Object data) {
    if (!(node.jjtGetChild(0) instanceof ASTClassOrInterfaceType)) {
        return data;
    }
    if (!TypeHelper.isA((ASTClassOrInterfaceType) node.jjtGetChild(0), String.class)) {
        return data;
    }
    List<ASTExpression> exp = node.findDescendantsOfType(ASTExpression.class);
    if (exp.size() >= 2) {
        return data;
    }
    if (node.hasDescendantOfAnyType(ASTArrayDimsAndInits.class, ASTAdditiveExpression.class)) {
        return data;
    }
    ASTName name = node.getFirstDescendantOfType(ASTName.class);
    // Literal, i.e., new String("foo")
    if (name == null) {
        addViolation(data, node);
        return data;
    }
    NameDeclaration nd = name.getNameDeclaration();
    if (nd == null) {
        return data;
    }
    if (nd instanceof TypedNameDeclaration && TypeHelper.isA((TypedNameDeclaration) nd, String.class)) {
        addViolation(data, node);
    }
    return data;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) TypedNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration) TypedNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 14 with NameDeclaration

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

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

the class ClassScope method findVariableHere.

protected Set<NameDeclaration> findVariableHere(PLSQLNameOccurrence occurrence) {
    Set<NameDeclaration> result = new HashSet<>();
    Map<VariableNameDeclaration, List<NameOccurrence>> variableDeclarations = getVariableDeclarations();
    Map<MethodNameDeclaration, List<NameOccurrence>> methodDeclarations = getMethodDeclarations();
    if (occurrence.isThisOrSuper() || occurrence.getImage().equals(className)) {
        if (variableDeclarations.isEmpty() && methodDeclarations.isEmpty()) {
            // }
            return result;
        }
        // and then we'll look up X.
        if (!variableDeclarations.isEmpty()) {
            result.add(variableDeclarations.keySet().iterator().next());
            return result;
        }
        result.add(methodDeclarations.keySet().iterator().next());
        return result;
    }
    if (occurrence.isMethodOrConstructorInvocation()) {
        for (MethodNameDeclaration mnd : methodDeclarations.keySet()) {
            if (mnd.getImage().equals(occurrence.getImage())) {
                int args = occurrence.getArgumentCount();
                if (args == mnd.getParameterCount() || mnd.isVarargs() && args >= mnd.getParameterCount() - 1) {
                    // FIXME if several methods have the same name
                    // and parameter count, only one will get caught here
                    // we need to make some attempt at type lookup and
                    // discrimination
                    // or, failing that, mark this as a usage of all those
                    // methods
                    result.add(mnd);
                }
            }
        }
        return result;
    }
    List<String> images = new ArrayList<>();
    images.add(occurrence.getImage());
    if (null == occurrence.getImage()) {
        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.finest("occurrence==" + occurrence.toString() + "with Argumanet Count == " + occurrence.getArgumentCount() + " for className=" + className);
        }
    }
    if (occurrence.getImage().startsWith(className)) {
        images.add(clipClassName(occurrence.getImage()));
    }
    ImageFinderFunction finder = new ImageFinderFunction(images);
    Applier.apply(finder, getVariableDeclarations().keySet().iterator());
    if (finder.getDecl() != null) {
        result.add(finder.getDecl());
    }
    return result;
}
Also used : ImageFinderFunction(net.sourceforge.pmd.lang.symboltable.ImageFinderFunction) ArrayList(java.util.ArrayList) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Aggregations

NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)49 List (java.util.List)28 Test (org.junit.Test)28 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)13 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)12 EnumTest (net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)12 Map (java.util.Map)10 Node (net.sourceforge.pmd.lang.ast.Node)10 Scope (net.sourceforge.pmd.lang.symboltable.Scope)8 ArrayList (java.util.ArrayList)7 HashSet (java.util.HashSet)6 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)6 ImageFinderFunction (net.sourceforge.pmd.lang.symboltable.ImageFinderFunction)6 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)5 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)5 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)4 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)3 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)3 ASTName (net.sourceforge.pmd.lang.plsql.ast.ASTName)3 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)2