use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.
the class ClassTypeResolverTest method testMethodTypeInferenceVarargsAsFixedArity.
@Test
public void testMethodTypeInferenceVarargsAsFixedArity() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(VarargsAsFixedArity.class);
List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"), AbstractJavaTypeNode.class);
int index = 0;
// int var = aMethod("");
assertEquals(int.class, expressions.get(index++).getType());
// String var2 = aMethod();
assertEquals(String.class, expressions.get(index++).getType());
// String var3 = aMethod("", "");
assertEquals(String.class, expressions.get(index++).getType());
// String var4 = aMethod(new Object[] { null });
assertEquals(String.class, expressions.get(index++).getType());
// null literal has null type
assertNull(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 testBoundsGenericFieldAccess.
@Test
public void testBoundsGenericFieldAccess() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(FieldAccessGenericBounds.class);
List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//StatementExpression/PrimaryExpression"), AbstractJavaTypeNode.class);
int index = 0;
// superGeneric.first = ""; // ? super String
assertEquals(forClass(LOWER_WILDCARD, String.class), expressions.get(index).getTypeDefinition());
assertEquals(forClass(LOWER_WILDCARD, String.class), getChildTypeDef(expressions.get(index++), 0));
// superGeneric.second = null; // ?
assertEquals(forClass(UPPER_WILDCARD, Object.class), expressions.get(index).getTypeDefinition());
assertEquals(forClass(UPPER_WILDCARD, Object.class), getChildTypeDef(expressions.get(index++), 0));
// inheritedSuperGeneric.first = ""; // ? super String
assertEquals(forClass(LOWER_WILDCARD, String.class), expressions.get(index).getTypeDefinition());
assertEquals(forClass(LOWER_WILDCARD, String.class), getChildTypeDef(expressions.get(index++), 0));
// inheritedSuperGeneric.second = null; // ?
assertEquals(forClass(UPPER_WILDCARD, Object.class), expressions.get(index).getTypeDefinition());
assertEquals(forClass(UPPER_WILDCARD, Object.class), getChildTypeDef(expressions.get(index++), 0));
// upperBound.first = null; // ? extends Number
assertEquals(forClass(UPPER_WILDCARD, Number.class), expressions.get(index).getTypeDefinition());
assertEquals(forClass(UPPER_WILDCARD, Number.class), getChildTypeDef(expressions.get(index++), 0));
// inheritedUpperBound.first = null; // ? extends String
assertEquals(forClass(UPPER_WILDCARD, String.class), expressions.get(index).getTypeDefinition());
assertEquals(forClass(UPPER_WILDCARD, String.class), getChildTypeDef(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 testParameterGenericFieldAccess.
@Test
public void testParameterGenericFieldAccess() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(FieldAccessGenericParameter.class);
List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//StatementExpression/PrimaryExpression"), AbstractJavaTypeNode.class);
int index = 0;
// classGeneric = null; // Double
assertEquals(Double.class, expressions.get(index).getType());
assertEquals(Double.class, getChildType(expressions.get(index++), 0));
// localGeneric = null; // Character
assertEquals(Character.class, expressions.get(index).getType());
assertEquals(Character.class, getChildType(expressions.get(index++), 0));
// parameterGeneric.second.second = new Integer(0);
assertEquals(Integer.class, expressions.get(index).getType());
assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
// localGeneric = null; // Number
assertEquals(Number.class, expressions.get(index).getType());
assertEquals(Number.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 testFieldAccessShadow.
@Test
public void testFieldAccessShadow() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(FieldAccessShadow.class);
List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//StatementExpression/PrimaryExpression"), AbstractJavaTypeNode.class);
int index = 0;
// field = "shadow";
assertEquals(String.class, expressions.get(index).getType());
assertEquals(String.class, getChildType(expressions.get(index++), 0));
// this.field = new Integer(10);
assertEquals(Integer.class, expressions.get(index).getType());
assertEquals(FieldAccessShadow.class, getChildType(expressions.get(index), 0));
assertEquals(Integer.class, getChildType(expressions.get(index++), 1));
// (this).field = new Integer(10);
assertEquals(Integer.class, expressions.get(index).getType());
assertEquals(FieldAccessShadow.class, getChildType(expressions.get(index), 0));
assertEquals(Integer.class, getChildType(expressions.get(index++), 1));
// s2 = new SuperClassB2();
assertEquals(SuperClassB2.class, expressions.get(index).getType());
assertEquals(SuperClassB2.class, getChildType(expressions.get(index++), 0));
// privateShadow = 10;
assertEquals(Number.class, expressions.get(index).getType());
assertEquals(Number.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 testSuperExpression.
@Test
public void testSuperExpression() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(SuperExpression.class);
List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression/PrimaryPrefix"), AbstractJavaTypeNode.class);
int index = 0;
assertEquals(SuperClassA.class, expressions.get(index++).getType());
assertEquals(SuperClassA.class, expressions.get(index++).getType());
assertEquals(SuperClassA.class, expressions.get(index++).getType());
assertEquals(SuperClassA.class, expressions.get(index++).getType());
assertEquals(SuperExpression.class, ((TypeNode) expressions.get(index).jjtGetParent().jjtGetChild(0)).getType());
assertEquals(SuperClassA.class, ((TypeNode) expressions.get(index++).jjtGetParent().jjtGetChild(1)).getType());
assertEquals(SuperExpression.class, expressions.get(index++).getType());
assertEquals(SuperExpression.class, expressions.get(index++).getType());
// Make sure we got them all
assertEquals("All expressions not tested", index, expressions.size());
}
Aggregations