Search in sources :

Example 41 with NameDeclaration

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

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

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

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

the class NodeInfoPanelController method initialize.

@Override
public void initialize(URL location, ResourceBundle resources) {
    EventStreams.valuesOf(scopeHierarchyTreeView.getSelectionModel().selectedItemProperty()).filter(Objects::nonNull).map(TreeItem::getValue).filterMap(o -> o instanceof NameDeclaration, o -> (NameDeclaration) o).subscribe(parent::onNameDeclarationSelected);
    scopeHierarchyTreeView.setCellFactory(view -> new ScopeHierarchyTreeCell(parent));
}
Also used : NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Initializable(javafx.fxml.Initializable) Label(javafx.scene.control.Label) ListView(javafx.scene.control.ListView) ScopeHierarchyTreeCell(net.sourceforge.pmd.util.fxdesigner.util.controls.ScopeHierarchyTreeCell) URL(java.net.URL) TreeItem(javafx.scene.control.TreeItem) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) FXCollections(javafx.collections.FXCollections) Node(net.sourceforge.pmd.lang.ast.Node) TreeView(javafx.scene.control.TreeView) Objects(java.util.Objects) MetricEvaluator(net.sourceforge.pmd.util.fxdesigner.model.MetricEvaluator) FXML(javafx.fxml.FXML) TabPane(javafx.scene.control.TabPane) ResourceBundle(java.util.ResourceBundle) AttributeAxisIterator(net.sourceforge.pmd.lang.ast.xpath.AttributeAxisIterator) Tab(javafx.scene.control.Tab) ScopeHierarchyTreeItem(net.sourceforge.pmd.util.fxdesigner.util.controls.ScopeHierarchyTreeItem) EventStreams(org.reactfx.EventStreams) Attribute(net.sourceforge.pmd.lang.ast.xpath.Attribute) ObservableList(javafx.collections.ObservableList) Collections(java.util.Collections) MetricResult(net.sourceforge.pmd.util.fxdesigner.model.MetricResult) TreeItem(javafx.scene.control.TreeItem) ScopeHierarchyTreeItem(net.sourceforge.pmd.util.fxdesigner.util.controls.ScopeHierarchyTreeItem) ScopeHierarchyTreeCell(net.sourceforge.pmd.util.fxdesigner.util.controls.ScopeHierarchyTreeCell) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration)

Example 45 with NameDeclaration

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

the class VariableAccessVisitor method collectDeclarations.

private Set<Map<NameDeclaration, List<NameOccurrence>>> collectDeclarations(DataFlowNode inode) {
    Set<Map<NameDeclaration, List<NameOccurrence>>> decls = new HashSet<>();
    Map<NameDeclaration, List<NameOccurrence>> varDecls;
    for (int i = 0; i < inode.getFlow().size(); i++) {
        DataFlowNode n = inode.getFlow().get(i);
        if (n instanceof StartOrEndDataFlowNode) {
            continue;
        }
        varDecls = ((PLSQLNode) n.getNode()).getScope().getDeclarations();
        if (!decls.contains(varDecls)) {
            decls.add(varDecls);
        }
    }
    return decls;
}
Also used : StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) PLSQLNode(net.sourceforge.pmd.lang.plsql.ast.PLSQLNode) ArrayList(java.util.ArrayList) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Map(java.util.Map) PLSQLNameOccurrence(net.sourceforge.pmd.lang.plsql.symboltable.PLSQLNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) 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