Search in sources :

Example 56 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testFieldAccessNested.

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

Example 57 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testBinaryLogicalOperators.

@Test
public void testBinaryLogicalOperators() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(Operators.class);
    List<ASTExpression> expressions = convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = 'binaryLogicalOperators']]//Expression"), ASTExpression.class);
    int index = 0;
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, 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) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression) Test(org.junit.Test)

Example 58 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testNestedAllocationExpressions.

@Test
public void testNestedAllocationExpressions() {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(NestedAllocationExpressions.class);
    List<ASTAllocationExpression> allocs = acu.findDescendantsOfType(ASTAllocationExpression.class);
    assertFalse(allocs.get(0).isAnonymousClass());
    assertEquals(Thread.class, allocs.get(0).getType());
    assertTrue(allocs.get(1).isAnonymousClass());
    // FUTURE 1.8 use Class.getTypeName() instead of toString
    assertTrue(allocs.get(1).getType().toString().endsWith("NestedAllocationExpressions$1"));
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) Test(org.junit.Test)

Example 59 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testMethodThirdPhase.

@Test
public void testMethodThirdPhase() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(MethodThirdPhase.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // Exception a = vararg(10, (Number) null, (Number) null);
    assertEquals(Exception.class, expressions.get(index).getType());
    assertEquals(Exception.class, getChildType(expressions.get(index), 0));
    assertEquals(Exception.class, getChildType(expressions.get(index++), 1));
    // Exception b = vararg(10);
    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 = 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 d = mostSpecific(10, 10, 10);
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.class, getChildType(expressions.get(index), 0));
    assertEquals(String.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 60 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testEnumAnonymousInnerClass.

/**
 * See bug #1138 Anonymous inner class in enum causes NPE
 */
@Test
public void testEnumAnonymousInnerClass() {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(EnumWithAnonymousInnerClass.class);
    // try it in jshell, an enum constant with a body is compiled to an anonymous class,
    // the counter is shared with other anonymous classes of the enum
    Class<?> enumAnon = acu.getFirstDescendantOfType(ASTEnumConstant.class).getQualifiedName().getType();
    assertEquals("net.sourceforge.pmd.typeresolution.testdata.EnumWithAnonymousInnerClass$1", enumAnon.getName());
    Class<?> inner = acu.getFirstDescendantOfType(ASTAllocationExpression.class).getFirstDescendantOfType(ASTClassOrInterfaceType.class).getType();
    assertEquals("net.sourceforge.pmd.typeresolution.testdata.EnumWithAnonymousInnerClass$2", inner.getName());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) 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