Search in sources :

Example 26 with ASTVariableDeclaratorId

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

use of net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId in project pmd by pmd.

the class AcceptanceTest method testFieldFinder.

@Test
public void testFieldFinder() {
    parseCode(TEST_FIELD);
    // System.out.println(TEST_FIELD);
    ASTVariableDeclaratorId declaration = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(1);
    assertEquals(3, declaration.getBeginLine());
    assertEquals("bbbbbbbbbb", declaration.getImage());
    assertEquals(1, declaration.getUsages().size());
    NameOccurrence no = declaration.getUsages().get(0);
    Node location = no.getLocation();
    assertEquals(6, location.getBeginLine());
// System.out.println("variable " + declaration.getImage() + " is used
// here: " + location.getImage());
}
Also used : ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) Node(net.sourceforge.pmd.lang.ast.Node) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) Test(org.junit.Test)

Example 28 with ASTVariableDeclaratorId

use of net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId in project pmd by pmd.

the class AcceptanceTest method testInnerOuterClass.

@Test
public void testInnerOuterClass() {
    parseCode(TEST_INNER_CLASS);
    ASTVariableDeclaratorId vdi = // get inner class
    acu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class).get(1).getFirstDescendantOfType(// get first declaration
    ASTVariableDeclaratorId.class);
    List<NameOccurrence> usages = vdi.getUsages();
    assertEquals(2, usages.size());
    assertEquals(5, usages.get(0).getLocation().getBeginLine());
    assertEquals(10, usages.get(1).getLocation().getBeginLine());
}
Also used : ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) Test(org.junit.Test)

Aggregations

ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)28 Test (org.junit.Test)13 Node (net.sourceforge.pmd.lang.ast.Node)10 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)9 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)6 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)6 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)5 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)5 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)4 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)4 List (java.util.List)3 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)3 ASTVariableDeclarator (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator)3 ArrayList (java.util.ArrayList)2 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)2 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)2 ASTFormalParameters (net.sourceforge.pmd.lang.java.ast.ASTFormalParameters)2 ASTLocalVariableDeclaration (net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration)2 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)2 ASTMethodDeclarator (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator)2