Example usage for java.lang.reflect TypeVariable getBounds

List of usage examples for java.lang.reflect TypeVariable getBounds

Introduction

In this page you can find the example usage for java.lang.reflect TypeVariable getBounds.

Prototype

Type[] getBounds();

Source Link

Document

Returns an array of Type objects representing the upper bound(s) of this type variable.

Usage

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

private void determineVariableFromParameter(VariableReference parameter, Type parameterType,
        Map<TypeVariable<?>, Type> typeMap) {
    Map<TypeVariable<?>, Type> parameterTypeMap = getParameterType(parameterType, parameter.getType());
    logger.info("Resulting map: " + parameterTypeMap);
    for (TypeVariable<?> typeVar : parameterTypeMap.keySet()) {
        Type actualType = parameterTypeMap.get(typeVar);
        if (typeMap.containsKey(typeVar)) {
            logger.info("Variable is in map: " + typeVar);
            Type currentType = typeMap.get(typeVar);
            if (currentType == null || TypeUtils.isAssignable(actualType, currentType)) {
                typeMap.put(typeVar, actualType);
            } else {
                logger.info("Not assignable: " + typeVar + " with bounds " + Arrays.asList(typeVar.getBounds())
                        + " and current type " + currentType + " from " + actualType);
                logger.info("" + GenericTypeReflector.isSuperType(currentType, actualType));
                logger.info("" + TypeUtils.isAssignable(actualType, typeVar));
            }/*w ww.j  a  v  a2s  .  com*/
        } else {
            logger.debug("Variable is not in map: " + typeVar);
            typeMap.put(typeVar, actualType);
        }
    }
}

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

public static boolean isAssignable(Type type, TypeVariable<?> typeVariable) {
    boolean isAssignable = true;
    for (Type boundType : typeVariable.getBounds()) {
        // Have to resolve the type because a typevariable may have a reference to itself
        // in its bounds
        Type resolvedBoundType = GenericUtils.replaceTypeVariable(boundType, typeVariable, type);
        if (!GenericClass.isAssignable(resolvedBoundType, type)) {
            isAssignable = false;/*from w  w  w .ja  va2 s. co m*/
            break;
        }
    }
    return isAssignable;
}

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

public static Type replaceTypeVariablesWithWildcards(Type targetType) {
    if (targetType instanceof TypeVariable) {
        TypeVariable<?> typeVariable = (TypeVariable<?>) targetType;
        return new WildcardTypeImpl(typeVariable.getBounds(), new Type[] {});
    } else if (targetType instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) targetType;
        Type owner = null;//from   w  ww.  j ava2s  .c  om
        if (parameterizedType.getOwnerType() != null)
            owner = replaceTypeVariablesWithWildcards(parameterizedType.getOwnerType());
        Type[] currentParameters = parameterizedType.getActualTypeArguments();
        Type[] parameters = new Type[currentParameters.length];
        for (int i = 0; i < parameters.length; i++) {
            parameters[i] = replaceTypeVariablesWithWildcards(currentParameters[i]);
        }
        return new ParameterizedTypeImpl((Class<?>) parameterizedType.getRawType(), parameters, owner);
    }
    return targetType;
}

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

/**
 * Determine whether the boundaries of the type variable are satisfied by
 * this class//from  ww  w. j  a  v  a2 s .  co  m
 * 
 * @param typeVariable
 * @return
 */
