Example usage for org.apache.commons.lang3.reflect TypeUtils getImplicitUpperBounds

List of usage examples for org.apache.commons.lang3.reflect TypeUtils getImplicitUpperBounds

Introduction

In this page you can find the example usage for org.apache.commons.lang3.reflect TypeUtils getImplicitUpperBounds.

Prototype

public static Type[] getImplicitUpperBounds(final WildcardType wildcardType) 

Source Link

Document

Returns an array containing the sole value of Object if WildcardType#getUpperBounds() returns an empty array.

Usage

From source file:org.evosuite.utils.generic.GenericClass.java

/**
 * If this is a LinkedList<?> and the super class is a List<Integer> then
 * this returns a LinkedList<Integer>
 * /*  ww w  . j  a  v  a  2 s . c o  m*/
 * @param superClass
 * @return
 * @throws ConstructionFailedException
 */
public GenericClass getWithParametersFromSuperclass(GenericClass superClass)
        throws ConstructionFailedException {
    GenericClass exactClass = new GenericClass(type);
    if (!(type instanceof ParameterizedType)) {
        exactClass.type = type;
        return exactClass;
    }
    ParameterizedType pType = (ParameterizedType) type;

    if (superClass.isParameterizedType()) {
        Map<TypeVariable<?>, Type> typeMap = TypeUtils.determineTypeArguments(rawClass,
                (ParameterizedType) superClass.getType());
        return getGenericInstantiation(typeMap);
    }

    Class<?> targetClass = superClass.getRawClass();
    Class<?> currentClass = rawClass;
    Type[] parameterTypes = new Type[superClass.getNumParameters()];
    superClass.getParameterTypes().toArray(parameterTypes);

    if (targetClass.equals(currentClass)) {
        logger.info("Raw classes match, setting parameters to: " + superClass.getParameterTypes());
        exactClass.type = new ParameterizedTypeImpl(currentClass, parameterTypes, pType.getOwnerType());
    } else {
        Type ownerType = pType.getOwnerType();
        Map<TypeVariable<?>, Type> superTypeMap = superClass.getTypeVariableMap();
        Type[] origArguments = pType.getActualTypeArguments();
        Type[] arguments = new Type[origArguments.length];
        // For some reason, doing this would lead to arguments being
        // of component type TypeVariable, which would lead to
        // ArrayStoreException if we try to assign a WildcardType
        //Type[] arguments = Arrays.copyOf(origArguments, origArguments.length);
        for (int i = 0; i < origArguments.length; i++)
            arguments[i] = origArguments[i];
        List<TypeVariable<?>> variables = getTypeVariables();
        for (int i = 0; i < arguments.length; i++) {
            TypeVariable<?> var = variables.get(i);
            if (superTypeMap.containsKey(var)) {
                arguments[i] = superTypeMap.get(var);
                logger.info("Setting type variable " + var + " to " + superTypeMap.get(var));
            } else if (arguments[i] instanceof WildcardType && i < parameterTypes.length) {
                logger.info("Replacing wildcard with " + parameterTypes[i]);
                logger.info("Lower Bounds: "
                        + Arrays.asList(TypeUtils.getImplicitLowerBounds((WildcardType) arguments[i])));
                logger.info("Upper Bounds: "
                        + Arrays.asList(TypeUtils.getImplicitUpperBounds((WildcardType) arguments[i])));
                logger.info("Type variable: " + variables.get(i));
                if (!TypeUtils.isAssignable(parameterTypes[i], arguments[i])) {
                    logger.info("Not assignable to bounds!");
                    return null;
                } else {
                    boolean assignable = false;
                    for (Type bound : variables.get(i).getBounds()) {
                        if (TypeUtils.isAssignable(parameterTypes[i], bound)) {
                            assignable = true;
                            break;
                        }
                    }
                    if (!assignable) {
                        logger.info("Not assignable to type variable!");
                        return null;
                    }
                }
                arguments[i] = parameterTypes[i];
            }
        }
        GenericClass ownerClass = new GenericClass(ownerType).getWithParametersFromSuperclass(superClass);
        if (ownerClass == null)
            return null;
        exactClass.type = new ParameterizedTypeImpl(currentClass, arguments, ownerClass.getType());
    }

    return exactClass;
}

From source file:org.evosuite.utils.GenericClass.java

/**
 * If this is a LinkedList<?> and the super class is a List<Integer> then
 * this returns a LinkedList<Integer>
 * // w w w  .  java2  s .c  o m
 * @param superClass
 * @return
 * @throws ConstructionFailedException
 */
public GenericClass getWithParametersFromSuperclass(GenericClass superClass)
        throws ConstructionFailedException {
    GenericClass exactClass = new GenericClass(type);
    if (!(type instanceof ParameterizedType)) {
        exactClass.type = type;
        return exactClass;
    }
    ParameterizedType pType = (ParameterizedType) type;

    if (superClass.isParameterizedType()) {
        Map<TypeVariable<?>, Type> typeMap = TypeUtils.determineTypeArguments(rawClass,
                (ParameterizedType) superClass.getType());
        return getGenericInstantiation(typeMap);
    }

    Class<?> targetClass = superClass.getRawClass();
    Class<?> currentClass = rawClass;
    Type[] parameterTypes = new Type[superClass.getNumParameters()];
    superClass.getParameterTypes().toArray(parameterTypes);

    if (targetClass.equals(currentClass)) {
        logger.info("Raw classes match, setting parameters to: {}", superClass.getParameterTypes());
        exactClass.type = new ParameterizedTypeImpl(currentClass, parameterTypes, pType.getOwnerType());
    } else {
        Type ownerType = pType.getOwnerType();
        Map<TypeVariable<?>, Type> superTypeMap = superClass.getTypeVariableMap();
        Type[] origArguments = pType.getActualTypeArguments();
        Type[] arguments = Arrays.copyOf(origArguments, origArguments.length);
        List<TypeVariable<?>> variables = getTypeVariables();
        for (int i = 0; i < arguments.length; i++) {
            TypeVariable<?> var = variables.get(i);
            if (superTypeMap.containsKey(var)) {
                arguments[i] = superTypeMap.get(var);
                logger.info("Setting type variable {} to {}", var, superTypeMap.get(var));
            } else if (arguments[i] instanceof WildcardType && i < parameterTypes.length) {
                logger.info("Replacing wildcard with {}", parameterTypes[i]);
                logger.info("Lower Bounds: {}",
                        Arrays.asList(TypeUtils.getImplicitLowerBounds((WildcardType) arguments[i])));
                logger.info("Upper Bounds: {}",
                        Arrays.asList(TypeUtils.getImplicitUpperBounds((WildcardType) arguments[i])));
                logger.info("Type variable: {}", variables.get(i));
                if (!TypeUtils.isAssignable(parameterTypes[i], arguments[i])) {
                    logger.info("Not assignable to bounds!");
                    return null;
                } else {
                    boolean assignable = false;
                    for (Type bound : variables.get(i).getBounds()) {
                        if (TypeUtils.isAssignable(parameterTypes[i], bound)) {
                            assignable = true;
                            break;
                        }
                    }
                    if (!assignable) {
                        logger.info("Not assignable to type variable!");
                        return null;
                    }
                }
                arguments[i] = parameterTypes[i];
            }
        }
        GenericClass ownerClass = new GenericClass(ownerType).getWithParametersFromSuperclass(superClass);
        if (ownerClass == null)
            return null;
        exactClass.type = new ParameterizedTypeImpl(currentClass, arguments, ownerClass.getType());
    }

    return exactClass;
}