Search in sources :

Example 1 with ClassScope

use of net.sourceforge.pmd.lang.java.symboltable.ClassScope in project pmd by pmd.

the class ClassTypeResolver method getTypeDefinitionOfVariableFromScope.

/**
 * Search for a field by it's image stating from a scope and taking into account if it's visible from the
 * accessingClass Class. The method takes into account that Nested inherited fields shadow outer scope fields.
 *
 * @param scope          The scope to start the search from.
 * @param image          The name of the field, local variable or method parameter.
 * @param accessingClass The Class (which is defined in the current ACU) that is trying to access the field.
 * @return Type def. of the field, or null if it could not be resolved.
 */
private JavaTypeDefinition getTypeDefinitionOfVariableFromScope(Scope scope, String image, Class<?> accessingClass) {
    if (accessingClass == null) {
        return null;
    }
    for (; /* empty */
    scope != null; scope = scope.getParent()) {
        // search each enclosing scope one by one
        for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : scope.getDeclarations(VariableNameDeclaration.class).entrySet()) {
            if (entry.getKey().getImage().equals(image)) {
                ASTType typeNode = entry.getKey().getDeclaratorId().getTypeNode();
                if (typeNode == null) {
                    // TODO : Type is infered, ie, this is a lambda such as (var) -> var.equals(other)
                    return null;
                }
                if (typeNode.jjtGetChild(0) instanceof ASTReferenceType) {
                    return ((TypeNode) typeNode.jjtGetChild(0)).getTypeDefinition();
                } else {
                    // primitive type
                    return JavaTypeDefinition.forClass(typeNode.getType());
                }
            }
        }
        // Nested class' inherited fields shadow enclosing variables
        if (scope instanceof ClassScope) {
            try {
                // get the superclass type def. ot the Class the ClassScope belongs to
                JavaTypeDefinition superClass = getSuperClassTypeDefinition(((ClassScope) scope).getClassDeclaration().getNode(), null);
                // TODO: check if anonymous classes are class scope
                // try searching this type def.
                JavaTypeDefinition foundTypeDef = getFieldType(superClass, image, accessingClass);
                if (foundTypeDef != null) {
                    // if null, then it's not an inherited field
                    return foundTypeDef;
                }
            } catch (ClassCastException ignored) {
            // if there is an anonymous class, getClassDeclaration().getType() will throw
            // TODO: maybe there is a better way to handle this, maybe this hides bugs
            }
        }
    }
    // will return null if not found
    return searchImportedStaticFields(image);
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) ASTExtendsList(net.sourceforge.pmd.lang.java.ast.ASTExtendsList) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) List(java.util.List) ArrayList(java.util.ArrayList) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) Map(java.util.Map) HashMap(java.util.HashMap) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType) ClassScope(net.sourceforge.pmd.lang.java.symboltable.ClassScope)

Example 2 with ClassScope

use of net.sourceforge.pmd.lang.java.symboltable.ClassScope in project pmd by pmd.

the class UseCollectionIsEmptyRule method getTypeOfMethodCall.

private ASTClassOrInterfaceType getTypeOfMethodCall(ASTPrimarySuffix node) {
    ASTClassOrInterfaceType type = null;
    ASTName methodName = node.jjtGetParent().getFirstChildOfType(ASTPrimaryPrefix.class).getFirstChildOfType(ASTName.class);
    if (methodName != null) {
        ClassScope classScope = node.getScope().getEnclosingScope(ClassScope.class);
        Map<MethodNameDeclaration, List<NameOccurrence>> methods = classScope.getMethodDeclarations();
        for (Map.Entry<MethodNameDeclaration, List<NameOccurrence>> e : methods.entrySet()) {
            if (e.getKey().getName().equals(methodName.getImage())) {
                type = e.getKey().getNode().getFirstParentOfType(ASTMethodDeclaration.class).getFirstChildOfType(ASTResultType.class).getFirstDescendantOfType(ASTClassOrInterfaceType.class);
                break;
            }
        }
    }
    return type;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) MethodNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) List(java.util.List) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) HashMap(java.util.HashMap) Map(java.util.Map) ClassScope(net.sourceforge.pmd.lang.java.symboltable.ClassScope)

Example 3 with ClassScope

use of net.sourceforge.pmd.lang.java.symboltable.ClassScope in project pmd by pmd.

