Search in sources :

Example 1 with ASTLambdaExpression

use of net.sourceforge.pmd.lang.java.ast.ASTLambdaExpression 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

ASTLambdaExpression (net.sourceforge.pmd.lang.java.ast.ASTLambdaExpression)1 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)1 Test (org.junit.Test)1