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());
}
}
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());
}
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());
}
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());
}
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());
}
}
Aggregations