Search in sources :

Example 1 with Scope

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

the class VariableNameDeclarationTest method testConstructor.

@Test
public void testConstructor() {
    parseCode(TEST1);
    List<ASTVariableDeclaratorId> nodes = acu.findDescendantsOfType(ASTVariableDeclaratorId.class);
    Scope s = nodes.get(0).getScope();
    NameDeclaration decl = s.getDeclarations().keySet().iterator().next();
    assertEquals("bar", decl.getImage());
    assertEquals(3, decl.getNode().getBeginLine());
}
Also used : Scope(net.sourceforge.pmd.lang.symboltable.Scope) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Test(org.junit.Test)

Example 2 with Scope

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

the class GlobalScopeTest method testClassDeclAppears.

@Test
public void testClassDeclAppears() {
    parseCode(TEST1);
    ASTCompilationUnit decl = acu;
    Scope scope = decl.getScope();
    Map<NameDeclaration, List<NameOccurrence>> m = scope.getDeclarations();
    ClassNameDeclaration classNameDeclaration = (ClassNameDeclaration) m.keySet().iterator().next();
    assertEquals(classNameDeclaration.getImage(), "Foo");
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Scope(net.sourceforge.pmd.lang.symboltable.Scope) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Test(org.junit.Test)

Example 3 with Scope

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

the class ScopeAndDeclarationFinder method visit.

@Override
public Object visit(ASTPackageBody node, Object data) {
    createClassScope(node);
    Scope s = ((PLSQLNode) node.jjtGetParent()).getScope();
    s.addDeclaration(new ClassNameDeclaration(node));
    cont(node);
    return data;
}
Also used : Scope(net.sourceforge.pmd.lang.symboltable.Scope) PLSQLNode(net.sourceforge.pmd.lang.plsql.ast.PLSQLNode)

Example 4 with Scope

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

the class ClassScope method determineArgumentTypes.

/**
 * Provide a list of types of the arguments of the given method call. The
 * types are simple type images. If the argument type cannot be determined
 * (e.g. because it is itself the result of a method call), the parameter
 * type is used - so it is assumed, it is of the correct type. This might
 * cause confusion when methods are overloaded.
 *
 * @param occurrence
 *            the method call
 * @param parameterTypes
 *            the parameter types of the called method
 * @return the list of argument types
 */
private List<TypedNameDeclaration> determineArgumentTypes(JavaNameOccurrence occurrence, List<TypedNameDeclaration> parameterTypes) {
    ASTArgumentList arguments = null;
    Node nextSibling;
    if (occurrence.getLocation() instanceof ASTPrimarySuffix) {
        nextSibling = getNextSibling(occurrence.getLocation());
    } else {
        nextSibling = getNextSibling(occurrence.getLocation().jjtGetParent());
    }
    if (nextSibling != null) {
        arguments = nextSibling.getFirstDescendantOfType(ASTArgumentList.class);
    }
    if (arguments == null) {
        return Collections.emptyList();
    }
    List<TypedNameDeclaration> argumentTypes = new ArrayList<>(arguments.jjtGetNumChildren());
    Map<String, Node> qualifiedTypeNames = getEnclosingScope(SourceFileScope.class).getQualifiedTypeNames();
    for (int i = 0; i < arguments.jjtGetNumChildren(); i++) {
        Node argument = arguments.jjtGetChild(i);
        Node child = null;
        boolean isMethodCall = false;
        if (argument.jjtGetNumChildren() > 0 && argument.jjtGetChild(0).jjtGetNumChildren() > 0 && argument.jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() > 0) {
            child = argument.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
            isMethodCall = argument.jjtGetChild(0).jjtGetNumChildren() > 1;
        }
        TypedNameDeclaration type = null;
        if (child instanceof ASTName && !isMethodCall) {
            ASTName name = (ASTName) child;
            Scope s = name.getScope();
            final JavaNameOccurrence nameOccurrence = new JavaNameOccurrence(name, name.getImage());
            while (s != null) {
                if (s.contains(nameOccurrence)) {
                    break;
                }
                s = s.getParent();
            }
            if (s != null) {
                Map<VariableNameDeclaration, List<NameOccurrence>> vars = s.getDeclarations(VariableNameDeclaration.class);
                for (VariableNameDeclaration d : vars.keySet()) {
                    // might be unknown
                    if (d.getImage().equals(name.getImage()) && d.getTypeImage() != null) {
                        String typeName = d.getTypeImage();
                        typeName = qualifyTypeName(typeName);
                        Node declaringNode = qualifiedTypeNames.get(typeName);
                        type = new SimpleTypedNameDeclaration(typeName, this.getEnclosingScope(SourceFileScope.class).resolveType(typeName), determineSuper(declaringNode));
                        break;
                    }
                }
            }
        } else if (child instanceof ASTLiteral) {
            ASTLiteral literal = (ASTLiteral) child;
            if (literal.isCharLiteral()) {
                type = new SimpleTypedNameDeclaration("char", literal.getType());
            } else if (literal.isStringLiteral()) {
                type = new SimpleTypedNameDeclaration("String", literal.getType());
            } else if (literal.isFloatLiteral()) {
                type = new SimpleTypedNameDeclaration("float", literal.getType());
            } else if (literal.isDoubleLiteral()) {
                type = new SimpleTypedNameDeclaration("double", literal.getType());
            } else if (literal.isIntLiteral()) {
                type = new SimpleTypedNameDeclaration("int", literal.getType());
            } else if (literal.isLongLiteral()) {
                type = new SimpleTypedNameDeclaration("long", literal.getType());
            } else if (literal.jjtGetNumChildren() == 1 && literal.jjtGetChild(0) instanceof ASTBooleanLiteral) {
                type = new SimpleTypedNameDeclaration("boolean", Boolean.TYPE);
            }
        } else if (child instanceof ASTAllocationExpression && child.jjtGetChild(0) instanceof ASTClassOrInterfaceType) {
            ASTClassOrInterfaceType classInterface = (ASTClassOrInterfaceType) child.jjtGetChild(0);
            type = convertToSimpleType(classInterface);
        }
        if (type == null && !parameterTypes.isEmpty()) {
            // arguments than parameterTypes
            if (parameterTypes.size() > i) {
                type = parameterTypes.get(i);
            } else {
                // last parameter is the vararg type
                type = parameterTypes.get(parameterTypes.size() - 1);
            }
        }
        if (type != null && type.getType() == null) {
            Class<?> typeBound = resolveGenericType(argument, type.getTypeImage());
            if (typeBound != null) {
                type = new SimpleTypedNameDeclaration(type.getTypeImage(), typeBound);
            }
        }
        argumentTypes.add(type);
    }
    return argumentTypes;
}
Also used : ASTBooleanLiteral(net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral) Node(net.sourceforge.pmd.lang.ast.Node) ArrayList(java.util.ArrayList) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) Scope(net.sourceforge.pmd.lang.symboltable.Scope) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) 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) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 5 with Scope

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

