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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations