use of net.sourceforge.pmd.lang.java.ast.TypeNode in project pmd by pmd.
the class ClassTypeResolver method visit.
@Override
public Object visit(ASTName node, Object data) {
Class<?> accessingClass = getEnclosingTypeDeclarationClass(node);
String[] dotSplitImage = node.getImage().split("\\.");
int startIndex = searchNodeNameForClass(node);
ASTArguments astArguments = getSuffixMethodArgs(node);
ASTArgumentList astArgumentList = getArgumentList(astArguments);
int methodArgsArity = getArgumentListArity(astArgumentList);
JavaTypeDefinition previousType;
if (node.getType() != null) {
// static field or method
// node.getType() has been set by the call to searchNodeNameForClass above
// node.getType() will have the value equal to the Class found by that method
previousType = node.getTypeDefinition();
} else {
// non-static field or method
if (dotSplitImage.length == 1 && astArguments != null) {
// method
List<MethodType> methods = getLocalApplicableMethods(node, dotSplitImage[0], Collections.<JavaTypeDefinition>emptyList(), methodArgsArity, accessingClass);
TypeNode enclosingType = getEnclosingTypeDeclaration(node);
if (enclosingType == null) {
// we can't proceed, probably uncompiled sources
return data;
}
previousType = getBestMethodReturnType(enclosingType.getTypeDefinition(), methods, astArgumentList);
} else {
// field
previousType = getTypeDefinitionOfVariableFromScope(node.getScope(), dotSplitImage[0], accessingClass);
}
// first element's type in dotSplitImage has already been resolved
startIndex = 1;
}
// as the code is not compiled there and symbol table works on uncompiled code
if (node.getNameDeclaration() != null && // if it's not null, then let other code handle things
previousType == null && node.getNameDeclaration().getNode() instanceof TypeNode) {
// Carry over the type from the declaration
Class<?> nodeType = ((TypeNode) node.getNameDeclaration().getNode()).getType();
// FIXME : generic classes and class with generic super types could have the wrong type assigned here
if (nodeType != null) {
node.setType(nodeType);
return super.visit(node, data);
}
}
for (int i = startIndex; i < dotSplitImage.length; ++i) {
if (previousType == null) {
break;
}
if (i == dotSplitImage.length - 1 && astArguments != null) {
// method
List<MethodType> methods = getApplicableMethods(previousType, dotSplitImage[i], Collections.<JavaTypeDefinition>emptyList(), methodArgsArity, accessingClass);
previousType = getBestMethodReturnType(previousType, methods, astArgumentList);
} else {
// field
previousType = getFieldType(previousType, dotSplitImage[i], accessingClass);
}
}
if (previousType != null) {
node.setTypeDefinition(previousType);
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.TypeNode in project pmd by pmd.
the class ClassTypeResolver method visit.
@Override
public Object visit(ASTAllocationExpression node, Object data) {
super.visit(node, data);
final ASTArrayDimsAndInits dims = node.getFirstChildOfType(ASTArrayDimsAndInits.class);
if (dims != null) {
final Class<?> arrayType = ((TypeNode) node.jjtGetChild(0)).getType();
if (arrayType != null) {
node.setType(Array.newInstance(arrayType, (int[]) Array.newInstance(int.class, dims.getArrayDepth())).getClass());
}
} else {
rollupTypeUnary(node);
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.TypeNode in project pmd by pmd.
the class ClassTypeResolver method visit.
@Override
public Object visit(ASTTypeParameters node, Object data) {
super.visit(node, data);
if (node.jjtGetParent() instanceof ASTClassOrInterfaceDeclaration) {
TypeNode parent = (TypeNode) node.jjtGetParent();
final JavaTypeDefinition[] boundGenerics = new JavaTypeDefinition[node.jjtGetNumChildren()];
for (int i = 0; i < node.jjtGetNumChildren(); ++i) {
boundGenerics[i] = ((TypeNode) node.jjtGetChild(i)).getTypeDefinition();
}
parent.setTypeDefinition(JavaTypeDefinition.forClass(parent.getType(), boundGenerics));
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.TypeNode 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.TypeNode in project pmd by pmd.
the class ClassTypeResolverTest method testNestedAnonymousClass.
@Test
public void testNestedAnonymousClass() throws Exception {
Node acu = parseAndTypeResolveForClass(NestedAnonymousClass.class, "1.8");
ASTAllocationExpression allocationExpression = acu.getFirstDescendantOfType(ASTAllocationExpression.class);
ASTAllocationExpression nestedAllocation = // get the declaration (boundary)
allocationExpression.getFirstDescendantOfType(ASTClassOrInterfaceBodyDeclaration.class).getFirstDescendantOfType(// and dive for the nested allocation
ASTAllocationExpression.class);
TypeNode child = (TypeNode) nestedAllocation.jjtGetChild(0);
Assert.assertTrue(Converter.class.isAssignableFrom(child.getType()));
Assert.assertSame(String.class, child.getTypeDefinition().getGenericType(0).getType());
}
Aggregations