use of net.sourceforge.pmd.lang.java.ast.TypeNode in project pmd by pmd.
the class ClassTypeResolverTest method testAnonymousClassFromInterface.
@Test
public void testAnonymousClassFromInterface() throws Exception {
Node acu = parseAndTypeResolveForClass(AnonymousClassFromInterface.class, "1.8");
ASTAllocationExpression allocationExpression = acu.getFirstDescendantOfType(ASTAllocationExpression.class);
TypeNode child = (TypeNode) allocationExpression.jjtGetChild(0);
Assert.assertTrue(Comparator.class.isAssignableFrom(child.getType()));
Assert.assertSame(Integer.class, child.getTypeDefinition().getGenericType(0).getType());
}
use of net.sourceforge.pmd.lang.java.ast.TypeNode in project pmd by pmd.
the class ClassTypeResolverTest method testAnonymousExtendingObject.
@Test
public void testAnonymousExtendingObject() throws Exception {
Node acu = parseAndTypeResolveForClass(AnoymousExtendingObject.class, "1.8");
ASTAllocationExpression allocationExpression = acu.getFirstDescendantOfType(ASTAllocationExpression.class);
TypeNode child = (TypeNode) allocationExpression.jjtGetChild(0);
Assert.assertTrue(Object.class.isAssignableFrom(child.getType()));
}
use of net.sourceforge.pmd.lang.java.ast.TypeNode in project pmd by pmd.
the class ClassTypeResolver method getEnclosingTypeDeclaration.
/**
* Returns the the first Class declaration around the node.
*
* @param node The node with the enclosing Class declaration.
* @return The JavaTypeDefinition of the enclosing Class declaration.
*/
private TypeNode getEnclosingTypeDeclaration(Node node) {
Node previousNode = null;
while (node != null) {
if (node instanceof ASTClassOrInterfaceDeclaration) {
return (TypeNode) node;
// anonymous class declaration
} else if (// is anonymous class declaration
node instanceof ASTAllocationExpression && // array cant be anonymous
node.getFirstChildOfType(ASTArrayDimsAndInits.class) == null && !(previousNode instanceof ASTArguments)) {
// we might come out of the constructor
return (TypeNode) node;
}
previousNode = node;
node = node.jjtGetParent();
}
return null;
}
use of net.sourceforge.pmd.lang.java.ast.TypeNode in project pmd by pmd.
the class ClassTypeResolver method visit.
@Override
public Object visit(ASTWildcardBounds node, Object data) {
super.visit(node, data);
JavaTypeDefinition childType = ((TypeNode) node.jjtGetChild(0)).getTypeDefinition();
if (node.jjtGetFirstToken().toString().equals("super")) {
node.setTypeDefinition(JavaTypeDefinition.forClass(LOWER_WILDCARD, childType));
} else {
// equals "extends"
node.setTypeDefinition(JavaTypeDefinition.forClass(UPPER_WILDCARD, childType));
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.TypeNode in project pmd by pmd.
the class ClassTypeResolver method getTypeDefinitionOfVariableFromScope.
/**
* Search for a field by it's image stating from a scope and taking into account if it's visible from the
* accessingClass Class. The method takes into account that Nested inherited fields shadow outer scope fields.
*
* @param scope The scope to start the search from.
* @param image The name of the field, local variable or method parameter.
* @param accessingClass The Class (which is defined in the current ACU) that is trying to access the field.
* @return Type def. of the field, or null if it could not be resolved.
*/
private JavaTypeDefinition getTypeDefinitionOfVariableFromScope(Scope scope, String image, Class<?> accessingClass) {
if (accessingClass == null) {
return null;
}
for (; /* empty */
scope != null; scope = scope.getParent()) {
// search each enclosing scope one by one
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : scope.getDeclarations(VariableNameDeclaration.class).entrySet()) {
if (entry.getKey().getImage().equals(image)) {
ASTType typeNode = entry.getKey().getDeclaratorId().getTypeNode();
if (typeNode == null) {
// TODO : Type is infered, ie, this is a lambda such as (var) -> var.equals(other)
return null;
}
if (typeNode.jjtGetChild(0) instanceof ASTReferenceType) {
return ((TypeNode) typeNode.jjtGetChild(0)).getTypeDefinition();
} else {
// primitive type
return JavaTypeDefinition.forClass(typeNode.getType());
}
}
}
// Nested class' inherited fields shadow enclosing variables
if (scope instanceof ClassScope) {
try {
// get the superclass type def. ot the Class the ClassScope belongs to
JavaTypeDefinition superClass = getSuperClassTypeDefinition(((ClassScope) scope).getClassDeclaration().getNode(), null);
// TODO: check if anonymous classes are class scope
// try searching this type def.
JavaTypeDefinition foundTypeDef = getFieldType(superClass, image, accessingClass);
if (foundTypeDef != null) {
// if null, then it's not an inherited field
return foundTypeDef;
}
} catch (ClassCastException ignored) {
// if there is an anonymous class, getClassDeclaration().getType() will throw
// TODO: maybe there is a better way to handle this, maybe this hides bugs
}
}
}
// will return null if not found
return searchImportedStaticFields(image);
}
Aggregations