Search in sources :

Example 1 with ASTTypeParameters

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

the class ClassScope method resolveGenericType.

/**
 * Tries to resolve a given typeImage as a generic Type. If the Generic Type
 * is found, any defined ClassOrInterfaceType below this type declaration is
 * used (this is typically a type bound, e.g. {@code <T extends List>}.
 *
 * @param argument
 *            the node, from where to start searching.
 * @param typeImage
 *            the type as string
 * @return the resolved class or <code>null</code> if nothing was found.
 */
private Class<?> resolveGenericType(Node argument, String typeImage) {
    List<ASTTypeParameter> types = new ArrayList<>();
    // first search only within the same method
    ASTClassOrInterfaceBodyDeclaration firstParentOfType = argument.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class);
    if (firstParentOfType != null) {
        types.addAll(firstParentOfType.findDescendantsOfType(ASTTypeParameter.class));
    }
    // then search class level types, from inner-most to outer-most
    List<ASTClassOrInterfaceDeclaration> enclosingClasses = argument.getParentsOfType(ASTClassOrInterfaceDeclaration.class);
    for (ASTClassOrInterfaceDeclaration enclosing : enclosingClasses) {
        ASTTypeParameters classLevelTypeParameters = enclosing.getFirstChildOfType(ASTTypeParameters.class);
        if (classLevelTypeParameters != null) {
            types.addAll(classLevelTypeParameters.findDescendantsOfType(ASTTypeParameter.class));
        }
    }
    return resolveGenericType(typeImage, types);
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ArrayList(java.util.ArrayList) ASTClassOrInterfaceBodyDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration) ASTTypeParameter(net.sourceforge.pmd.lang.java.ast.ASTTypeParameter) ASTTypeParameters(net.sourceforge.pmd.lang.java.ast.ASTTypeParameters)

Aggregations

ArrayList (java.util.ArrayList)1 ASTClassOrInterfaceBodyDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration)1 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)1 ASTTypeParameter (net.sourceforge.pmd.lang.java.ast.ASTTypeParameter)1 ASTTypeParameters (net.sourceforge.pmd.lang.java.ast.ASTTypeParameters)1