public boolean satisfiesBoundaries(TypeVariable<?> typeVariable, Map<TypeVariable<?>, Type> typeMap) {
    boolean isAssignable = true;
    logger.debug("Checking class: {} against type variable {} with map {}", type, typeVariable, typeMap);
    Map<TypeVariable<?>, Type> ownerVariableMap = getTypeVariableMap();
    for (Type bound : typeVariable.getBounds()) {
        if (bound instanceof ParameterizedType) {
            Class<?> boundClass = GenericTypeReflector.erase(bound);
            if (boundClass.isAssignableFrom(rawClass)) {
                Map<TypeVariable<?>, Type> xmap = TypeUtils.determineTypeArguments(rawClass,
                        (ParameterizedType) bound);
                ownerVariableMap.putAll(xmap);
            }
        }
    }
    ownerVariableMap.putAll(typeMap);
    boolean changed = true;
    while (changed) {
        changed = false;
        for (TypeVariable<?> var : ownerVariableMap.keySet()) {
            logger.debug("Type var: {} of {}", var, var.getGenericDeclaration());
            if (ownerVariableMap.get(var) instanceof TypeVariable<?>) {
                logger.debug("Is set to type var: {} of {}", ownerVariableMap.get(var),
                        ((TypeVariable<?>) ownerVariableMap.get(var)).getGenericDeclaration());
                TypeVariable<?> value = (TypeVariable<?>) ownerVariableMap.get(var);
                if (ownerVariableMap.containsKey(value)) {
                    logger.debug("Replacing {} with {}", var, ownerVariableMap.get(value));
                    ownerVariableMap.put(var, ownerVariableMap.get(value));
                    changed = true;
                } else {
                    logger.debug("Not in map: {}", value);
                }
            } else {
                logger.debug("Is set to concrete type: {}", ownerVariableMap.get(var));
            }
        }
        logger.debug("Current iteration of map: {}", ownerVariableMap);
    }

    GenericClass concreteClass = new GenericClass(GenericUtils.replaceTypeVariables(type, ownerVariableMap));
    logger.debug("Concrete class after variable replacement: {}", concreteClass);

    for (Type theType : typeVariable.getBounds()) {
        logger.debug("Current boundary of {}: {}", typeVariable, theType);
        // Special case: Enum is defined as Enum<T extends Enum>
        if (GenericTypeReflector.erase(theType).equals(Enum.class)) {
            logger.debug("Is ENUM case");
            // if this is an enum then it's ok. 
            if (isEnum()) {
                logger.debug("Class {} is an enum!", toString());

                continue;
            } else {
                // If it's not an enum, it cannot be assignable to enum!
                logger.debug("Class {} is not an enum.", toString());
                isAssignable = false;
                break;
            }
        }

        Type boundType = GenericUtils.replaceTypeVariables(theType, ownerVariableMap);
        boundType = GenericUtils.replaceTypeVariable(boundType, typeVariable, getType());
        boundType = GenericUtils.replaceTypeVariablesWithWildcards(boundType);

        logger.debug("Bound after variable replacement: {}", boundType);
        if (!concreteClass.isAssignableTo(boundType)) {
            logger.debug("Not assignable: {} and {}", type, boundType);
            // If the boundary is not assignable it may still be possible 
            // to instantiate the generic to an assignable type
            if (GenericTypeReflector.erase(boundType).isAssignableFrom(getRawClass())) {
                logger.debug("Raw classes are assignable: {}, {}", boundType, getRawClass());
                Type instanceType = GenericTypeReflector.getExactSuperType(boundType, getRawClass());
                if (instanceType == null) {
                    // This happens when the raw class is not a supertype 
                    // of the boundary
                    logger.debug("Instance type is null");
                    isAssignable = false;
                    break;
                }
                GenericClass instanceClass = new GenericClass(instanceType, getRawClass());

                logger.debug("Instance type is {}", instanceType);
                if (instanceClass.hasTypeVariables())
                    logger.debug("Instance type has type variables");
                if (instanceClass.hasWildcardTypes())
                    logger.debug("Instance type has wildcard variables");

                boundType = GenericUtils.replaceTypeVariable(theType, typeVariable, instanceType);
                logger.debug("Instance type after replacement is {}", boundType);
                if (GenericClass.isAssignable(boundType, instanceType)) {
                    logger.debug("Found assignable generic exact type: {}", instanceType);
                    continue;
                } else {
                    logger.debug("Is not assignable: {} and {}", boundType, instanceType);
                }
            }
            isAssignable = false;
            break;
        }
    }
    logger.debug("Result: is assignable {}", isAssignable);
    return isAssignable;
}

