Search in sources :

Example 61 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testRawGenericFieldAccess.

@Test
public void testRawGenericFieldAccess() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(FieldAccessGenericRaw.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//StatementExpression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // rawGeneric.first = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // rawGeneric.second = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // rawGeneric.third = new Object();
    assertEquals(Object.class, expressions.get(index).getType());
    assertEquals(Object.class, getChildType(expressions.get(index++), 0));
    // rawGeneric.fourth.second = "";
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.class, getChildType(expressions.get(index++), 0));
    // rawGeneric.rawGeneric.second = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // inheritedRawGeneric.first = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // inheritedRawGeneric.second = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // inheritedRawGeneric.third = new Object();
    assertEquals(Object.class, expressions.get(index).getType());
    assertEquals(Object.class, getChildType(expressions.get(index++), 0));
    // inheritedRawGeneric.fourth.second = "";
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.class, getChildType(expressions.get(index++), 0));
    // inheritedRawGeneric.rawGeneric.second = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // parameterRawGeneric.first = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // parameterRawGeneric.second = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // parameterRawGeneric.third = new Object();
    assertEquals(Object.class, expressions.get(index).getType());
    assertEquals(Object.class, getChildType(expressions.get(index++), 0));
    // parameterRawGeneric.fourth.second = "";
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.class, getChildType(expressions.get(index++), 0));
    // parameterRawGeneric.rawGeneric.second = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // bug #471
    // rawGeneric.fifth = new GenericClass();
    assertEquals(GenericClass.class, expressions.get(index).getType());
    assertEquals(GenericClass.class, getChildType(expressions.get(index++), 0));
    // inheritedRawGeneric.fifth = new GenericClass();
    assertEquals(GenericClass.class, expressions.get(index).getType());
    assertEquals(GenericClass.class, getChildType(expressions.get(index++), 0));
    // parameterRawGeneric.fifth = new GenericClass();
    assertEquals(GenericClass.class, expressions.get(index).getType());
    assertEquals(GenericClass.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 62 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testFieldAccessSuper.

@Test
public void testFieldAccessSuper() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(FieldAccessSuper.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//StatementExpression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // s = new SuperClassA();
    assertEquals(SuperClassA.class, expressions.get(index).getType());
    assertEquals(SuperClassA.class, getChildType(expressions.get(index++), 0));
    // (this).s.s2 = new SuperClassA2();
    assertEquals(SuperClassA2.class, expressions.get(index).getType());
    assertEquals(FieldAccessSuper.class, getChildType(expressions.get(index), 0));
    assertEquals(SuperClassA.class, getChildType(expressions.get(index), 1));
    assertEquals(SuperClassA2.class, getChildType(expressions.get(index++), 2));
    // s.s.s2 = new SuperClassA2();
    assertEquals(SuperClassA2.class, expressions.get(index).getType());
    assertEquals(SuperClassA2.class, getChildType(expressions.get(index++), 0));
    // super.s = new SuperClassA();
    assertEquals(SuperClassA.class, expressions.get(index).getType());
    assertEquals(SuperClassA.class, getChildType(expressions.get(index), 0));
    assertEquals(SuperClassA.class, getChildType(expressions.get(index++), 1));
    // net.sourceforge.pmd.typeresolution.testdata.FieldAccessSuper.this.s = new SuperClassA();
    assertEquals(SuperClassA.class, expressions.get(index).getType());
    assertEquals(FieldAccessSuper.class, getChildType(expressions.get(index), 0));
    assertEquals(FieldAccessSuper.class, getChildType(expressions.get(index), 1));
    assertEquals(SuperClassA.class, getChildType(expressions.get(index++), 2));
    // s = new SuperClassA();
    assertEquals(SuperClassA.class, expressions.get(index).getType());
    assertEquals(SuperClassA.class, getChildType(expressions.get(index++), 0));
    // bs = new SuperClassB();
    assertEquals(SuperClassB.class, expressions.get(index).getType());
    assertEquals(SuperClassB.class, getChildType(expressions.get(index++), 0));
    // FieldAccessSuper.Nested.super.bs = new SuperClassB();
    assertEquals(SuperClassB.class, expressions.get(index).getType());
    assertEquals(FieldAccessSuper.Nested.class, getChildType(expressions.get(index), 0));
    assertEquals(SuperClassB.class, getChildType(expressions.get(index), 1));
    assertEquals(SuperClassB.class, getChildType(expressions.get(index++), 2));
    // FieldAccessSuper.super.s = new SuperClassA();
    assertEquals(SuperClassA.class, expressions.get(index).getType());
    assertEquals(FieldAccessSuper.class, getChildType(expressions.get(index), 0));
    assertEquals(SuperClassA.class, getChildType(expressions.get(index), 1));
    assertEquals(SuperClassA.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) FieldAccessSuper(net.sourceforge.pmd.typeresolution.testdata.FieldAccessSuper) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) Test(org.junit.Test)

Example 63 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testFieldAccess.

@Test
public void testFieldAccess() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(FieldAccess.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//StatementExpression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // param.field = 10;
    assertEquals(Integer.TYPE, expressions.get(index).getType());
    assertEquals(Integer.TYPE, getChildType(expressions.get(index++), 0));
    // local.field = 10;
    assertEquals(Integer.TYPE, expressions.get(index).getType());
    assertEquals(Integer.TYPE, getChildType(expressions.get(index++), 0));
    // f.f.f.field = 10;
    assertEquals(Integer.TYPE, expressions.get(index).getType());
    assertEquals(Integer.TYPE, getChildType(expressions.get(index++), 0));
    // (this).f.f.field = 10;
    assertEquals(Integer.TYPE, expressions.get(index).getType());
    assertEquals(FieldAccess.class, getChildType(expressions.get(index), 0));
    assertEquals(FieldAccess.class, getChildType(expressions.get(index), 1));
    assertEquals(FieldAccess.class, getChildType(expressions.get(index), 2));
    assertEquals(Integer.TYPE, getChildType(expressions.get(index++), 3));
    // field = 10;
    assertEquals(Integer.TYPE, expressions.get(index).getType());
    assertEquals(Integer.TYPE, 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 64 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testMethodMostSpecific.

@Test
public void testMethodMostSpecific() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(MethodMostSpecific.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // String a = moreSpecific((Number) null, (AbstractCollection) 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 b = moreSpecific((Integer) null, (AbstractList) 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 c = moreSpecific((Double) null, (RoleList) 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 65 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testFullyQualifiedType.

/**
 * The type should be filled also on the ASTVariableDeclaratorId node, not
 * only on the variable name declaration.
 */
@Test
public void testFullyQualifiedType() {
    String source = "public class Foo {\n" + "    public void bar() {\n" + "        java.util.StringTokenizer st = new StringTokenizer(\"a.b.c.d\", \".\");\n" + "        while (st.hasMoreTokens()) {\n" + "            System.out.println(st.nextToken());\n" + "        }\n" + "    }\n" + "}";
    ASTCompilationUnit acu = parseAndTypeResolveForString(source, "1.5");
    List<ASTName> names = acu.findDescendantsOfType(ASTName.class);
    ASTName theStringTokenizer = null;
    for (ASTName name : names) {
        if (name.hasImageEqualTo("st.hasMoreTokens")) {
            theStringTokenizer = name;
            break;
        }
    }
    Assert.assertNotNull(theStringTokenizer);
    VariableNameDeclaration declaration = (VariableNameDeclaration) theStringTokenizer.getNameDeclaration();
    Assert.assertNotNull(declaration);
    Assert.assertEquals("java.util.StringTokenizer", declaration.getTypeImage());
    Assert.assertNotNull(declaration.getType());
    Assert.assertSame(StringTokenizer.class, declaration.getType());
    ASTVariableDeclaratorId id = (ASTVariableDeclaratorId) declaration.getNode();
    Assert.assertNotNull(id.getType());
    Assert.assertSame(StringTokenizer.class, id.getType());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) 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