Search in sources :

Example 6 with AbstractJavaTypeNode

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());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) Test(org.junit.Test)

Example 7 with AbstractJavaTypeNode

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());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) Test(org.junit.Test)

Example 8 with AbstractJavaTypeNode

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());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) AnoymousExtendingObject(net.sourceforge.pmd.typeresolution.testdata.AnoymousExtendingObject) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) Test(org.junit.Test)

Example 9 with AbstractJavaTypeNode

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());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) Test(org.junit.Test)

Example 10 with AbstractJavaTypeNode

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());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) Test(org.junit.Test)

Aggregations

AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)27 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)26 Test (org.junit.Test)26 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)25 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)1 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)1 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)1 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)1 JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)1 JavaTypeDefinition (net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition)1 AnoymousExtendingObject (net.sourceforge.pmd.typeresolution.testdata.AnoymousExtendingObject)1 ArrayTypes (net.sourceforge.pmd.typeresolution.testdata.ArrayTypes)1 FieldAccessNested (net.sourceforge.pmd.typeresolution.testdata.FieldAccessNested)1 FieldAccessSuper (net.sourceforge.pmd.typeresolution.testdata.FieldAccessSuper)1