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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations