use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit 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.ASTCompilationUnit in project pmd by pmd.
the class ClassTypeResolverTest method testBinaryLogicalOperators.
@Test
public void testBinaryLogicalOperators() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(Operators.class);
List<ASTExpression> expressions = convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = 'binaryLogicalOperators']]//Expression"), ASTExpression.class);
int index = 0;
assertEquals(Boolean.TYPE, expressions.get(index++).getType());
assertEquals(Boolean.TYPE, expressions.get(index++).getType());
assertEquals(Boolean.TYPE, expressions.get(index++).getType());
assertEquals(Boolean.TYPE, expressions.get(index++).getType());
assertEquals(Boolean.TYPE, expressions.get(index++).getType());
assertEquals(Boolean.TYPE, expressions.get(index++).getType());
assertEquals(Boolean.TYPE, expressions.get(index++).getType());
assertEquals(Boolean.TYPE, expressions.get(index++).getType());
assertEquals(Boolean.TYPE, expressions.get(index++).getType());
assertEquals(Boolean.TYPE, expressions.get(index++).getType());
assertEquals(Boolean.TYPE, expressions.get(index++).getType());
assertEquals(Boolean.TYPE, expressions.get(index++).getType());
assertEquals(Boolean.TYPE, expressions.get(index++).getType());
// 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 testNestedAllocationExpressions.
@Test
public void testNestedAllocationExpressions() {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(NestedAllocationExpressions.class);
List<ASTAllocationExpression> allocs = acu.findDescendantsOfType(ASTAllocationExpression.class);
assertFalse(allocs.get(0).isAnonymousClass());
assertEquals(Thread.class, allocs.get(0).getType());
assertTrue(allocs.get(1).isAnonymousClass());
// FUTURE 1.8 use Class.getTypeName() instead of toString
assertTrue(allocs.get(1).getType().toString().endsWith("NestedAllocationExpressions$1"));
}
use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit 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.ASTCompilationUnit in project pmd by pmd.
the class ClassTypeResolverTest method testEnumAnonymousInnerClass.
/**
* See bug #1138 Anonymous inner class in enum causes NPE
*/
@Test
public void testEnumAnonymousInnerClass() {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(EnumWithAnonymousInnerClass.class);
// try it in jshell, an enum constant with a body is compiled to an anonymous class,
// the counter is shared with other anonymous classes of the enum
Class<?> enumAnon = acu.getFirstDescendantOfType(ASTEnumConstant.class).getQualifiedName().getType();
assertEquals("net.sourceforge.pmd.typeresolution.testdata.EnumWithAnonymousInnerClass$1", enumAnon.getName());
Class<?> inner = acu.getFirstDescendantOfType(ASTAllocationExpression.class).getFirstDescendantOfType(ASTClassOrInterfaceType.class).getType();
assertEquals("net.sourceforge.pmd.typeresolution.testdata.EnumWithAnonymousInnerClass$2", inner.getName());
}
Aggregations