Search in sources :

Example 41 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testSimpleGenericFieldAccess.

@Test
public void testSimpleGenericFieldAccess() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(FieldAccessGenericSimple.class);
    List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//StatementExpression/PrimaryExpression"), AbstractJavaTypeNode.class);
    int index = 0;
    // genericField.first = "";
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.class, getChildType(expressions.get(index++), 0));
    // genericField.second = new Double(0);
    assertEquals(Double.class, expressions.get(index).getType());
    assertEquals(Double.class, getChildType(expressions.get(index++), 0));
    // genericTypeArg.second.second = new Double(0);
    assertEquals(Double.class, expressions.get(index).getType());
    assertEquals(Double.class, getChildType(expressions.get(index++), 0));
    // param.first = new Integer(0);
    assertEquals(Integer.class, expressions.get(index).getType());
    assertEquals(Integer.class, getChildType(expressions.get(index++), 0));
    // local.second = new Long(0);
    assertEquals(Long.class, expressions.get(index).getType());
    assertEquals(Long.class, getChildType(expressions.get(index++), 0));
    // param.generic.first = new Character('c');
    assertEquals(Character.class, expressions.get(index).getType());
    assertEquals(Character.class, getChildType(expressions.get(index++), 0));
    // local.generic.second = new Float(0);
    assertEquals(Float.class, expressions.get(index).getType());
    assertEquals(Float.class, getChildType(expressions.get(index++), 0));
    // genericField.generic.generic.generic.first = new Double(0);
    assertEquals(Double.class, expressions.get(index).getType());
    assertEquals(Double.class, getChildType(expressions.get(index++), 0));
    // fieldA = new Long(0);
    assertEquals(Long.class, expressions.get(index).getType());
    assertEquals(Long.class, getChildType(expressions.get(index++), 0));
    // fieldB.generic.second = "";
    assertEquals(String.class, expressions.get(index).getType());
    assertEquals(String.class, getChildType(expressions.get(index++), 0));
    // fieldAcc.fieldA = new Long(0);
    assertEquals(Long.class, expressions.get(index).getType());
    assertEquals(Long.class, getChildType(expressions.get(index++), 0));
    // fieldA = new Long(0);
    assertEquals(Long.class, expressions.get(index).getType());
    assertEquals(Long.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 42 with ASTCompilationUnit

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

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

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

the class ClassTypeResolverTest method testUnaryNumericOperators.

@Test
public void testUnaryNumericOperators() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(Operators.class);
    List<TypeNode> expressions = new ArrayList<>();
    expressions.addAll(convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = 'unaryNumericOperators']]//Expression"), TypeNode.class));
    expressions.addAll(convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = " + "'unaryNumericOperators']]//PostfixExpression"), TypeNode.class));
    expressions.addAll(convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = " + "'unaryNumericOperators']]//PreIncrementExpression"), TypeNode.class));
    expressions.addAll(convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = " + "'unaryNumericOperators']]//PreDecrementExpression"), TypeNode.class));
    int index = 0;
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    assertEquals(Double.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) ArrayList(java.util.ArrayList) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) Test(org.junit.Test)

Example 45 with ASTCompilationUnit

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

the class ClassTypeResolverTest method testThisExpression.

@Test
public void testThisExpression() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(ThisExpression.class);
    List<ASTPrimaryExpression> expressions = convertList(acu.findChildNodesWithXPath("//PrimaryExpression"), ASTPrimaryExpression.class);
    List<ASTPrimaryPrefix> prefixes = convertList(acu.findChildNodesWithXPath("//PrimaryPrefix"), ASTPrimaryPrefix.class);
    int index = 0;
    assertEquals(ThisExpression.class, expressions.get(index).getType());
    assertEquals(ThisExpression.class, prefixes.get(index++).getType());
    assertEquals(ThisExpression.class, expressions.get(index).getType());
    assertEquals(ThisExpression.class, prefixes.get(index++).getType());
    assertEquals(ThisExpression.class, expressions.get(index).getType());
    assertEquals(ThisExpression.class, prefixes.get(index++).getType());
    assertEquals(ThisExpression.class, expressions.get(index).getType());
    assertEquals(ThisExpression.class, prefixes.get(index++).getType());
    assertEquals(ThisExpression.ThisExprNested.class, expressions.get(index).getType());
    assertEquals(ThisExpression.ThisExprNested.class, prefixes.get(index++).getType());
    // Qualified this
    assertEquals(ThisExpression.class, expressions.get(index).getType());
    assertEquals(ThisExpression.class, prefixes.get(index).getType());
    assertEquals(ThisExpression.class, ((TypeNode) expressions.get(index++).jjtGetChild(1)).getType());
    assertEquals(ThisExpression.ThisExprStaticNested.class, expressions.get(index).getType());
    assertEquals(ThisExpression.ThisExprStaticNested.class, prefixes.get(index++).getType());
    // Make sure we got them all
    assertEquals("All expressions not tested", index, expressions.size());
    assertEquals("All expressions not tested", index, prefixes.size());
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ThisExpression(net.sourceforge.pmd.typeresolution.testdata.ThisExpression) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) 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