Search in sources :

Example 11 with AbstractJavaTypeNode

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

the class ClassTypeResolverTest method testSuperExpression.

@Test
public void testSuperExpression() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(SuperExpression.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression/PrimaryPrefix"), AbstractJavaTypeNode.class);
    int index = 0;
    assertEquals(SuperClassA.class, expressions.get(index++).getType());
    assertEquals(SuperClassA.class, expressions.get(index++).getType());
    assertEquals(SuperClassA.class, expressions.get(index++).getType());
    assertEquals(SuperClassA.class, expressions.get(index++).getType());
    assertEquals(SuperExpression.class, ((TypeNode) expressions.get(index).jjtGetParent().jjtGetChild(0)).getType());
    assertEquals(SuperClassA.class, ((TypeNode) expressions.get(index++).jjtGetParent().jjtGetChild(1)).getType());
    assertEquals(SuperExpression.class, expressions.get(index++).getType());
    assertEquals(SuperExpression.class, 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) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) Test(org.junit.Test)

Example 12 with AbstractJavaTypeNode

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

the class ClassTypeResolverTest method testMethodGenericExplicit.

@Test
public void testMethodGenericExplicit() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(MethodGenericExplicit.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // String s = this.<String>foo();
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.class, getChildType(expressions.get(index), 1));
    assertEquals(String.class, getChildType(expressions.get(index++), 2));
    // Make sure we got them all
    assertEquals("All expressions not tested", index, expressions.size());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) Test(org.junit.Test)

Example 13 with AbstractJavaTypeNode

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

the class ClassTypeResolverJava8Test method testSuperExpression.

@Test
public void testSuperExpression() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass18(SuperExpression.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression/PrimaryPrefix"), AbstractJavaTypeNode.class);
    int index = 0;
    assertEquals(SuperClass.class, 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) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) Test(org.junit.Test)

Example 14 with AbstractJavaTypeNode

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

the class ClassTypeResolver method visit.

@Override
public Object visit(ASTPrimaryExpression primaryNode, Object data) {
    // visit method arguments in reverse
    for (int i = primaryNode.jjtGetNumChildren() - 1; i >= 0; --i) {
        ((JavaNode) primaryNode.jjtGetChild(i)).jjtAccept(this, data);
    }
    JavaTypeDefinition primaryNodeType = null;
    AbstractJavaTypeNode previousChild = null;
    AbstractJavaTypeNode nextChild;
    Class<?> accessingClass = getEnclosingTypeDeclarationClass(primaryNode);
    for (int childIndex = 0; childIndex < primaryNode.jjtGetNumChildren(); ++childIndex) {
        AbstractJavaTypeNode currentChild = (AbstractJavaTypeNode) primaryNode.jjtGetChild(childIndex);
        nextChild = childIndex + 1 < primaryNode.jjtGetNumChildren() ? (AbstractJavaTypeNode) primaryNode.jjtGetChild(childIndex + 1) : null;
        // skip children which already have their type assigned
        if (currentChild.getType() == null) {
            // Last token, because if 'this' is a Suffix, it'll have tokens '.' and 'this'
            if (currentChild.jjtGetLastToken().toString().equals("this")) {
                if (previousChild != null) {
                    // Qualified 'this' expression
                    currentChild.setTypeDefinition(previousChild.getTypeDefinition());
                } else {
                    // simple 'this' expression
                    ASTClassOrInterfaceDeclaration typeDeclaration = currentChild.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
                    if (typeDeclaration != null) {
                        currentChild.setTypeDefinition(typeDeclaration.getTypeDefinition());
                    }
                }
            // Last token, because if 'super' is a Suffix, it'll have tokens '.' and 'super'
            } else if (currentChild.jjtGetLastToken().toString().equals("super")) {
                if (previousChild != null) {
                    // Qualified 'super' expression
                    // anonymous classes can't have qualified super expression, thus
                    // getSuperClassTypeDefinition's second argumet isn't null, but we are not
                    // looking for enclosing super types
                    currentChild.setTypeDefinition(getSuperClassTypeDefinition(currentChild, previousChild.getType()));
                } else {
                    // simple 'super' expression
                    currentChild.setTypeDefinition(getSuperClassTypeDefinition(currentChild, null));
                }
            } else if (currentChild.getFirstChildOfType(ASTArguments.class) != null) {
                currentChild.setTypeDefinition(previousChild.getTypeDefinition());
            } else if (previousChild != null && previousChild.getType() != null) {
                String currentChildImage = currentChild.getImage();
                if (currentChildImage == null) {
                    // this.<Something>foo(); <Something>foo would be in a Suffix and would have a null image
                    currentChildImage = currentChild.jjtGetLastToken().toString();
                }
                ASTArguments astArguments = nextChild != null ? nextChild.getFirstChildOfType(ASTArguments.class) : null;
                if (astArguments != null) {
                    // method
                    ASTArgumentList astArgumentList = getArgumentList(astArguments);
                    int methodArgsArity = getArgumentListArity(astArgumentList);
                    List<JavaTypeDefinition> typeArguments = getMethodExplicitTypeArugments(currentChild);
                    List<MethodType> methods = getApplicableMethods(previousChild.getTypeDefinition(), currentChildImage, typeArguments, methodArgsArity, accessingClass);
                    currentChild.setTypeDefinition(getBestMethodReturnType(previousChild.getTypeDefinition(), methods, astArgumentList));
                } else {
                    // field
                    currentChild.setTypeDefinition(getFieldType(previousChild.getTypeDefinition(), currentChildImage, accessingClass));
                }
            }
        }
        if (currentChild.getType() != null) {
            primaryNodeType = currentChild.getTypeDefinition();
        } else {
            // avoid falsely passing tests
            primaryNodeType = null;
            break;
        }
        previousChild = currentChild;
    }
    primaryNode.setTypeDefinition(primaryNodeType);
    return data;
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) 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) ASTArguments(net.sourceforge.pmd.lang.java.ast.ASTArguments) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)

