Search in sources :

Example 21 with AbstractJavaTypeNode

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

the class ClassTypeResolverTest method testGenericArrays.

@Test
public void testGenericArrays() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(GenericsArrays.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // List<String> var = Arrays.asList(params);
    AbstractJavaTypeNode expression = expressions.get(index++);
    // TODO : Type inference is still incomplete, we fail to detect the return type of the method
    // assertEquals(List.class, expression.getTypeDefinition().getType());
    // assertEquals(String.class, expression.getTypeDefinition().getGenericType(0).getType());
    // List<String> var2 = Arrays.<String>asList(params);
    AbstractJavaTypeNode expression2 = expressions.get(index++);
    assertEquals(List.class, expression2.getTypeDefinition().getType());
    assertEquals(String.class, expression2.getTypeDefinition().getGenericType(0).getType());
    // List<String[]> var3 = Arrays.<String[]>asList(params);
    AbstractJavaTypeNode expression3 = expressions.get(index++);
    assertEquals(List.class, expression3.getTypeDefinition().getType());
    assertEquals(String[].class, expression3.getTypeDefinition().getGenericType(0).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 22 with AbstractJavaTypeNode

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

the class ClassTypeResolverTest method testFieldAccessNested.

@Test
public void testFieldAccessNested() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(FieldAccessNested.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//StatementExpression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // field = 10;
    assertEquals(Integer.TYPE, expressions.get(index).getType());
    assertEquals(Integer.TYPE, getChildType(expressions.get(index++), 0));
    // a = new SuperClassA();
    assertEquals(SuperClassA.class, expressions.get(index).getType());
    assertEquals(SuperClassA.class, getChildType(expressions.get(index++), 0));
    // net.sourceforge.pmd.typeresolution.testdata.FieldAccessNested.Nested.this.a = new SuperClassA();
    assertEquals(SuperClassA.class, expressions.get(index).getType());
    assertEquals(FieldAccessNested.Nested.class, getChildType(expressions.get(index), 0));
    assertEquals(FieldAccessNested.Nested.class, getChildType(expressions.get(index), 1));
    assertEquals(SuperClassA.class, getChildType(expressions.get(index++), 2));
    // FieldAccessNested.Nested.this.a = new SuperClassA();
    assertEquals(SuperClassA.class, expressions.get(index).getType());
    assertEquals(FieldAccessNested.Nested.class, getChildType(expressions.get(index), 0));
    assertEquals(FieldAccessNested.Nested.class, getChildType(expressions.get(index), 1));
    assertEquals(SuperClassA.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) FieldAccessNested(net.sourceforge.pmd.typeresolution.testdata.FieldAccessNested) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) Test(org.junit.Test)

Example 23 with AbstractJavaTypeNode

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

the class ClassTypeResolverTest method testMethodThirdPhase.

@Test
public void testMethodThirdPhase() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(MethodThirdPhase.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // Exception a = vararg(10, (Number) null, (Number) null);
    assertEquals(Exception.class, expressions.get(index).getType());
    assertEquals(Exception.class, getChildType(expressions.get(index), 0));
    assertEquals(Exception.class, getChildType(expressions.get(index++), 1));
    // Exception b = vararg(10);
    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 = vararg(10, "", "", "");
    assertEquals(int.class, expressions.get(index).getType());
    assertEquals(int.class, getChildType(expressions.get(index), 0));
    assertEquals(int.class, getChildType(expressions.get(index++), 1));
    // String d = mostSpecific(10, 10, 10);
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.class, getChildType(expressions.get(index), 0));
    assertEquals(String.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)

Example 24 with AbstractJavaTypeNode

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

the class ClassTypeResolverTest method testRawGenericFieldAccess.

@Test
public void testRawGenericFieldAccess() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(FieldAccessGenericRaw.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//StatementExpression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // rawGeneric.first = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // rawGeneric.second = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // rawGeneric.third = new Object();
    assertEquals(Object.class, expressions.get(index).getType());
    assertEquals(Object.class, getChildType(expressions.get(index++), 0));
    // rawGeneric.fourth.second = "";
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.class, getChildType(expressions.get(index++), 0));
    // rawGeneric.rawGeneric.second = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // inheritedRawGeneric.first = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // inheritedRawGeneric.second = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // inheritedRawGeneric.third = new Object();
    assertEquals(Object.class, expressions.get(index).getType());
    assertEquals(Object.class, getChildType(expressions.get(index++), 0));
    // inheritedRawGeneric.fourth.second = "";
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.class, getChildType(expressions.get(index++), 0));
    // inheritedRawGeneric.rawGeneric.second = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // parameterRawGeneric.first = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // parameterRawGeneric.second = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // parameterRawGeneric.third = new Object();
    assertEquals(Object.class, expressions.get(index).getType());
    assertEquals(Object.class, getChildType(expressions.get(index++), 0));
    // parameterRawGeneric.fourth.second = "";
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.class, getChildType(expressions.get(index++), 0));
    // parameterRawGeneric.rawGeneric.second = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // bug #471
    // rawGeneric.fifth = new GenericClass();
    assertEquals(GenericClass.class, expressions.get(index).getType());
    assertEquals(GenericClass.class, getChildType(expressions.get(index++), 0));
    // inheritedRawGeneric.fifth = new GenericClass();
    assertEquals(GenericClass.class, expressions.get(index).getType());
    assertEquals(GenericClass.class, getChildType(expressions.get(index++), 0));
    // parameterRawGeneric.fifth = new GenericClass();
    assertEquals(GenericClass.class, expressions.get(index).getType());
    assertEquals(GenericClass.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)

Example 25 with AbstractJavaTypeNode

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

the class ClassTypeResolverTest method testFieldAccessSuper.

@Test
public void testFieldAccessSuper() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(FieldAccessSuper.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//StatementExpression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // s = new SuperClassA();
    assertEquals(SuperClassA.class, expressions.get(index).getType());
    assertEquals(SuperClassA.class, getChildType(expressions.get(index++), 0));
    // (this).s.s2 = new SuperClassA2();
    assertEquals(SuperClassA2.class, expressions.get(index).getType());
    assertEquals(FieldAccessSuper.class, getChildType(expressions.get(index), 0));
    assertEquals(SuperClassA.class, getChildType(expressions.get(index), 1));
    assertEquals(SuperClassA2.class, getChildType(expressions.get(index++), 2));
    // s.s.s2 = new SuperClassA2();
    assertEquals(SuperClassA2.class, expressions.get(index).getType());
    assertEquals(SuperClassA2.class, getChildType(expressions.get(index++), 0));
    // super.s = new SuperClassA();
    assertEquals(SuperClassA.class, expressions.get(index).getType());
    assertEquals(SuperClassA.class, getChildType(expressions.get(index), 0));
    assertEquals(SuperClassA.class, getChildType(expressions.get(index++), 1));
    // net.sourceforge.pmd.typeresolution.testdata.FieldAccessSuper.this.s = new SuperClassA();
    assertEquals(SuperClassA.class, expressions.get(index).getType());
    assertEquals(FieldAccessSuper.class, getChildType(expressions.get(index), 0));
    assertEquals(FieldAccessSuper.class, getChildType(expressions.get(index), 1));
    assertEquals(SuperClassA.class, getChildType(expressions.get(index++), 2));
    // s = new SuperClassA();
    assertEquals(SuperClassA.class, expressions.get(index).getType());
    assertEquals(SuperClassA.class, getChildType(expressions.get(index++), 0));
    // bs = new SuperClassB();
    assertEquals(SuperClassB.class, expressions.get(index).getType());
    assertEquals(SuperClassB.class, getChildType(expressions.get(index++), 0));
    // FieldAccessSuper.Nested.super.bs = new SuperClassB();
    assertEquals(SuperClassB.class, expressions.get(index).getType());
    assertEquals(FieldAccessSuper.Nested.class, getChildType(expressions.get(index), 0));
    assertEquals(SuperClassB.class, getChildType(expressions.get(index), 1));
    assertEquals(SuperClassB.class, getChildType(expressions.get(index++), 2));
    // FieldAccessSuper.super.s = new SuperClassA();
    assertEquals(SuperClassA.class, expressions.get(index).getType());
    assertEquals(FieldAccessSuper.class, getChildType(expressions.get(index), 0));
    assertEquals(SuperClassA.class, getChildType(expressions.get(index), 1));
    assertEquals(SuperClassA.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) FieldAccessSuper(net.sourceforge.pmd.typeresolution.testdata.FieldAccessSuper) 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