From source file:org.romaframework.core.schema.SchemaHelper.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Class<?> resolveClassFromType(Type type, ParameterizedType params) {
    if (type instanceof Class<?>)
        return (Class<?>) type;
    if (type instanceof ParameterizedType) {
        return resolveClassFromType(((ParameterizedType) type).getRawType(), (ParameterizedType) type);
    }//w w w .  j  av a 2s  . c om
    if (type instanceof GenericArrayType) {
        GenericArrayType gat = (GenericArrayType) type;
        Class<?> arrItemp = resolveClassFromType(gat.getGenericComponentType(), null);
        return Array.newInstance(arrItemp, 0).getClass();
    }
    if (type instanceof TypeVariable<?>) {
        TypeVariable<?> t = (TypeVariable<?>) type;
        if (params != null) {
            Class<?> cl = resolveClassFromType(params.getRawType(), null);
            if (cl != null) {

                TypeVariable<Class<?>>[] var = ((Class) cl).getTypeParameters();
                int i = 0;
                for (; i < var.length; i++) {
                    if (var[i].getName().equals(t.getName())) {
                        return resolveClassFromType(params.getActualTypeArguments()[i],
                                resolveParameterizedType(params.getOwnerType()));
                    }
                }
            }
        }
        Type[] bounds = t.getBounds();
        if (bounds.length == 1)
            return resolveClassFromType(bounds[0], params);
    }
    if (type instanceof WildcardType) {
        // TODO:
    }
    return null;
}

From source file:org.soybeanMilk.SbmUtils.java

/**
 * //from   w ww  .  j a  v a2  s .  com
 * @param type
 * @param variableTypesMap
 * @return
 * @date 2012-5-14
 */
private static Type reifyInner(Type type, Map<TypeVariable<?>, Type> variableTypesMap) {
    Type result = null;

    if (type instanceof Class<?>) {
        result = type;
    } else if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;

        Type[] at = pt.getActualTypeArguments();
        Type[] cat = new Type[at.length];

        //pt?pt??
        boolean reified = true;
        for (int i = 0; i < at.length; i++) {
            cat[i] = reifyInner(at[i], variableTypesMap);

            if (cat[i] != at[i])
                reified = false;
        }

        if (reified)
            result = pt;
        else
            result = new CustomParameterizedType(pt.getRawType(), pt.getOwnerType(), cat);
    } else if (type instanceof GenericArrayType) {
        GenericArrayType gap = (GenericArrayType) type;

        Type ct = gap.getGenericComponentType();
        Type cct = reifyInner(ct, variableTypesMap);

        if (cct == ct)
            result = gap;
        else
            result = new CustomGenericArrayType(cct);
    } else if (type instanceof TypeVariable<?>) {
        TypeVariable<?> tv = (TypeVariable<?>) type;

        if (variableTypesMap != null)
            result = variableTypesMap.get(tv);

        if (result == null) {
            Type[] bounds = tv.getBounds();

            if (bounds == null || bounds.length == 0)
                result = Object.class;
            else
                result = bounds[0];
        }

        result = reifyInner(result, variableTypesMap);
    } else if (type instanceof WildcardType) {
        WildcardType wt = (WildcardType) type;

        Type[] upperBounds = wt.getUpperBounds();

        Type upperType = (upperBounds != null && upperBounds.length > 0 ? upperBounds[0] : null);

        if (upperType == null)
            upperType = Object.class;

        result = reifyInner(upperType, variableTypesMap);
    } else
        result = type;

    return result;
}

From source file:org.springframework.core.GenericTypeResolver.java

/**
 * Extracts the bound {@code Type} for a given {@link TypeVariable}.
 *///  w w  w. j ava  2s. c  o  m
static Type extractBoundForTypeVariable(TypeVariable typeVariable) {
    Type[] bounds = typeVariable.getBounds();
    if (bounds.length == 0) {
        return Object.class;
    }
    Type bound = bounds[0];
    if (bound instanceof TypeVariable) {
        bound = extractBoundForTypeVariable((TypeVariable) bound);
    }
    return bound;
}