Example 15 with AbstractJavaTypeNode

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

the class ClassTypeResolverTest method testSimpleGenericFieldAccess.

@Test
public void testSimpleGenericFieldAccess() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(FieldAccessGenericSimple.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//StatementExpression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // genericField.first = "";
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.class, getChildType(expressions.get(index++), 0));
    // genericField.second = new Double(0);
    assertEquals(Double.class, expressions.get(index).getType());
    assertEquals(Double.class, getChildType(expressions.get(index++), 0));
    // genericTypeArg.second.second = new Double(0);
    assertEquals(Double.class, expressions.get(index).getType());
    assertEquals(Double.class, getChildType(expressions.get(index++), 0));
    // param.first = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // local.second = new Long(0);
    assertEquals(Long.class, expressions.get(index).getType());
    assertEquals(Long.class, getChildType(expressions.get(index++), 0));
    // param.generic.first = new Character('c');
    assertEquals(Character.class, expressions.get(index).getType());
    assertEquals(Character.class, getChildType(expressions.get(index++), 0));
    // local.generic.second = new Float(0);
    assertEquals(Float.class, expressions.get(index).getType());
    assertEquals(Float.class, getChildType(expressions.get(index++), 0));
    // genericField.generic.generic.generic.first = new Double(0);
    assertEquals(Double.class, expressions.get(index).getType());
    assertEquals(Double.class, getChildType(expressions.get(index++), 0));
    // fieldA = new Long(0);
    assertEquals(Long.class, expressions.get(index).getType());
    assertEquals(Long.class, getChildType(expressions.get(index++), 0));
    // fieldB.generic.second = "";
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.class, getChildType(expressions.get(index++), 0));
    // fieldAcc.fieldA = new Long(0);
    assertEquals(Long.class, expressions.get(index).getType());
    assertEquals(Long.class, getChildType(expressions.get(index++), 0));
    // fieldA = new Long(0);
    assertEquals(Long.class, expressions.get(index).getType());
    assertEquals(Long.class, getChildType(expressions.get(index++), 0));
    // Make sure we got them all
    assertEquals("All expressions not tested", index, expressions.size());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) Test(org.junit.Test)

Aggregations

AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)27 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)26 Test (org.junit.Test)26 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)25 ArrayList (java.util.ArrayList)1 List (java.util.List)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 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)1 JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)1 JavaTypeDefinition (net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition)1 AnoymousExtendingObject (net.sourceforge.pmd.typeresolution.testdata.AnoymousExtendingObject)1 ArrayTypes (net.sourceforge.pmd.typeresolution.testdata.ArrayTypes)1 FieldAccessNested (net.sourceforge.pmd.typeresolution.testdata.FieldAccessNested)1 FieldAccessSuper (net.sourceforge.pmd.typeresolution.testdata.FieldAccessSuper)1