use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit 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.ASTCompilationUnit 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());
}
use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit 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());
}
use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit 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());
}
use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.
the class ClassTypeResolverTest method testFullyQualifiedType.
/**
* The type should be filled also on the ASTVariableDeclaratorId node, not
* only on the variable name declaration.
*/
@Test
public void testFullyQualifiedType() {
String source = "public class Foo {\n" + " public void bar() {\n" + " java.util.StringTokenizer st = new StringTokenizer(\"a.b.c.d\", \".\");\n" + " while (st.hasMoreTokens()) {\n" + " System.out.println(st.nextToken());\n" + " }\n" + " }\n" + "}";
ASTCompilationUnit acu = parseAndTypeResolveForString(source, "1.5");
List<ASTName> names = acu.findDescendantsOfType(ASTName.class);
ASTName theStringTokenizer = null;
for (ASTName name : names) {
if (name.hasImageEqualTo("st.hasMoreTokens")) {
theStringTokenizer = name;
break;
}
}
Assert.assertNotNull(theStringTokenizer);
VariableNameDeclaration declaration = (VariableNameDeclaration) theStringTokenizer.getNameDeclaration();
Assert.assertNotNull(declaration);
Assert.assertEquals("java.util.StringTokenizer", declaration.getTypeImage());
Assert.assertNotNull(declaration.getType());
Assert.assertSame(StringTokenizer.class, declaration.getType());
ASTVariableDeclaratorId id = (ASTVariableDeclaratorId) declaration.getNode();
Assert.assertNotNull(id.getType());
Assert.assertSame(StringTokenizer.class, id.getType());
}
Aggregations