use of net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode in project pmd by pmd.
the class ClassTypeResolverTest method testMethodSecondPhase.
@Test
public void testMethodSecondPhase() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(MethodSecondPhase.class);
List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"), AbstractJavaTypeNode.class);
int index = 0;
// String a = boxing(10, "");
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 = boxing('a', "");
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 = boxing(10L, "");
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 = unboxing("", (Integer) 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 e = unboxing("", (Character) 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 f = unboxing("", (Byte) 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.AbstractJavaTypeNode 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.AbstractJavaTypeNode 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.AbstractJavaTypeNode 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.AbstractJavaTypeNode 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());
}
Aggregations