Search in sources :

Example 46 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit 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 47 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit 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 48 with ASTCompilationUnit

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

the class ClassTypeResolverTest method acceptanceTest.

@Test
public void acceptanceTest() {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(ArrayListFound.class);
    assertEquals(ArrayListFound.class, acu.getFirstDescendantOfType(ASTTypeDeclaration.class).getType());
    assertEquals(ArrayListFound.class, acu.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class).getType());
    ASTImportDeclaration id = acu.getFirstDescendantOfType(ASTImportDeclaration.class);
    assertEquals("java.util", id.getPackage().getName());
    assertEquals(ArrayList.class, id.getType());
    assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTClassOrInterfaceType.class).getType());
    assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTReferenceType.class).getType());
    assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTType.class).getType());
    assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTVariableDeclaratorId.class).getType());
    assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTVariableDeclarator.class).getType());
    assertEquals(ArrayList.class, acu.getFirstDescendantOfType(ASTFieldDeclaration.class).getType());
    acu = parseAndTypeResolveForClass15(DefaultJavaLangImport.class);
    assertEquals(String.class, acu.getFirstDescendantOfType(ASTClassOrInterfaceType.class).getType());
    assertEquals(Override.class, acu.findDescendantsOfType(ASTName.class).get(1).getType());
}
Also used : ASTImportDeclaration(net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) DefaultJavaLangImport(net.sourceforge.pmd.typeresolution.testdata.DefaultJavaLangImport) Test(org.junit.Test)

Example 49 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit 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)

Example 50 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testGenericArrays.

@Test
public void testGenericArrays() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(GenericsArrays.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // List<String> var = Arrays.asList(params);
    AbstractJavaTypeNode expression = expressions.get(index++);
    // TODO : Type inference is still incomplete, we fail to detect the return type of the method
    // assertEquals(List.class, expression.getTypeDefinition().getType());
    // assertEquals(String.class, expression.getTypeDefinition().getGenericType(0).getType());
    // List<String> var2 = Arrays.<String>asList(params);
    AbstractJavaTypeNode expression2 = expressions.get(index++);
    assertEquals(List.class, expression2.getTypeDefinition().getType());
    assertEquals(String.class, expression2.getTypeDefinition().getGenericType(0).getType());
    // List<String[]> var3 = Arrays.<String[]>asList(params);
    AbstractJavaTypeNode expression3 = expressions.get(index++);
    assertEquals(List.class, expression3.getTypeDefinition().getType());
    assertEquals(String[].class, expression3.getTypeDefinition().getGenericType(0).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