use of net.sourceforge.pmd.lang.symboltable.NameOccurrence 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.symboltable.NameOccurrence in project pmd by pmd.
the class MethodScopeTest method testMethodParameterOccurrenceRecorded.
@Test
public void testMethodParameterOccurrenceRecorded() {
parseCode(TEST1);
Map<NameDeclaration, List<NameOccurrence>> m = acu.findDescendantsOfType(ASTMethodDeclaration.class).get(0).getScope().getDeclarations();
NameDeclaration vnd = m.keySet().iterator().next();
assertEquals("bar", vnd.getImage());
List<NameOccurrence> occs = m.get(vnd);
NameOccurrence occ = occs.get(0);
assertEquals(3, occ.getLocation().getBeginLine());
}
use of net.sourceforge.pmd.lang.symboltable.NameOccurrence 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.symboltable.NameOccurrence in project pmd by pmd.
the class VariableAccessVisitor method markUsages.
private List<VariableAccess> markUsages(DataFlowNode inode) {
// undefinitions was once a field... seems like it works fine as a local
List<VariableAccess> undefinitions = new ArrayList<>();
Set<Map<VariableNameDeclaration, List<NameOccurrence>>> variableDeclarations = collectDeclarations(inode);
for (Map<VariableNameDeclaration, List<NameOccurrence>> declarations : variableDeclarations) {
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : declarations.entrySet()) {
VariableNameDeclaration vnd = entry.getKey();
if (vnd.getAccessNodeParent() instanceof ASTFormalParameter) {
// no definition/undefinition/references for parameters
continue;
} else if (vnd.getAccessNodeParent().getFirstDescendantOfType(ASTVariableInitializer.class) != null) {
// add definition for initialized variables
addVariableAccess(vnd.getNode(), new VariableAccess(VariableAccess.DEFINITION, vnd.getImage()), inode.getFlow());
}
undefinitions.add(new VariableAccess(VariableAccess.UNDEFINITION, vnd.getImage()));
for (NameOccurrence occurrence : entry.getValue()) {
addAccess((JavaNameOccurrence) occurrence, inode);
}
}
}
return undefinitions;
}
use of net.sourceforge.pmd.lang.symboltable.NameOccurrence in project pmd by pmd.
the class CheckSkipResultRule method visit.
@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
ASTType typeNode = node.getTypeNode();
if (typeNode == null || !TypeHelper.isA(typeNode, InputStream.class)) {
return data;
}
for (NameOccurrence occ : node.getUsages()) {
JavaNameOccurrence jocc = (JavaNameOccurrence) occ;
NameOccurrence qualifier = jocc.getNameForWhichThisIsAQualifier();
if (qualifier != null && "skip".equals(qualifier.getImage())) {
Node loc = jocc.getLocation();
if (loc != null) {
ASTPrimaryExpression exp = loc.getFirstParentOfType(ASTPrimaryExpression.class);
while (exp != null) {
if (exp.jjtGetParent() instanceof ASTStatementExpression) {
// if exp is in a bare statement,
// the returned value is not used
addViolation(data, occ.getLocation());
break;
} else if (exp.jjtGetParent() instanceof ASTExpression && exp.jjtGetParent().jjtGetParent() instanceof ASTPrimaryPrefix) {
// if exp is enclosed in a pair of parenthesis
// let's have a look at the enclosing expression
// we'll see if it's in a bare statement
exp = exp.getFirstParentOfType(ASTPrimaryExpression.class);
} else {
// or assignement so the returned value is used
break;
}
}
}
}
}
return data;
}
Aggregations