the class ScopeAndDeclarationFinder method visit.

@Override
public Object visit(ASTConstructorDeclaration node, Object data) {
    /*
         * Local variables declared inside the constructor need to be in a
         * different scope so special handling is needed
         */
    createMethodScope(node);
    Scope methodScope = node.getScope();
    Node formalParameters = node.jjtGetChild(0);
    int i = 1;
    int n = node.jjtGetNumChildren();
    if (!(formalParameters instanceof ASTFormalParameters)) {
        visit((ASTTypeParameters) formalParameters, data);
        formalParameters = node.jjtGetChild(1);
        i++;
    }
    visit((ASTFormalParameters) formalParameters, data);
    Scope localScope = null;
    for (; i < n; i++) {
        JavaNode b = (JavaNode) node.jjtGetChild(i);
        if (b instanceof ASTBlockStatement) {
            if (localScope == null) {
                createLocalScope(node);
                localScope = node.getScope();
            }
            b.setScope(localScope);
            visit(b, data);
        } else {
            visit(b, data);
        }
    }
    if (localScope != null) {
        // pop the local scope
        scopes.pop();
        // reset the correct scope for the constructor
        node.setScope(methodScope);
    }
    // pop the method scope
    scopes.pop();
    return data;
}
Also used : Scope(net.sourceforge.pmd.lang.symboltable.Scope) Node(net.sourceforge.pmd.lang.ast.Node) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTFormalParameters(net.sourceforge.pmd.lang.java.ast.ASTFormalParameters) 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