Search in sources :

Example 36 with NameDeclaration

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

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

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

the class ClassScopeTest method testTwoParamsVararg.

@Test
public final void testTwoParamsVararg() {
    parseCode15(TWO_PARAMS_VARARG);
    ASTClassOrInterfaceDeclaration n = acu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class).get(0);
    Map<NameDeclaration, List<NameOccurrence>> m = ((ClassScope) n.getScope()).getDeclarations();
    MethodNameDeclaration mnd = (MethodNameDeclaration) m.keySet().iterator().next();
    assertEquals("(String,String...)", mnd.getParameterDisplaySignature());
}
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) EnumTest(net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)

Example 39 with NameDeclaration

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

the class LocalScopeTest method testLocalVariableDeclarationFound.

@Test
public void testLocalVariableDeclarationFound() {
    parseCode(TEST1);
    List<ASTVariableDeclaratorId> nodes = acu.findDescendantsOfType(ASTVariableDeclaratorId.class);
    ASTVariableDeclaratorId node = nodes.get(0);
    Map<NameDeclaration, List<NameOccurrence>> vars = node.getScope().getDeclarations();
    assertEquals(1, vars.size());
    NameDeclaration decl = vars.keySet().iterator().next();
    assertEquals("b", decl.getImage());
}
Also used : ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Test(org.junit.Test)

Example 40 with NameDeclaration

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

the class ScopeAndDeclarationFinderTest method testJava8LambdaScoping.

/**
 * Unit test for https://sourceforge.net/p/pmd/bugs/1317/
 */
@Test
public void testJava8LambdaScoping() {
    String source = "public class MultipleLambdas {\n" + "  Observer a = (o, arg) -> System.out.println(\"a:\" + arg);\n" + "  Observer b = (o, arg) -> System.out.println(\"b:\" + arg);\n" + "}";
    parseCode(source, LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.8"));
    List<ASTLambdaExpression> lambdas = acu.findDescendantsOfType(ASTLambdaExpression.class);
    Assert.assertEquals(2, lambdas.size());
    LocalScope scope1 = (LocalScope) lambdas.get(0).getScope();
    LocalScope scope2 = (LocalScope) lambdas.get(1).getScope();
    Assert.assertNotSame(scope1, scope2);
    for (ASTLambdaExpression l : lambdas) {
        LocalScope scope = (LocalScope) l.getScope();
        Assert.assertEquals(2, scope.getVariableDeclarations().size());
        Assert.assertTrue(scope.contains(new JavaNameOccurrence(null, "o")));
        Assert.assertTrue(scope.contains(new JavaNameOccurrence(null, "arg")));
        Set<NameDeclaration> declarations = scope.findVariableHere(new JavaNameOccurrence(null, "arg"));
        Assert.assertEquals(1, declarations.size());
        NameDeclaration decl = declarations.iterator().next();
        Assert.assertEquals(1, scope.getVariableDeclarations().get(decl).size());
    }
}
Also used : NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) ASTLambdaExpression(net.sourceforge.pmd.lang.java.ast.ASTLambdaExpression) Test(org.junit.Test)

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