Search in sources :

Example 1 with TypeNode

use of net.sourceforge.pmd.lang.java.ast.TypeNode in project pmd by pmd.

the class ClassTypeResolverTest method testAnonymousClassFromInterface.

@Test
public void testAnonymousClassFromInterface() throws Exception {
    Node acu = parseAndTypeResolveForClass(AnonymousClassFromInterface.class, "1.8");
    ASTAllocationExpression allocationExpression = acu.getFirstDescendantOfType(ASTAllocationExpression.class);
    TypeNode child = (TypeNode) allocationExpression.jjtGetChild(0);
    Assert.assertTrue(Comparator.class.isAssignableFrom(child.getType()));
    Assert.assertSame(Integer.class, child.getTypeDefinition().getGenericType(0).getType());
}
Also used : TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) Comparator(java.util.Comparator) Test(org.junit.Test)

Example 2 with TypeNode

use of net.sourceforge.pmd.lang.java.ast.TypeNode in project pmd by pmd.

the class ClassTypeResolverTest method testAnonymousExtendingObject.

@Test
public void testAnonymousExtendingObject() throws Exception {
    Node acu = parseAndTypeResolveForClass(AnoymousExtendingObject.class, "1.8");
    ASTAllocationExpression allocationExpression = acu.getFirstDescendantOfType(ASTAllocationExpression.class);
    TypeNode child = (TypeNode) allocationExpression.jjtGetChild(0);
    Assert.assertTrue(Object.class.isAssignableFrom(child.getType()));
}
Also used : TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) AnoymousExtendingObject(net.sourceforge.pmd.typeresolution.testdata.AnoymousExtendingObject) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) Test(org.junit.Test)

Example 3 with TypeNode

use of net.sourceforge.pmd.lang.java.ast.TypeNode in project pmd by pmd.

the class ClassTypeResolver method getEnclosingTypeDeclaration.

/**
 * Returns the the first Class declaration around the node.
 *
 * @param node The node with the enclosing Class declaration.
 * @return The JavaTypeDefinition of the enclosing Class declaration.
 */
private TypeNode getEnclosingTypeDeclaration(Node node) {
    Node previousNode = null;
    while (node != null) {
        if (node instanceof ASTClassOrInterfaceDeclaration) {
            return (TypeNode) node;
        // anonymous class declaration
        } else if (// is anonymous class declaration
        node instanceof ASTAllocationExpression && // array cant be anonymous
        node.getFirstChildOfType(ASTArrayDimsAndInits.class) == null && !(previousNode instanceof ASTArguments)) {
            // we might come out of the constructor
            return (TypeNode) node;
        }
        previousNode = node;
        node = node.jjtGetParent();
    }
    return null;
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) QualifiableNode(net.sourceforge.pmd.lang.ast.QualifiableNode) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) ASTArguments(net.sourceforge.pmd.lang.java.ast.ASTArguments)

Example 4 with TypeNode

use of net.sourceforge.pmd.lang.java.ast.TypeNode in project pmd by pmd.

the class ClassTypeResolver method visit.

@Override
public Object visit(ASTWildcardBounds node, Object data) {
    super.visit(node, data);
    JavaTypeDefinition childType = ((TypeNode) node.jjtGetChild(0)).getTypeDefinition();
    if (node.jjtGetFirstToken().toString().equals("super")) {
        node.setTypeDefinition(JavaTypeDefinition.forClass(LOWER_WILDCARD, childType));
    } else {
        // equals "extends"
        node.setTypeDefinition(JavaTypeDefinition.forClass(UPPER_WILDCARD, childType));
    }
    return data;
}
Also used : JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)

Example 5 with TypeNode

use of net.sourceforge.pmd.lang.java.ast.TypeNode 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)

Aggregations

TypeNode (net.sourceforge.pmd.lang.java.ast.TypeNode)15 AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)11 Node (net.sourceforge.pmd.lang.ast.Node)6 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)4 JavaTypeDefinition (net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 AbstractJavaNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaNode)3 QualifiableNode (net.sourceforge.pmd.lang.ast.QualifiableNode)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)2 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)2 JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)2 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)2 Type (java.lang.reflect.Type)1 TypeVariable (java.lang.reflect.TypeVariable)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1