Search in sources :

Example 11 with ASTCompilationUnit

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());
}
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 12 with ASTCompilationUnit

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());
}
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 13 with ASTCompilationUnit

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());
}
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 14 with ASTCompilationUnit

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());
}
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 15 with ASTCompilationUnit

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());
}
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

ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)79 Test (org.junit.Test)66 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)36 AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)27 StringReader (java.io.StringReader)8 RuleContext (net.sourceforge.pmd.RuleContext)8 LanguageVersionHandler (net.sourceforge.pmd.lang.LanguageVersionHandler)8 ASTImportDeclaration (net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration)7 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)6 Node (net.sourceforge.pmd.lang.ast.Node)5 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)5 ArrayList (java.util.ArrayList)4 ParserOptions (net.sourceforge.pmd.lang.ParserOptions)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Method (java.lang.reflect.Method)2 List (java.util.List)2 LanguageVersion (net.sourceforge.pmd.lang.LanguageVersion)2 Parser (net.sourceforge.pmd.lang.Parser)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2