Search in sources :

Example 6 with ASTVariableDeclaratorId

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

the class ImageFinderFunctionTest method testSingleImage.

@Test
public void testSingleImage() {
    ImageFinderFunction f = new ImageFinderFunction("foo");
    ASTVariableDeclaratorId node = new ASTVariableDeclaratorId(1);
    node.setImage("foo");
    NameDeclaration decl = new VariableNameDeclaration(node);
    f.applyTo(decl);
    assertEquals(decl, f.getDecl());
}
Also used : ImageFinderFunction(net.sourceforge.pmd.lang.symboltable.ImageFinderFunction) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Test(org.junit.Test)

Example 7 with ASTVariableDeclaratorId

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

the class LocalScopeTest method testPostfixUsageIsRecorded.

@Test
public void testPostfixUsageIsRecorded() {
    parseCode(TEST3);
    List<ASTVariableDeclaratorId> nodes = acu.findDescendantsOfType(ASTVariableDeclaratorId.class);
    ASTVariableDeclaratorId node = nodes.get(0);
    Map<NameDeclaration, List<NameOccurrence>> vars = node.getScope().getDeclarations();
    NameDeclaration decl = vars.keySet().iterator().next();
    List<NameOccurrence> usages = vars.get(decl);
    JavaNameOccurrence occ = (JavaNameOccurrence) usages.get(0);
    assertEquals(4, occ.getLocation().getBeginLine());
}
Also used : ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) Test(org.junit.Test)

Example 8 with ASTVariableDeclaratorId

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

the class LocalScopeTest method testQualifiedNameOccurrence.

@Test
public void testQualifiedNameOccurrence() {
    parseCode(TEST2);
    List<ASTVariableDeclaratorId> nodes = acu.findDescendantsOfType(ASTVariableDeclaratorId.class);
    ASTVariableDeclaratorId node = nodes.get(0);
    Map<NameDeclaration, List<NameOccurrence>> vars = node.getScope().getDeclarations();
    NameDeclaration decl = vars.keySet().iterator().next();
    JavaNameOccurrence occ = (JavaNameOccurrence) vars.get(decl).get(0);
    assertEquals("b", occ.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 9 with ASTVariableDeclaratorId

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

the class AcceptanceTest method testEnum.

@Test
public void testEnum() {
    parseCode(NameOccurrencesTest.TEST_ENUM);
    ASTVariableDeclaratorId vdi = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0);
    List<NameOccurrence> usages = vdi.getUsages();
    assertEquals(2, usages.size());
    assertEquals(5, usages.get(0).getLocation().getBeginLine());
    assertEquals(9, 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)

Example 10 with ASTVariableDeclaratorId

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

the class CloseResourceRule method checkForResources.

private void checkForResources(Node node, Object data) {
    List<ASTLocalVariableDeclaration> vars = node.findDescendantsOfType(ASTLocalVariableDeclaration.class);
    List<ASTVariableDeclaratorId> ids = new ArrayList<>();
    // find all variable references to Connection objects
    for (ASTLocalVariableDeclaration var : vars) {
        ASTType type = var.getTypeNode();
        if (type.jjtGetChild(0) instanceof ASTReferenceType) {
            ASTReferenceType ref = (ASTReferenceType) type.jjtGetChild(0);
            if (ref.jjtGetChild(0) instanceof ASTClassOrInterfaceType) {
                ASTClassOrInterfaceType clazz = (ASTClassOrInterfaceType) ref.jjtGetChild(0);
                if (clazz.getType() != null && types.contains(clazz.getType().getName()) || clazz.getType() == null && simpleTypes.contains(toSimpleType(clazz.getImage())) && !clazz.isReferenceToClassSameCompilationUnit() || types.contains(clazz.getImage()) && !clazz.isReferenceToClassSameCompilationUnit()) {
                    ASTVariableDeclaratorId id = var.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
                    ids.add(id);
                }
            }
        }
    }
    // if there are connections, ensure each is closed.
    for (ASTVariableDeclaratorId x : ids) {
        ensureClosed((ASTLocalVariableDeclaration) x.jjtGetParent().jjtGetParent(), x, data);
    }
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTLocalVariableDeclaration(net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration) ArrayList(java.util.ArrayList) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

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