Search in sources :

Example 16 with AbstractJavaTypeNode

use of net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode in project pmd by pmd.

the class ClassTypeResolverTest method testMethodFirstPhase.

@Test
public void testMethodFirstPhase() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass(MethodFirstPhase.class, "1.8");
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // int a = subtype(10, 'a', "");
    assertEquals(int.class, expressions.get(index).getType());
    assertEquals(int.class, getChildType(expressions.get(index), 0));
    assertEquals(int.class, getChildType(expressions.get(index++), 1));
    // Exception b = vararg((Object) null);
    assertEquals(Exception.class, expressions.get(index).getType());
    assertEquals(Exception.class, getChildType(expressions.get(index), 0));
    assertEquals(Exception.class, getChildType(expressions.get(index++), 1));
    // Set<String> set = new HashSet<>();
    assertEquals(HashSet.class, expressions.get(index++).getType());
    // List<String> myList = new ArrayList<>();
    assertEquals(ArrayList.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)

Example 17 with AbstractJavaTypeNode

use of net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode in project pmd by pmd.

the class ClassTypeResolverTest method testFieldAccessGenericNested.

@Test
public void testFieldAccessGenericNested() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(FieldAccessGenericNested.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//StatementExpression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // n.field = null;
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.class, getChildType(expressions.get(index++), 0));
    // n.generic.first = null;
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.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 18 with AbstractJavaTypeNode

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

Example 19 with AbstractJavaTypeNode

use of net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode 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());
}
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 20 with AbstractJavaTypeNode

use of net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode 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());
}
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