Search in sources :

Example 6 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testFieldAccessStatic.

@Test
public void testFieldAccessStatic() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(FieldAccessStatic.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//StatementExpression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // staticPrimitive = 10;
    assertEquals(Integer.TYPE, expressions.get(index).getType());
    assertEquals(Integer.TYPE, getChildType(expressions.get(index++), 0));
    // staticGeneric.first = new Long(0);
    assertEquals(Long.class, expressions.get(index).getType());
    assertEquals(Long.class, getChildType(expressions.get(index++), 0));
    // StaticMembers.staticPrimitive = 10;
    assertEquals(Integer.TYPE, expressions.get(index).getType());
    assertEquals(Integer.TYPE, getChildType(expressions.get(index++), 0));
    // net.sourceforge.pmd.typeresolution.testdata.dummytypes.StaticMembers.staticPrimitive = 10;
    assertEquals(Integer.TYPE, expressions.get(index).getType());
    assertEquals(Integer.TYPE, getChildType(expressions.get(index++), 0));
    // net.sourceforge.pmd.typeresolution.testdata.dummytypes.StaticMembers
    // .staticGeneric.generic.second = new Long(10);
    assertEquals(Long.class, expressions.get(index).getType());
    assertEquals(Long.class, getChildType(expressions.get(index++), 0));
    // staticPrimitive = "";
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.class, getChildType(expressions.get(index++), 0));
    // staticChar = 3.1; // it's a double
    assertEquals(Double.TYPE, expressions.get(index).getType());
    assertEquals(Double.TYPE, getChildType(expressions.get(index++), 0));
    // FieldAccessStatic.Nested.staticPrimitive = "";
    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 7 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testMethodTypeInference.

@Test
public void testMethodTypeInference() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(GenericMethodsImplicit.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // SuperClassA2 a = bar((SuperClassA) null, (SuperClassAOther) null, null, (SuperClassAOther2) null);
    assertEquals(SuperClassA2.class, expressions.get(index).getType());
    assertEquals(SuperClassA2.class, getChildType(expressions.get(index), 0));
    assertEquals(SuperClassA2.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 8 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testMethodStaticAccess.

@Test
public void testMethodStaticAccess() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(MethodStaticAccess.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // int a = primitiveStaticMethod();
    assertEquals(int.class, expressions.get(index).getType());
    assertEquals(int.class, getChildType(expressions.get(index), 0));
    assertEquals(int.class, getChildType(expressions.get(index++), 1));
    // StaticMembers b = staticInstanceMethod();
    assertEquals(StaticMembers.class, expressions.get(index).getType());
    assertEquals(StaticMembers.class, getChildType(expressions.get(index), 0));
    assertEquals(StaticMembers.class, getChildType(expressions.get(index++), 1));
    // int c = StaticMembers.primitiveStaticMethod();
    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 = MethodStaticAccess.Nested.primitiveStaticMethod();
    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 9 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testPrimarySimpleGenericFieldAccess.

@Test
public void testPrimarySimpleGenericFieldAccess() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(FieldAccessPrimaryGenericSimple.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//StatementExpression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // this.genericField.first = "";
    assertEquals(String.class, expressions.get(index).getType());
    assertChildTypeArgsEqualTo(expressions.get(index), 1, String.class, Double.class);
    assertEquals(String.class, getChildType(expressions.get(index++), 2));
    // (this).genericField.second = new Double(0);
    assertEquals(Double.class, expressions.get(index).getType());
    assertChildTypeArgsEqualTo(expressions.get(index), 1, String.class, Double.class);
    assertEquals(Double.class, getChildType(expressions.get(index++), 2));
    // this.genericTypeArg.second.second = new Double(0);
    assertEquals(Double.class, expressions.get(index).getType());
    assertChildTypeArgsEqualTo(expressions.get(index), 2, Number.class, Double.class);
    assertEquals(Double.class, getChildType(expressions.get(index++), 3));
    // (this).genericField.generic.generic.generic.first = new Double(0);
    assertEquals(Double.class, expressions.get(index).getType());
    assertEquals(Double.class, getChildType(expressions.get(index++), 5));
    // (this).fieldA = new Long(0);
    assertEquals(Long.class, expressions.get(index).getType());
    assertEquals(Long.class, getChildType(expressions.get(index++), 1));
    // this.fieldB.generic.second = "";
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.class, getChildType(expressions.get(index++), 3));
    // super.fieldA = new Long(0);
    assertEquals(Long.class, expressions.get(index).getType());
    assertChildTypeArgsEqualTo(expressions.get(index), 0, Long.class);
    assertEquals(Long.class, getChildType(expressions.get(index++), 1));
    // super.fieldB.generic.second = "";
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.class, getChildType(expressions.get(index++), 3));
    // this.field.first = "";
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.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 10 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testMethodSecondPhase.

@Test
public void testMethodSecondPhase() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(MethodSecondPhase.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // String a = boxing(10, "");
    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 = boxing('a', "");
    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 = boxing(10L, "");
    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 = unboxing("", (Integer) 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 e = unboxing("", (Character) 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 f = unboxing("", (Byte) 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)

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