use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.
the class ClassTypeResolverTest method testArrayTypes.
@Test
public void testArrayTypes() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(ArrayTypes.class);
List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableDeclarator"), AbstractJavaTypeNode.class);
int index = 0;
// int[] a = new int[1];
testSubtreeNodeTypes(expressions.get(index++), int[].class);
// Object[][] b = new Object[1][0];
testSubtreeNodeTypes(expressions.get(index++), Object[][].class);
// ArrayTypes[][][] c = new ArrayTypes[][][] { new ArrayTypes[1][2] };
testSubtreeNodeTypes(expressions.get(index++), ArrayTypes[][][].class);
// 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 testMethodAccessibility.
@Test
public void testMethodAccessibility() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(MethodAccessibility.class);
List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"), AbstractJavaTypeNode.class);
int index = 0;
// SuperClassA a = inheritedA();
assertEquals(SuperClassA.class, expressions.get(index).getType());
assertEquals(SuperClassA.class, getChildType(expressions.get(index), 0));
assertEquals(SuperClassA.class, getChildType(expressions.get(index++), 1));
// SuperClassB b = inheritedB();
assertEquals(SuperClassB.class, expressions.get(index).getType());
assertEquals(SuperClassB.class, getChildType(expressions.get(index), 0));
assertEquals(SuperClassB.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 acceptanceTest.
@Test
public void acceptanceTest() {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(ArrayListFound.class);
assertEquals(ArrayListFound.class, acu.getFirstDescendantOfType(ASTTypeDeclaration.class).getType());
assertEquals(ArrayListFound.class, acu.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class).getType());
ASTImportDeclaration id = acu.getFirstDescendantOfType(ASTImportDeclaration.class);
assertEquals("java.util", id.getPackage().getName());
assertEquals(ArrayList.class, id.getType());
assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTClassOrInterfaceType.class).getType());
assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTReferenceType.class).getType());
assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTType.class).getType());
assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTVariableDeclaratorId.class).getType());
assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTVariableDeclarator.class).getType());
assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTFieldDeclaration.class).getType());
acu = parseAndTypeResolveForClass15(DefaultJavaLangImport.class);
assertEquals(String.class, acu.getFirstDescendantOfType(ASTClassOrInterfaceType.class).getType());
assertEquals(Override.class, acu.findDescendantsOfType(ASTName.class).get(1).getType());
}
use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.
the class ClassTypeResolverTest method testMethodPotentialApplicability.
@Test
public void testMethodPotentialApplicability() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(MethodPotentialApplicability.class);
List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"), AbstractJavaTypeNode.class);
int index = 0;
// int a = vararg("");
assertEquals(int.class, expressions.get(index).getType());
assertEquals(int.class, getChildType(expressions.get(index), 0));
assertEquals(int.class, getChildType(expressions.get(index++), 1));
// int b = 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 c = notVararg(0, 0);
assertEquals(String.class, expressions.get(index).getType());
assertEquals(String.class, getChildType(expressions.get(index), 0));
assertEquals(String.class, getChildType(expressions.get(index++), 1));
// Number d = noArguments();
assertEquals(Number.class, expressions.get(index).getType());
assertEquals(Number.class, getChildType(expressions.get(index), 0));
assertEquals(Number.class, getChildType(expressions.get(index++), 1));
// Number e = field.noArguments();
assertEquals(Number.class, expressions.get(index).getType());
assertEquals(Number.class, getChildType(expressions.get(index), 0));
assertEquals(Number.class, getChildType(expressions.get(index++), 1));
// int f = this.vararg("");
assertEquals(int.class, expressions.get(index).getType());
assertEquals(int.class, getChildType(expressions.get(index), 1));
assertEquals(int.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 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());
}
Aggregations