Search in sources :

Example 11 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(ASTName node, Object data) {
    Class<?> accessingClass = getEnclosingTypeDeclarationClass(node);
    String[] dotSplitImage = node.getImage().split("\\.");
    int startIndex = searchNodeNameForClass(node);
    ASTArguments astArguments = getSuffixMethodArgs(node);
    ASTArgumentList astArgumentList = getArgumentList(astArguments);
    int methodArgsArity = getArgumentListArity(astArgumentList);
    JavaTypeDefinition previousType;
    if (node.getType() != null) {
        // static field or method
        // node.getType() has been set by the call to searchNodeNameForClass above
        // node.getType() will have the value equal to the Class found by that method
        previousType = node.getTypeDefinition();
    } else {
        // non-static field or method
        if (dotSplitImage.length == 1 && astArguments != null) {
            // method
            List<MethodType> methods = getLocalApplicableMethods(node, dotSplitImage[0], Collections.<JavaTypeDefinition>emptyList(), methodArgsArity, accessingClass);
            TypeNode enclosingType = getEnclosingTypeDeclaration(node);
            if (enclosingType == null) {
                // we can't proceed, probably uncompiled sources
                return data;
            }
            previousType = getBestMethodReturnType(enclosingType.getTypeDefinition(), methods, astArgumentList);
        } else {
            // field
            previousType = getTypeDefinitionOfVariableFromScope(node.getScope(), dotSplitImage[0], accessingClass);
        }
        // first element's type in dotSplitImage has already been resolved
        startIndex = 1;
    }
    // as the code is not compiled there and symbol table works on uncompiled code
    if (node.getNameDeclaration() != null && // if it's not null, then let other code handle things
    previousType == null && node.getNameDeclaration().getNode() instanceof TypeNode) {
        // Carry over the type from the declaration
        Class<?> nodeType = ((TypeNode) node.getNameDeclaration().getNode()).getType();
        // FIXME : generic classes and class with generic super types could have the wrong type assigned here
        if (nodeType != null) {
            node.setType(nodeType);
            return super.visit(node, data);
        }
    }
    for (int i = startIndex; i < dotSplitImage.length; ++i) {
        if (previousType == null) {
            break;
        }
        if (i == dotSplitImage.length - 1 && astArguments != null) {
            // method
            List<MethodType> methods = getApplicableMethods(previousType, dotSplitImage[i], Collections.<JavaTypeDefinition>emptyList(), methodArgsArity, accessingClass);
            previousType = getBestMethodReturnType(previousType, methods, astArgumentList);
        } else {
            // field
            previousType = getFieldType(previousType, dotSplitImage[i], accessingClass);
        }
    }
    if (previousType != null) {
        node.setTypeDefinition(previousType);
    }
    return super.visit(node, 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) ASTArguments(net.sourceforge.pmd.lang.java.ast.ASTArguments) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList)

Example 12 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(ASTAllocationExpression node, Object data) {
    super.visit(node, data);
    final ASTArrayDimsAndInits dims = node.getFirstChildOfType(ASTArrayDimsAndInits.class);
    if (dims != null) {
        final Class<?> arrayType = ((TypeNode) node.jjtGetChild(0)).getType();
        if (arrayType != null) {
            node.setType(Array.newInstance(arrayType, (int[]) Array.newInstance(int.class, dims.getArrayDepth())).getClass());
        }
    } else {
        rollupTypeUnary(node);
    }
    return data;
}
Also used : ASTArrayDimsAndInits(net.sourceforge.pmd.lang.java.ast.ASTArrayDimsAndInits) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)

Example 13 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(ASTTypeParameters node, Object data) {
    super.visit(node, data);
    if (node.jjtGetParent() instanceof ASTClassOrInterfaceDeclaration) {
        TypeNode parent = (TypeNode) node.jjtGetParent();
        final JavaTypeDefinition[] boundGenerics = new JavaTypeDefinition[node.jjtGetNumChildren()];
        for (int i = 0; i < node.jjtGetNumChildren(); ++i) {
            boundGenerics[i] = ((TypeNode) node.jjtGetChild(i)).getTypeDefinition();
        }
        parent.setTypeDefinition(JavaTypeDefinition.forClass(parent.getType(), boundGenerics));
    }
    return data;
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) 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 14 with TypeNode

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

the class ClassTypeResolverTest method testUnaryNumericOperators.

@Test
public void testUnaryNumericOperators() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(Operators.class);
    List<TypeNode> expressions = new ArrayList<>();
    expressions.addAll(convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = 'unaryNumericOperators']]//Expression"), TypeNode.class));
    expressions.addAll(convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = " + "'unaryNumericOperators']]//PostfixExpression"), TypeNode.class));
    expressions.addAll(convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = " + "'unaryNumericOperators']]//PreIncrementExpression"), TypeNode.class));
    expressions.addAll(convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = " + "'unaryNumericOperators']]//PreDecrementExpression"), TypeNode.class));
    int index = 0;
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    // Make sure we got them all.
    assertEquals("All expressions not tested", index, expressions.size());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ArrayList(java.util.ArrayList) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) Test(org.junit.Test)

Example 15 with TypeNode

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

the class ClassTypeResolverTest method testNestedAnonymousClass.

@Test
public void testNestedAnonymousClass() throws Exception {
    Node acu = parseAndTypeResolveForClass(NestedAnonymousClass.class, "1.8");
    ASTAllocationExpression allocationExpression = acu.getFirstDescendantOfType(ASTAllocationExpression.class);
    ASTAllocationExpression nestedAllocation = // get the declaration (boundary)
    allocationExpression.getFirstDescendantOfType(ASTClassOrInterfaceBodyDeclaration.class).getFirstDescendantOfType(// and dive for the nested allocation
    ASTAllocationExpression.class);
    TypeNode child = (TypeNode) nestedAllocation.jjtGetChild(0);
    Assert.assertTrue(Converter.class.isAssignableFrom(child.getType()));
    Assert.assertSame(String.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) Converter(net.sourceforge.pmd.typeresolution.testdata.dummytypes.Converter) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) ASTClassOrInterfaceBodyDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration) Test(org.junit.Test)

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