Search in sources :

Example 26 with AbstractJavaTypeNode

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

the class ClassTypeResolverTest method testFieldAccess.

@Test
public void testFieldAccess() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(FieldAccess.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//StatementExpression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // param.field = 10;
    assertEquals(Integer.TYPE, expressions.get(index).getType());
    assertEquals(Integer.TYPE, getChildType(expressions.get(index++), 0));
    // local.field = 10;
    assertEquals(Integer.TYPE, expressions.get(index).getType());
    assertEquals(Integer.TYPE, getChildType(expressions.get(index++), 0));
    // f.f.f.field = 10;
    assertEquals(Integer.TYPE, expressions.get(index).getType());
    assertEquals(Integer.TYPE, getChildType(expressions.get(index++), 0));
    // (this).f.f.field = 10;
    assertEquals(Integer.TYPE, expressions.get(index).getType());
    assertEquals(FieldAccess.class, getChildType(expressions.get(index), 0));
    assertEquals(FieldAccess.class, getChildType(expressions.get(index), 1));
    assertEquals(FieldAccess.class, getChildType(expressions.get(index), 2));
    assertEquals(Integer.TYPE, getChildType(expressions.get(index++), 3));
    // field = 10;
    assertEquals(Integer.TYPE, expressions.get(index).getType());
    assertEquals(Integer.TYPE, 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)

Example 27 with AbstractJavaTypeNode

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

the class ClassTypeResolverTest method testMethodMostSpecific.

@Test
public void testMethodMostSpecific() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(MethodMostSpecific.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // String a = moreSpecific((Number) null, (AbstractCollection) null);
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.class, getChildType(expressions.get(index), 0));
    assertEquals(String.class, getChildType(expressions.get(index++), 1));
    // Exception b = moreSpecific((Integer) null, (AbstractList) null);
    assertEquals(Exception.class, expressions.get(index).getType());
    assertEquals(Exception.class, getChildType(expressions.get(index), 0));
    assertEquals(Exception.class, getChildType(expressions.get(index++), 1));
    // int c = moreSpecific((Double) null, (RoleList) null);
    assertEquals(int.class, expressions.get(index).getType());
    assertEquals(int.class, getChildType(expressions.get(index), 0));
    assertEquals(int.class, getChildType(expressions.get(index++), 1));
    // 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