the class AccessorMethodGenerationRule method analyzeScope.

private void analyzeScope(final AbstractJavaScope file, final Object data) {
    for (final ClassNameDeclaration classDecl : file.getDeclarations(ClassNameDeclaration.class).keySet()) {
        final ClassScope classScope = (ClassScope) classDecl.getScope();
        // Check fields
        for (final Map.Entry<VariableNameDeclaration, List<NameOccurrence>> varDecl : classScope.getVariableDeclarations().entrySet()) {
            final ASTFieldDeclaration field = varDecl.getKey().getNode().getFirstParentOfType(ASTFieldDeclaration.class);
            analyzeMember(field, varDecl.getValue(), classScope, data);
        }
        // Check methods
        for (final Map.Entry<MethodNameDeclaration, List<NameOccurrence>> methodDecl : classScope.getMethodDeclarations().entrySet()) {
            final ASTMethodDeclaration method = methodDecl.getKey().getNode().getFirstParentOfType(ASTMethodDeclaration.class);
            analyzeMember(method, methodDecl.getValue(), classScope, data);
        }
        // Check inner classes
        analyzeScope(classScope, data);
    }
}
Also used : MethodNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration) ClassNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.ClassNameDeclaration) List(java.util.List) Map(java.util.Map) ClassScope(net.sourceforge.pmd.lang.java.symboltable.ClassScope)

Example 4 with ClassScope

use of net.sourceforge.pmd.lang.java.symboltable.ClassScope in project pmd by pmd.

the class CouplingBetweenObjectsRule method checkVariableType.

/**
 * performs a check on the variable and updates the counter. Counter is
 * instance for a class and is reset upon new class scan.
 *
 * @param variableType
 *            The variable type.
 */
private void checkVariableType(Node nameNode, String variableType) {
    // TODO - move this into the symbol table somehow?
    if (nameNode.getParentsOfType(ASTClassOrInterfaceDeclaration.class).isEmpty()) {
        return;
    }
    // if the field is of any type other than the class type
    // increment the count
    ClassScope clzScope = ((JavaNode) nameNode).getScope().getEnclosingScope(ClassScope.class);
    if (!clzScope.getClassName().equals(variableType) && !this.filterTypes(variableType) && !this.typesFoundSoFar.contains(variableType)) {
        couplingCount++;
        typesFoundSoFar.add(variableType);
    }
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ClassScope(net.sourceforge.pmd.lang.java.symboltable.ClassScope)

Example 5 with ClassScope

use of net.sourceforge.pmd.lang.java.symboltable.ClassScope in project pmd by pmd.

the class AccessorClassGenerationRule method visit.

@Override
public Object visit(final ASTAllocationExpression node, final Object data) {
    if (node.jjtGetChild(0) instanceof ASTClassOrInterfaceType) {
        // Ignore primitives
        final ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) node.jjtGetChild(0);
        final List<ASTConstructorDeclaration> constructors = privateConstructors.get(type.getImage());
        if (constructors != null) {
            final ASTArguments callArguments = node.getFirstChildOfType(ASTArguments.class);
            // Is this really a constructor call and not an array?
            if (callArguments != null) {
                final ClassScope enclosingScope = node.getScope().getEnclosingScope(ClassScope.class);
                for (final ASTConstructorDeclaration cd : constructors) {
                    // Are we within the same class scope?
                    if (cd.getScope().getEnclosingScope(ClassScope.class) == enclosingScope) {
                        break;
                    }
                    if (cd.getParameterCount() == callArguments.getArgumentCount()) {
                        // TODO : Check types
                        addViolation(data, node);
                        break;
                    }
                }
            }
        }
    }
    return data;
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTArguments(net.sourceforge.pmd.lang.java.ast.ASTArguments) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ClassScope(net.sourceforge.pmd.lang.java.symboltable.ClassScope)

Aggregations

ClassScope (net.sourceforge.pmd.lang.java.symboltable.ClassScope)5 List (java.util.List)3 Map (java.util.Map)3 HashMap (java.util.HashMap)2 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)2 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)2 MethodNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration)2 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)2 ArrayList (java.util.ArrayList)1 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)1 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)1 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)1 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)1 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)1 ASTFieldDeclaration (net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration)1 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)1 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)1 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)1 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)1 AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)1