use of net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode in project pmd by pmd.
the class ClassTypeResolverTest method testSuperExpression.
@Test
public void testSuperExpression() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(SuperExpression.class);
List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression/PrimaryPrefix"), AbstractJavaTypeNode.class);
int index = 0;
assertEquals(SuperClassA.class, expressions.get(index++).getType());
assertEquals(SuperClassA.class, expressions.get(index++).getType());
assertEquals(SuperClassA.class, expressions.get(index++).getType());
assertEquals(SuperClassA.class, expressions.get(index++).getType());
assertEquals(SuperExpression.class, ((TypeNode) expressions.get(index).jjtGetParent().jjtGetChild(0)).getType());
assertEquals(SuperClassA.class, ((TypeNode) expressions.get(index++).jjtGetParent().jjtGetChild(1)).getType());
assertEquals(SuperExpression.class, expressions.get(index++).getType());
assertEquals(SuperExpression.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.AbstractJavaTypeNode in project pmd by pmd.
the class ClassTypeResolverTest method testMethodGenericExplicit.
@Test
public void testMethodGenericExplicit() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(MethodGenericExplicit.class);
List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"), AbstractJavaTypeNode.class);
int index = 0;
// String s = this.<String>foo();
assertEquals(String.class, expressions.get(index).getType());
assertEquals(String.class, getChildType(expressions.get(index), 1));
assertEquals(String.class, getChildType(expressions.get(index++), 2));
// Make sure we got them all
assertEquals("All expressions not tested", index, expressions.size());
}
use of net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode in project pmd by pmd.
the class ClassTypeResolverJava8Test method testSuperExpression.
@Test
public void testSuperExpression() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass18(SuperExpression.class);
List<AbstractJavaTypeNode> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression/PrimaryPrefix"), AbstractJavaTypeNode.class);
int index = 0;
assertEquals(SuperClass.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.AbstractJavaTypeNode in project pmd by pmd.
the class ClassTypeResolver method visit.
@Override
public Object visit(ASTPrimaryExpression primaryNode, Object data) {
// visit method arguments in reverse
for (int i = primaryNode.jjtGetNumChildren() - 1; i >= 0; --i) {
((JavaNode) primaryNode.jjtGetChild(i)).jjtAccept(this, data);
}
JavaTypeDefinition primaryNodeType = null;
AbstractJavaTypeNode previousChild = null;
AbstractJavaTypeNode nextChild;
Class<?> accessingClass = getEnclosingTypeDeclarationClass(primaryNode);
for (int childIndex = 0; childIndex < primaryNode.jjtGetNumChildren(); ++childIndex) {
AbstractJavaTypeNode currentChild = (AbstractJavaTypeNode) primaryNode.jjtGetChild(childIndex);
nextChild = childIndex + 1 < primaryNode.jjtGetNumChildren() ? (AbstractJavaTypeNode) primaryNode.jjtGetChild(childIndex + 1) : null;
// skip children which already have their type assigned
if (currentChild.getType() == null) {
// Last token, because if 'this' is a Suffix, it'll have tokens '.' and 'this'
if (currentChild.jjtGetLastToken().toString().equals("this")) {
if (previousChild != null) {
// Qualified 'this' expression
currentChild.setTypeDefinition(previousChild.getTypeDefinition());
} else {
// simple 'this' expression
ASTClassOrInterfaceDeclaration typeDeclaration = currentChild.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
if (typeDeclaration != null) {
currentChild.setTypeDefinition(typeDeclaration.getTypeDefinition());
}
}
// Last token, because if 'super' is a Suffix, it'll have tokens '.' and 'super'
} else if (currentChild.jjtGetLastToken().toString().equals("super")) {
if (previousChild != null) {
// Qualified 'super' expression
// anonymous classes can't have qualified super expression, thus
// getSuperClassTypeDefinition's second argumet isn't null, but we are not
// looking for enclosing super types
currentChild.setTypeDefinition(getSuperClassTypeDefinition(currentChild, previousChild.getType()));
} else {
// simple 'super' expression
currentChild.setTypeDefinition(getSuperClassTypeDefinition(currentChild, null));
}
} else if (currentChild.getFirstChildOfType(ASTArguments.class) != null) {
currentChild.setTypeDefinition(previousChild.getTypeDefinition());
} else if (previousChild != null && previousChild.getType() != null) {
String currentChildImage = currentChild.getImage();
if (currentChildImage == null) {
// this.<Something>foo(); <Something>foo would be in a Suffix and would have a null image
currentChildImage = currentChild.jjtGetLastToken().toString();
}
ASTArguments astArguments = nextChild != null ? nextChild.getFirstChildOfType(ASTArguments.class) : null;
if (astArguments != null) {
// method
ASTArgumentList astArgumentList = getArgumentList(astArguments);
int methodArgsArity = getArgumentListArity(astArgumentList);
List<JavaTypeDefinition> typeArguments = getMethodExplicitTypeArugments(currentChild);
List<MethodType> methods = getApplicableMethods(previousChild.getTypeDefinition(), currentChildImage, typeArguments, methodArgsArity, accessingClass);
currentChild.setTypeDefinition(getBestMethodReturnType(previousChild.getTypeDefinition(), methods, astArgumentList));
} else {
// field
currentChild.setTypeDefinition(getFieldType(previousChild.getTypeDefinition(), currentChildImage, accessingClass));
}
}
}
if (currentChild.getType() != null) {
primaryNodeType = currentChild.getTypeDefinition();
} else {
// avoid falsely passing tests
primaryNodeType = null;
break;
}
previousChild = currentChild;
}
primaryNode.setTypeDefinition(primaryNodeType);
return data;
}
use of net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode 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());
}
Aggregations