Search in sources :

Example 31 with NameOccurrence

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

the class AssignmentToNonFinalStaticRule method initializedInConstructor.

private boolean initializedInConstructor(List<NameOccurrence> usages) {
    boolean initInConstructor = false;
    for (NameOccurrence occ : usages) {
        // legitimate usages of these with static fields, e.g. typesafe enum pattern.
        if (((JavaNameOccurrence) occ).isOnLeftHandSide()) {
            Node node = occ.getLocation();
            Node constructor = node.getFirstParentOfType(ASTConstructorDeclaration.class);
            if (constructor != null) {
                initInConstructor = true;
            }
        }
    }
    return initInConstructor;
}
Also used : JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) Node(net.sourceforge.pmd.lang.ast.Node) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 32 with NameOccurrence

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

the class ClassScopeTest method testMethodUsageSeenWithThis.

@Test
public void testMethodUsageSeenWithThis() {
    parseCode(METHOD_USAGE_SEEN_WITH_THIS);
    ASTClassOrInterfaceDeclaration n = acu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class).get(0);
    Map<NameDeclaration, List<NameOccurrence>> m = ((ClassScope) n.getScope()).getDeclarations();
    Iterator<Map.Entry<NameDeclaration, List<NameOccurrence>>> i = m.entrySet().iterator();
    MethodNameDeclaration mnd;
    Map.Entry<NameDeclaration, List<NameOccurrence>> entry;
    do {
        entry = i.next();
        mnd = (MethodNameDeclaration) entry.getKey();
    } while (!mnd.getImage().equals("bar"));
    List<NameOccurrence> usages = entry.getValue();
    assertEquals(1, usages.size());
    assertEquals("bar", ((JavaNameOccurrence) usages.get(0)).getImage());
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) 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) EnumTest(net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)

Example 33 with NameOccurrence

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

the class ClassScopeTest method testNestedClassFieldAndParameter.

/**
 * Test case for bug report #2410201
 */
@Test
public void testNestedClassFieldAndParameter() {
    parseCode(NESTED_CLASS_FIELD_AND_PARAM);
    ASTMethodDeclaration node = acu.getFirstDescendantOfType(ASTMethodDeclaration.class);
    Map<NameDeclaration, List<NameOccurrence>> vd = node.getScope().getDeclarations();
    assertEquals(1, vd.size());
    for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : vd.entrySet()) {
        assertEquals("field", entry.getKey().getImage());
        List<NameOccurrence> occurrences = entry.getValue();
        assertEquals(2, occurrences.size());
        NameOccurrence no1 = occurrences.get(0);
        assertEquals(8, no1.getLocation().getBeginLine());
        NameOccurrence no2 = occurrences.get(1);
        assertEquals(9, no2.getLocation().getBeginLine());
    }
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) 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) EnumTest(net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)

Example 34 with NameOccurrence

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

the class ClassScopeTest method testMethodUsageSeen.

@Test
public void testMethodUsageSeen() {
    parseCode(METHOD_USAGE_SEEN);
    ASTClassOrInterfaceDeclaration n = acu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class).get(0);
    Map<NameDeclaration, List<NameOccurrence>> m = ((ClassScope) n.getScope()).getDeclarations();
    Iterator<Map.Entry<NameDeclaration, List<NameOccurrence>>> i = m.entrySet().iterator();
    MethodNameDeclaration mnd;
    Map.Entry<NameDeclaration, List<NameOccurrence>> entry;
    do {
        entry = i.next();
        mnd = (MethodNameDeclaration) entry.getKey();
    } while (!mnd.getImage().equals("bar"));
    List<NameOccurrence> usages = entry.getValue();
    assertEquals(1, usages.size());
    assertEquals("bar", ((JavaNameOccurrence) usages.get(0)).getImage());
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) 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) EnumTest(net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)

Example 35 with NameOccurrence

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

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