Search in sources :

Example 6 with ParameterizedTypeImpl

use of org.evosuite.utils.ParameterizedTypeImpl in project evosuite by EvoSuite.

the class GenericClass method getGenericParameterizedTypeInstantiation.

/**
 * Instantiate all type parameters of a parameterized type
 *
 * @param typeMap
 * @param recursionLevel
 * @return
 * @throws ConstructionFailedException
 */
private GenericClass getGenericParameterizedTypeInstantiation(Map<TypeVariable<?>, Type> typeMap, int recursionLevel) throws ConstructionFailedException {
    // FIXME: This negatively affects coverage. Why was it added?
    // 
    // if(isClass() && !hasTypeVariables()) {
    // return this;
    // }
    List<TypeVariable<?>> typeParameters = getTypeVariables();
    Type[] parameterTypes = new Type[typeParameters.size()];
    Type ownerType = null;
    int numParam = 0;
    for (GenericClass parameterClass : getParameterClasses()) {
        logger.debug("Current parameter to instantiate", parameterClass);
        /*
			 * If the parameter is a parameterized type variable such as T extends Map<String, K extends Number>
			 * then the boundaries of the parameters of the type variable need to be respected
			 */
        if (!parameterClass.hasWildcardOrTypeVariables()) {
            logger.debug("Parameter has no wildcard or type variable");
            parameterTypes[numParam++] = parameterClass.getType();
        } else {
            logger.debug("Current parameter has type variables: " + parameterClass);
            Map<TypeVariable<?>, Type> extendedMap = new HashMap<TypeVariable<?>, Type>(typeMap);
            extendedMap.putAll(parameterClass.getTypeVariableMap());
            if (!extendedMap.containsKey(typeParameters.get(numParam)) && !parameterClass.isTypeVariable())
                extendedMap.put(typeParameters.get(numParam), parameterClass.getType());
            logger.debug("New type map: " + extendedMap);
            if (parameterClass.isWildcardType()) {
                logger.debug("Is wildcard type, here we should value the wildcard boundaries");
                logger.debug("Wildcard boundaries: " + parameterClass.getGenericBounds());
                logger.debug("Boundaries of underlying var: " + Arrays.asList(typeParameters.get(numParam).getBounds()));
                GenericClass parameterInstance = parameterClass.getGenericWildcardInstantiation(extendedMap, recursionLevel + 1);
                // if(!parameterTypeClass.isAssignableFrom(parameterInstance)) {
                if (!parameterInstance.satisfiesBoundaries(typeParameters.get(numParam))) {
                    throw new ConstructionFailedException("Invalid generic instance");
                }
                // GenericClass parameterInstance = new GenericClass(
                // typeParameters.get(numParam)).getGenericInstantiation(extendedMap,
                // recursionLevel + 1);
                parameterTypes[numParam++] = parameterInstance.getType();
            } else {
                logger.debug("Is not wildcard but type variable? " + parameterClass.isTypeVariable());
                GenericClass parameterInstance = parameterClass.getGenericInstantiation(extendedMap, recursionLevel + 1);
                parameterTypes[numParam++] = parameterInstance.getType();
            }
        }
    }
    if (hasOwnerType()) {
        GenericClass ownerClass = getOwnerType().getGenericInstantiation(typeMap, recursionLevel);
        ownerType = ownerClass.getType();
    }
    return new GenericClass(new ParameterizedTypeImpl(rawClass, parameterTypes, ownerType));
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) CaptureType(com.googlecode.gentyref.CaptureType) TypeVariable(java.lang.reflect.TypeVariable) ParameterizedTypeImpl(org.evosuite.utils.ParameterizedTypeImpl) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 7 with ParameterizedTypeImpl

use of org.evosuite.utils.ParameterizedTypeImpl in project evosuite by EvoSuite.

the class GenericUtils method replaceTypeVariable.

