Search in sources :

Example 11 with Scope

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

the class LocalVariableCouldBeFinalRule method visit.

public Object visit(ASTLocalVariableDeclaration node, Object data) {
    if (node.isFinal()) {
        return data;
    }
    Scope s = node.getScope();
    Map<VariableNameDeclaration, List<NameOccurrence>> decls = s.getDeclarations(VariableNameDeclaration.class);
    for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : decls.entrySet()) {
        VariableNameDeclaration var = entry.getKey();
        if (var.getAccessNodeParent() != node) {
            continue;
        }
        if (!assigned(entry.getValue())) {
            addViolation(data, var.getAccessNodeParent(), var.getImage());
        }
    }
    return data;
}
Also used : Scope(net.sourceforge.pmd.lang.symboltable.Scope) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) List(java.util.List) Map(java.util.Map)

Example 12 with Scope

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

the class AcceptanceTest method testDemo.

@Test
public void testDemo() {
    parseCode(TEST_DEMO);
    // System.out.println(TEST_DEMO);
    ASTMethodDeclaration node = acu.findDescendantsOfType(ASTMethodDeclaration.class).get(0);
    Scope s = node.getScope();
    Map<NameDeclaration, List<NameOccurrence>> m = s.getDeclarations();
    for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : m.entrySet()) {
        assertEquals("buz", entry.getKey().getImage());
        assertEquals("ArrayList", ((TypedNameDeclaration) entry.getKey()).getTypeImage());
        List<NameOccurrence> u = entry.getValue();
        assertEquals(1, u.size());
        NameOccurrence o = u.get(0);
        int beginLine = o.getLocation().getBeginLine();
        assertEquals(3, beginLine);
    // System.out.println("Variable: " + d.getImage());
    // System.out.println("Type: " + d.getTypeImage());
    // System.out.println("Usages: " + u.size());
    // System.out.println("Used in line " + beginLine);
    }
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) Scope(net.sourceforge.pmd.lang.symboltable.Scope) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Map(java.util.Map) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) Test(org.junit.Test)

Example 13 with Scope

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

the class AcceptanceTest method testCatchBlocks.

@Test
public void testCatchBlocks() {
    parseCode(TEST_CATCH_BLOCKS);
    ASTCatchStatement c = acu.findDescendantsOfType(ASTCatchStatement.class).get(0);
    ASTBlock a = c.findDescendantsOfType(ASTBlock.class).get(0);
    Scope s = a.getScope();
    Map<NameDeclaration, List<NameOccurrence>> vars = s.getParent().getDeclarations();
    assertEquals(1, vars.size());
    NameDeclaration v = vars.keySet().iterator().next();
    assertEquals("e", v.getImage());
    assertEquals(1, (vars.get(v)).size());
}
Also used : Scope(net.sourceforge.pmd.lang.symboltable.Scope) ASTBlock(net.sourceforge.pmd.lang.java.ast.ASTBlock) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) ASTCatchStatement(net.sourceforge.pmd.lang.java.ast.ASTCatchStatement) Test(org.junit.Test)

Example 14 with Scope

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

the class AcceptanceTest method testEq.

@Test
public void testEq() {
    parseCode(TEST_EQ);
    ASTEqualityExpression e = acu.findDescendantsOfType(ASTEqualityExpression.class).get(0);
    ASTMethodDeclaration method = e.getFirstParentOfType(ASTMethodDeclaration.class);
    Scope s = method.getScope();
    Map<NameDeclaration, List<NameOccurrence>> m = s.getDeclarations();
    assertEquals(2, m.size());
    for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : m.entrySet()) {
        NameDeclaration vnd = entry.getKey();
        List<NameOccurrence> usages = entry.getValue();
        if (vnd.getImage().equals("a") || vnd.getImage().equals("b")) {
            assertEquals(1, usages.size());
            assertEquals(3, usages.get(0).getLocation().getBeginLine());
        } else {
            fail("Unkown variable " + vnd);
        }
    }
}
Also used : ASTEqualityExpression(net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) Scope(net.sourceforge.pmd.lang.symboltable.Scope) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Map(java.util.Map) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) Test(org.junit.Test)

Example 15 with Scope

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

the class OccurrenceFinder method visit.

@Override
public Object visit(ASTPrimaryExpression node, Object data) {
    NameFinder nameFinder = new NameFinder(node);
    // Maybe do some sort of State pattern thingy for when NameDeclaration
    // is empty/not empty
    Set<NameDeclaration> declarations = new HashSet<>();
    List<PLSQLNameOccurrence> names = nameFinder.getNames();
    for (PLSQLNameOccurrence occ : names) {
        Search search = new Search(occ);
        if (declarations.isEmpty()) {
            // doing the first name lookup
            search.execute();
            declarations.addAll(search.getResult());
            if (declarations.isEmpty()) {
                // SymbolNotFoundException
                break;
            }
        } else {
            Set<NameDeclaration> additionalDeclarations = new HashSet<>();
            for (NameDeclaration decl : declarations) {
                // now we've got a scope we're starting with, so work from
                // there
                Scope scope = decl.getScope();
                if (null == scope) {
                    if (LOGGER.isLoggable(Level.FINEST)) {
                        LOGGER.finest("NameOccurrence has no Scope:" + decl.getClass().getCanonicalName() + "=>" + decl.getImage());
                    }
                    break;
                }
                search.execute(scope);
                Set<NameDeclaration> found = search.getResult();
                additionalDeclarations.addAll(found);
                if (found.isEmpty()) {
                    // SymbolNotFoundException
                    break;
                }
            }
            declarations.addAll(additionalDeclarations);
        }
    }
    return super.visit(node, data);
}
Also used : Scope(net.sourceforge.pmd.lang.symboltable.Scope) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) HashSet(java.util.HashSet)

Aggregations

Scope (net.sourceforge.pmd.lang.symboltable.Scope)19 List (java.util.List)9 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)8 Test (org.junit.Test)5 Map (java.util.Map)4 PLSQLNode (net.sourceforge.pmd.lang.plsql.ast.PLSQLNode)4 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)4 Node (net.sourceforge.pmd.lang.ast.Node)3 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)3 ArrayList (java.util.ArrayList)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)2 ASTImplementsList (net.sourceforge.pmd.lang.java.ast.ASTImplementsList)2 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)2 AbstractJavaNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaNode)2 JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)2 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)2 HashSet (java.util.HashSet)1 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)1 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)1