Search in sources :

Example 6 with Scope

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

the class UnnecessaryLocalBeforeReturnRule method isInitDataModifiedAfterInit.

private boolean isInitDataModifiedAfterInit(final VariableNameDeclaration variableDeclaration, final ASTReturnStatement rtn) {
    final ASTVariableInitializer initializer = variableDeclaration.getAccessNodeParent().getFirstDescendantOfType(ASTVariableInitializer.class);
    if (initializer != null) {
        final List<ASTName> referencedNames = initializer.findDescendantsOfType(ASTName.class);
        for (final ASTName refName : referencedNames) {
            // TODO : Shouldn't the scope allow us to search for a var name occurrences directly, moving up through parent scopes?
            Scope scope = refName.getScope();
            do {
                final Map<VariableNameDeclaration, List<NameOccurrence>> declarations = scope.getDeclarations(VariableNameDeclaration.class);
                for (final Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : declarations.entrySet()) {
                    if (entry.getKey().getName().equals(refName.getImage())) {
                        // Variable found! Check usage locations
                        for (final NameOccurrence occ : entry.getValue()) {
                            final ScopedNode location = occ.getLocation();
                            // Is it used after initializing our "unnecessary" local but before the return statement?
                            if (isAfter(location, initializer) && isAfter(rtn, location)) {
                                return true;
                            }
                        }
                        return false;
                    }
                }
                scope = scope.getParent();
            } while (scope != null);
        }
    }
    return false;
}
Also used : ScopedNode(net.sourceforge.pmd.lang.symboltable.ScopedNode) Scope(net.sourceforge.pmd.lang.symboltable.Scope) ASTVariableInitializer(net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) List(java.util.List) Map(java.util.Map) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 7 with Scope

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

the class JUnitTestsShouldIncludeAssertRule method visit.

@Override
public Object visit(ASTMethodDeclaration method, Object data) {
    if (isJUnitMethod(method, data)) {
        if (!isExpectAnnotated(method.jjtGetParent())) {
            Scope classScope = method.getScope().getParent();
            Map<String, List<NameOccurrence>> expectables = getRuleAnnotatedExpectedExceptions(classScope);
            if (!containsExpectOrAssert(method.getBlock(), expectables)) {
                addViolation(data, method);
            }
        }
    }
    return data;
}
Also used : Scope(net.sourceforge.pmd.lang.symboltable.Scope) List(java.util.List)

Example 8 with Scope

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

the class ClassScope method addNameOccurrence.

public Set<NameDeclaration> addNameOccurrence(NameOccurrence occurrence) {
    JavaNameOccurrence javaOccurrence = (JavaNameOccurrence) occurrence;
    Set<NameDeclaration> declarations = findVariableHere(javaOccurrence);
    if (!declarations.isEmpty() && (javaOccurrence.isMethodOrConstructorInvocation() || javaOccurrence.isMethodReference())) {
        for (NameDeclaration decl : declarations) {
            List<NameOccurrence> nameOccurrences = getMethodDeclarations().get(decl);
            if (nameOccurrences == null) {
                // search inner classes
                for (ClassNameDeclaration innerClass : getClassDeclarations().keySet()) {
                    Scope innerClassScope = innerClass.getScope();
                    if (innerClassScope.contains(javaOccurrence)) {
                        innerClassScope.addNameOccurrence(javaOccurrence);
                    }
                }
            } else {
                nameOccurrences.add(javaOccurrence);
                Node n = javaOccurrence.getLocation();
                if (n instanceof ASTName) {
                    ((ASTName) n).setNameDeclaration(decl);
                }
            // TODO what to do with PrimarySuffix case?
            }
        }
    } else if (!declarations.isEmpty() && !javaOccurrence.isThisOrSuper()) {
        for (NameDeclaration decl : declarations) {
            List<NameOccurrence> nameOccurrences = getVariableDeclarations().get(decl);
            if (nameOccurrences == null) {
                // search inner classes
                for (ClassNameDeclaration innerClass : getClassDeclarations().keySet()) {
                    Scope innerClassScope = innerClass.getScope();
                    if (innerClassScope.contains(javaOccurrence)) {
                        innerClassScope.addNameOccurrence(javaOccurrence);
                    }
                }
            } else {
                nameOccurrences.add(javaOccurrence);
                Node n = javaOccurrence.getLocation();
                if (n instanceof ASTName) {
                    ((ASTName) n).setNameDeclaration(decl);
                }
            // TODO what to do with PrimarySuffix case?
            }
        }
    }
    return declarations;
}
Also used : Scope(net.sourceforge.pmd.lang.symboltable.Scope) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTImplementsList(net.sourceforge.pmd.lang.java.ast.ASTImplementsList) ArrayList(java.util.ArrayList) ASTExtendsList(net.sourceforge.pmd.lang.java.ast.ASTExtendsList) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 9 with Scope

use of net.sourceforge.pmd.lang.symboltable.Scope 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 10 with Scope

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

the class ScopeAndDeclarationFinder method createClassScope.

/**
 * Creates a new class scope for an AST node. The scope on top of the stack
 * is set as the parent of the new scope, which is then also stored on the
 * scope stack.
 *
 * @param node
 *            the AST node for which the scope has to be created.
 * @throws java.util.EmptyStackException
 *             if the scope stack is empty.
 */
private void createClassScope(JavaNode node) {
    Scope s = ((JavaNode) node.jjtGetParent()).getScope();
    ClassNameDeclaration classNameDeclaration = new ClassNameDeclaration(node);
    s.addDeclaration(classNameDeclaration);
    if (node instanceof ASTClassOrInterfaceBody) {
        addScope(new ClassScope(classNameDeclaration), node);
    } else {
        addScope(new ClassScope(node.getImage(), classNameDeclaration), node);
    }
}
Also used : Scope(net.sourceforge.pmd.lang.symboltable.Scope) ASTClassOrInterfaceBody(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBody) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode)

Aggregations

Scope (net.sourceforge.pmd.lang.symboltable.Scope)19 List (java.util.List)9 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)8 Test (org.junit.Test)5 Map (java.util.Map)4 PLSQLNode (net.sourceforge.pmd.lang.plsql.ast.PLSQLNode)4 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)4 Node (net.sourceforge.pmd.lang.ast.Node)3 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)3 ArrayList (java.util.ArrayList)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)2 ASTImplementsList (net.sourceforge.pmd.lang.java.ast.ASTImplementsList)2 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)2 AbstractJavaNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaNode)2 JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)2 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)2 HashSet (java.util.HashSet)1 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)1 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)1