use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.
the class OccurrenceFinder method visit.
public Object visit(ASTPrimaryExpression node, Object data) {
NameFinder nameFinder = new NameFinder(node);
declarations.clear();
additionalDeclarations.clear();
List<JavaNameOccurrence> names = nameFinder.getNames();
for (JavaNameOccurrence occ : names) {
Search search = new Search(occ);
if (declarations.isEmpty()) {
// doing the first name lookup
search.execute();
declarations.addAll(search.getResult());
if (declarations.isEmpty()) {
// SymbolNotFoundException
break;
}
} else {
for (NameDeclaration decl : declarations) {
// now we've got a scope we're starting with, so work from
// there
Scope startingScope = decl.getScope();
// outerClassRef.member. See also bug #1302
if (decl instanceof VariableNameDeclaration) {
String typeImage = ((VariableNameDeclaration) decl).getTypeImage();
ClassNameDeclaration clazzDeclaration = startingScope.getEnclosingScope(SourceFileScope.class).findClassNameDeclaration(typeImage);
if (clazzDeclaration != null) {
startingScope = clazzDeclaration.getScope();
}
}
search.execute(startingScope);
Set<NameDeclaration> result = search.getResult();
additionalDeclarations.addAll(result);
if (result.isEmpty()) {
// SymbolNotFoundException
break;
}
}
declarations.addAll(additionalDeclarations);
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.
the class InvalidSlf4jMessageFormatRule method visit.
@Override
public Object visit(final ASTName node, final Object data) {
final NameDeclaration nameDeclaration = node.getNameDeclaration();
// ignore imports or methods
if (!(nameDeclaration instanceof VariableNameDeclaration)) {
return super.visit(node, data);
}
// ignore non slf4j logger
Class<?> type = ((VariableNameDeclaration) nameDeclaration).getType();
if (type == null || !type.getName().equals(LOGGER_CLASS)) {
return super.visit(node, data);
}
// get the node that contains the logger
final ASTPrimaryExpression parentNode = node.getFirstParentOfType(ASTPrimaryExpression.class);
// get the log level
final String method = parentNode.getFirstChildOfType(ASTPrimaryPrefix.class).getFirstChildOfType(ASTName.class).getImage().replace(nameDeclaration.getImage() + ".", "");
// ignore if not a log level
if (!LOGGER_LEVELS.contains(method)) {
return super.visit(node, data);
}
// find the arguments
final List<ASTExpression> argumentList = parentNode.getFirstChildOfType(ASTPrimarySuffix.class).getFirstDescendantOfType(ASTArgumentList.class).findChildrenOfType(ASTExpression.class);
// remove the message parameter
final ASTPrimaryExpression messageParam = argumentList.remove(0).getFirstDescendantOfType(ASTPrimaryExpression.class);
final int expectedArguments = expectedArguments(messageParam);
if (expectedArguments == 0) {
// or if we couldn't analyze the message parameter
return super.visit(node, data);
}
// But only, if it is not used as a placeholder argument
if (argumentList.size() > expectedArguments) {
removeThrowableParam(argumentList);
}
if (argumentList.size() < expectedArguments) {
addViolationWithMessage(data, node, "Missing arguments," + getExpectedMessage(argumentList, expectedArguments));
} else if (argumentList.size() > expectedArguments) {
addViolationWithMessage(data, node, "Too many arguments," + getExpectedMessage(argumentList, expectedArguments));
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.
the class VariableNameDeclarationTest method testParamTypeImage.
@Test
public void testParamTypeImage() {
parseCode(TEST5);
NameDeclaration decl = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0).getScope().getDeclarations().keySet().iterator().next();
assertEquals("String", ((TypedNameDeclaration) decl).getTypeImage());
}
use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.
the class VariableNameDeclarationTest method testRefTypeImage.
@Test
public void testRefTypeImage() {
parseCode(TEST4);
NameDeclaration decl = acu.findDescendantsOfType(ASTVariableDeclaratorId.class).get(0).getScope().getDeclarations().keySet().iterator().next();
assertEquals("String", ((TypedNameDeclaration) decl).getTypeImage());
}
use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.
the class ClassScopeTest method testMethodDeclarationRecorded.
@Test
public void testMethodDeclarationRecorded() {
parseCode(METHOD_DECLARATIONS_RECORDED);
ASTClassOrInterfaceDeclaration n = acu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class).get(0);
ClassScope s = (ClassScope) n.getScope();
Map<NameDeclaration, List<NameOccurrence>> m = s.getDeclarations();
assertEquals(1, m.size());
MethodNameDeclaration mnd = (MethodNameDeclaration) m.keySet().iterator().next();
assertEquals("bar", mnd.getImage());
ASTMethodDeclaration node = (ASTMethodDeclaration) mnd.getNode().jjtGetParent();
assertTrue(node.isPrivate());
}
Aggregations