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