Search in sources :

Example 26 with NameDeclaration

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);
}
Also used : Scope(net.sourceforge.pmd.lang.symboltable.Scope) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration)

Example 27 with NameDeclaration

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);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 28 with NameDeclaration

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());
}
Also used : NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Test(org.junit.Test)

Example 29 with NameDeclaration

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());
}
Also used : NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Test(org.junit.Test)

Example 30 with NameDeclaration

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());
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Test(org.junit.Test) EnumTest(net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)

Aggregations

NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)49 List (java.util.List)28 Test (org.junit.Test)28 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)13 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)12 EnumTest (net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)12 Map (java.util.Map)10 Node (net.sourceforge.pmd.lang.ast.Node)10 Scope (net.sourceforge.pmd.lang.symboltable.Scope)8 ArrayList (java.util.ArrayList)7 HashSet (java.util.HashSet)6 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)6 ImageFinderFunction (net.sourceforge.pmd.lang.symboltable.ImageFinderFunction)6 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)5 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)5 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)4 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)3 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)3 ASTName (net.sourceforge.pmd.lang.plsql.ast.ASTName)3 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)2