public static Type replaceTypeVariable(Type targetType, TypeVariable<?> variable, Type variableType) {
    if (targetType instanceof Class<?>)
        return targetType;
    else if (targetType instanceof GenericArrayType) {
        GenericArrayType gType = (GenericArrayType) targetType;
        Type componentType = replaceTypeVariable(gType.getGenericComponentType(), variable, variableType);
        return GenericArrayTypeImpl.createArrayType(componentType);
    } else if (targetType instanceof ParameterizedType) {
        ParameterizedType pType = (ParameterizedType) targetType;
        Type ownerType = null;
        if (pType.getOwnerType() != null) {
            ownerType = replaceTypeVariable(pType.getOwnerType(), variable, variableType);
        }
        Type[] originalParameterTypes = pType.getActualTypeArguments();
        Type[] parameterTypes = new Type[originalParameterTypes.length];
        for (int i = 0; i < originalParameterTypes.length; i++) {
            parameterTypes[i] = replaceTypeVariable(originalParameterTypes[i], variable, variableType);
        }
        return new ParameterizedTypeImpl((Class<?>) pType.getRawType(), parameterTypes, ownerType);
    } else if (targetType instanceof WildcardType) {
        WildcardType wType = (WildcardType) targetType;
        Type[] originalUpperBounds = wType.getUpperBounds();
        Type[] originalLowerBounds = wType.getLowerBounds();
        Type[] upperBounds = new Type[originalUpperBounds.length];
        Type[] lowerBounds = new Type[originalLowerBounds.length];
        for (int i = 0; i < originalUpperBounds.length; i++) {
            upperBounds[i] = replaceTypeVariable(originalUpperBounds[i], variable, variableType);
        }
        for (int i = 0; i < originalLowerBounds.length; i++) {
            lowerBounds[i] = replaceTypeVariable(originalLowerBounds[i], variable, variableType);
        }
        return new WildcardTypeImpl(upperBounds, lowerBounds);
    } else if (targetType instanceof TypeVariable<?>) {
        if (targetType.equals(variable)) {
            // logger.debug("Do equal: " + variable + "/" + targetType);
            return variableType;
        } else {
            // + ((TypeVariable<?>) targetType).getGenericDeclaration());
            return targetType;
        }
    } else {
        // + targetType);
        return targetType;
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedTypeImpl(org.evosuite.utils.ParameterizedTypeImpl)

Example 8 with ParameterizedTypeImpl

use of org.evosuite.utils.ParameterizedTypeImpl in project evosuite by EvoSuite.

the class EvoInvocationListenerTest method testGenerics.

@Test
public void testGenerics() {
    ParameterizedTypeImpl type = new ParameterizedTypeImpl(AGenericClass.class, new Type[] { String.class }, null);
    EvoInvocationListener listener = new EvoInvocationListener(type);
    AGenericClass<String> aGenericClass = (AGenericClass<String>) mock(AGenericClass.class, withSettings().invocationListeners(listener));
    when(aGenericClass.genericAsInput(any((Class<String>) type.getActualTypeArguments()[0]))).thenReturn(true);
    listener.activate();
    boolean b = aGenericClass.genericAsInput("foo");
    assertTrue(b);
    List<MethodDescriptor> list = listener.getCopyOfMethodDescriptors();
    Assert.assertEquals(1, list.size());
}
Also used : ParameterizedTypeImpl(org.evosuite.utils.ParameterizedTypeImpl) Test(org.junit.Test)

Aggregations

ParameterizedTypeImpl (org.evosuite.utils.ParameterizedTypeImpl)8 GenericArrayType (java.lang.reflect.GenericArrayType)7 ParameterizedType (java.lang.reflect.ParameterizedType)7 Type (java.lang.reflect.Type)7 WildcardType (java.lang.reflect.WildcardType)7 CaptureType (com.googlecode.gentyref.CaptureType)6 TypeVariable (java.lang.reflect.TypeVariable)4 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)1 Test (org.junit.Test)1