use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.
the class MethodNameDeclarationTest method testEquality.
@Test
public void testEquality() {
// Verify proper number of nodes are not equal
parseCode15(SIMILAR);
ASTClassOrInterfaceDeclaration n = acu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class).get(0);
Map<NameDeclaration, List<NameOccurrence>> m = ((ClassScope) n.getScope()).getDeclarations();
Set<NameDeclaration> methodNameDeclarations = m.keySet();
assertEquals("Wrong number of method name declarations", methodNameDeclarations.size(), 3);
}
use of net.sourceforge.pmd.lang.symboltable.NameDeclaration 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.NameDeclaration in project pmd by pmd.
the class StringInstantiationRule method visit.
@Override
public Object visit(ASTAllocationExpression node, Object data) {
if (!(node.jjtGetChild(0) instanceof ASTClassOrInterfaceType)) {
return data;
}
if (!TypeHelper.isA((ASTClassOrInterfaceType) node.jjtGetChild(0), String.class)) {
return data;
}
List<ASTExpression> exp = node.findDescendantsOfType(ASTExpression.class);
if (exp.size() >= 2) {
return data;
}
if (node.hasDescendantOfAnyType(ASTArrayDimsAndInits.class, ASTAdditiveExpression.class)) {
return data;
}
ASTName name = node.getFirstDescendantOfType(ASTName.class);
// Literal, i.e., new String("foo")
if (name == null) {
addViolation(data, node);
return data;
}
NameDeclaration nd = name.getNameDeclaration();
if (nd == null) {
return data;
}
if (nd instanceof TypedNameDeclaration && TypeHelper.isA((TypedNameDeclaration) nd, String.class)) {
addViolation(data, node);
}
return data;
}
use of net.sourceforge.pmd.lang.symboltable.NameDeclaration 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<NameDeclaration, List<NameOccurrence>>> variableDeclarations = collectDeclarations(inode);
for (Map<NameDeclaration, List<NameOccurrence>> declarations : variableDeclarations) {
for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : declarations.entrySet()) {
NameDeclaration vnd = entry.getKey();
if (vnd.getNode().jjtGetParent() instanceof ASTFormalParameter) {
// no definition/undefinition/references for parameters
continue;
} else if (vnd.getNode().jjtGetParent().getFirstDescendantOfType(ASTVariableOrConstantInitializer.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(occurrence, inode);
}
}
}
return undefinitions;
}
use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.
the class ClassScope method findVariableHere.
protected Set<NameDeclaration> findVariableHere(PLSQLNameOccurrence occurrence) {
Set<NameDeclaration> result = new HashSet<>();
Map<VariableNameDeclaration, List<NameOccurrence>> variableDeclarations = getVariableDeclarations();
Map<MethodNameDeclaration, List<NameOccurrence>> methodDeclarations = getMethodDeclarations();
if (occurrence.isThisOrSuper() || occurrence.getImage().equals(className)) {
if (variableDeclarations.isEmpty() && methodDeclarations.isEmpty()) {
// }
return result;
}
// and then we'll look up X.
if (!variableDeclarations.isEmpty()) {
result.add(variableDeclarations.keySet().iterator().next());
return result;
}
result.add(methodDeclarations.keySet().iterator().next());
return result;
}
if (occurrence.isMethodOrConstructorInvocation()) {
for (MethodNameDeclaration mnd : methodDeclarations.keySet()) {
if (mnd.getImage().equals(occurrence.getImage())) {
int args = occurrence.getArgumentCount();
if (args == mnd.getParameterCount() || mnd.isVarargs() && args >= mnd.getParameterCount() - 1) {
// FIXME if several methods have the same name
// and parameter count, only one will get caught here
// we need to make some attempt at type lookup and
// discrimination
// or, failing that, mark this as a usage of all those
// methods
result.add(mnd);
}
}
}
return result;
}
List<String> images = new ArrayList<>();
images.add(occurrence.getImage());
if (null == occurrence.getImage()) {
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest("occurrence==" + occurrence.toString() + "with Argumanet Count == " + occurrence.getArgumentCount() + " for className=" + className);
}
}
if (occurrence.getImage().startsWith(className)) {
images.add(clipClassName(occurrence.getImage()));
}
ImageFinderFunction finder = new ImageFinderFunction(images);
Applier.apply(finder, getVariableDeclarations().keySet().iterator());
if (finder.getDecl() != null) {
result.add(finder.getDecl());
}
return result;
}
Aggregations