Search in sources :

Example 46 with NameDeclaration

use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.

the class ClassScope method addNameOccurrence.

@Override
public Set<NameDeclaration> addNameOccurrence(NameOccurrence occ) {
    PLSQLNameOccurrence occurrence = (PLSQLNameOccurrence) occ;
    Set<NameDeclaration> declarations = findVariableHere(occurrence);
    Map<MethodNameDeclaration, List<NameOccurrence>> methodNames = getMethodDeclarations();
    if (!declarations.isEmpty() && occurrence.isMethodOrConstructorInvocation()) {
        for (NameDeclaration decl : declarations) {
            List<NameOccurrence> nameOccurrences = methodNames.get(decl);
            if (nameOccurrences == null) {
            // TODO may be a class name: Foo.this.super();
            } else {
                nameOccurrences.add(occurrence);
                Node n = occurrence.getLocation();
                if (n instanceof ASTName) {
                    ((ASTName) n).setNameDeclaration(decl);
                }
            // TODO what to do with PrimarySuffix case?
            }
        }
    } else if (!declarations.isEmpty() && !occurrence.isThisOrSuper()) {
        Map<VariableNameDeclaration, List<NameOccurrence>> variableNames = getVariableDeclarations();
        for (NameDeclaration decl : declarations) {
            List<NameOccurrence> nameOccurrences = variableNames.get(decl);
            if (nameOccurrences == null) {
            // TODO may be a class name
            } else {
                nameOccurrences.add(occurrence);
                Node n = occurrence.getLocation();
                if (n instanceof ASTName) {
                    ((ASTName) n).setNameDeclaration(decl);
                }
            // TODO what to do with PrimarySuffix case?
            }
        }
    }
    return declarations;
}
Also used : ASTName(net.sourceforge.pmd.lang.plsql.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) AbstractPLSQLNode(net.sourceforge.pmd.lang.plsql.ast.AbstractPLSQLNode) ArrayList(java.util.ArrayList) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Map(java.util.Map) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 47 with NameDeclaration

use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.

the class MethodScope method addNameOccurrence.

@Override
public Set<NameDeclaration> addNameOccurrence(NameOccurrence occ) {
    PLSQLNameOccurrence occurrence = (PLSQLNameOccurrence) occ;
    Set<NameDeclaration> declarations = findVariableHere(occurrence);
    if (!declarations.isEmpty() && !occurrence.isThisOrSuper()) {
        for (NameDeclaration decl : declarations) {
            getVariableDeclarations().get(decl).add(occurrence);
            Node n = occurrence.getLocation();
            if (n instanceof ASTName) {
                ((ASTName) n).setNameDeclaration(decl);
            }
        // TODO what to do with PrimarySuffix case?
        }
    }
    return declarations;
}
Also used : ASTName(net.sourceforge.pmd.lang.plsql.ast.ASTName) AbstractPLSQLNode(net.sourceforge.pmd.lang.plsql.ast.AbstractPLSQLNode) Node(net.sourceforge.pmd.lang.ast.Node) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration)

Example 48 with NameDeclaration

use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.

the class MethodScope method findVariableHere.

public Set<NameDeclaration> findVariableHere(PLSQLNameOccurrence occurrence) {
    Set<NameDeclaration> result = new HashSet<>();
    if (occurrence.isThisOrSuper() || occurrence.isMethodOrConstructorInvocation()) {
        return result;
    }
    ImageFinderFunction finder = new ImageFinderFunction(occurrence.getImage());
    Applier.apply(finder, getVariableDeclarations().keySet().iterator());
    if (finder.getDecl() != null) {
        result.add(finder.getDecl());
    }
    return result;
}
Also used : ImageFinderFunction(net.sourceforge.pmd.lang.symboltable.ImageFinderFunction) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) HashSet(java.util.HashSet)

Example 49 with NameDeclaration

use of net.sourceforge.pmd.lang.symboltable.NameDeclaration in project pmd by pmd.

the class OccurrenceFinder method visit.

@Override
public Object visit(ASTPrimaryExpression node, Object data) {
    NameFinder nameFinder = new NameFinder(node);
    // Maybe do some sort of State pattern thingy for when NameDeclaration
    // is empty/not empty
    Set<NameDeclaration> declarations = new HashSet<>();
    List<PLSQLNameOccurrence> names = nameFinder.getNames();
    for (PLSQLNameOccurrence 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 {
            Set<NameDeclaration> additionalDeclarations = new HashSet<>();
            for (NameDeclaration decl : declarations) {
                // now we've got a scope we're starting with, so work from
                // there
                Scope scope = decl.getScope();
                if (null == scope) {
                    if (LOGGER.isLoggable(Level.FINEST)) {
                        LOGGER.finest("NameOccurrence has no Scope:" + decl.getClass().getCanonicalName() + "=>" + decl.getImage());
                    }
                    break;
                }
                search.execute(scope);
                Set<NameDeclaration> found = search.getResult();
                additionalDeclarations.addAll(found);
                if (found.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) HashSet(java.util.HashSet)

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