Search in sources :

Example 1 with ParameterizedTypeImpl

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

the class GenericClass method getWithGenericParameterTypes.

public GenericClass getWithGenericParameterTypes(List<GenericClass> parameters) {
    Type[] typeArray = new Type[parameters.size()];
    for (int i = 0; i < parameters.size(); i++) {
        typeArray[i] = parameters.get(i).getType();
    }
    Type ownerType = null;
    if (type instanceof ParameterizedType) {
        ownerType = ((ParameterizedType) type).getOwnerType();
    }
    return new GenericClass(new ParameterizedTypeImpl(rawClass, typeArray, ownerType));
}
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) CaptureType(com.googlecode.gentyref.CaptureType) ParameterizedTypeImpl(org.evosuite.utils.ParameterizedTypeImpl)

Example 2 with ParameterizedTypeImpl

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

the class GenericClass method getWithParameterTypes.

public GenericClass getWithParameterTypes(List<Type> parameters) {
    Type[] typeArray = new Type[parameters.size()];
    for (int i = 0; i < parameters.size(); i++) {
        typeArray[i] = parameters.get(i);
    }
    Type ownerType = null;
    if (type instanceof ParameterizedType) {
        ownerType = ((ParameterizedType) type).getOwnerType();
    }
    return new GenericClass(new ParameterizedTypeImpl(rawClass, typeArray, ownerType));
}
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) CaptureType(com.googlecode.gentyref.CaptureType) ParameterizedTypeImpl(org.evosuite.utils.ParameterizedTypeImpl)

Example 3 with ParameterizedTypeImpl

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

the class GenericClass method getWithParametersFromSuperclass.

/**
 * If this is a LinkedList<?> and the super class is a List<Integer> then
 * this returns a LinkedList<Integer>
 *
 * @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];
        // 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;
}
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) CaptureType(com.googlecode.gentyref.CaptureType) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) ParameterizedTypeImpl(org.evosuite.utils.ParameterizedTypeImpl)

Example 4 with ParameterizedTypeImpl

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

the class GenericClass method readObject.

/**
 * De-serialize. Need to use current classloader.
 *
 * @param ois
 * @throws ClassNotFoundException
 * @throws IOException
 */
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
    String name = (String) ois.readObject();
    if (name == null) {
        this.rawClass = null;
        this.type = null;
        return;
    }
    this.rawClass = getClass(name);
    Boolean isParameterized = (Boolean) ois.readObject();
    if (isParameterized) {
        // GenericClass rawType = (GenericClass) ois.readObject();
        GenericClass ownerType = (GenericClass) ois.readObject();
        @SuppressWarnings("unchecked") List<GenericClass> parameterClasses = (List<GenericClass>) ois.readObject();
        Type[] parameterTypes = new Type[parameterClasses.size()];
        for (int i = 0; i < parameterClasses.size(); i++) parameterTypes[i] = parameterClasses.get(i).getType();
        this.type = new ParameterizedTypeImpl(rawClass, parameterTypes, ownerType.getType());
    } else {
        // GenericTypeReflector.addWildcardParameters(raw_class);
        this.type = addTypeParameters(rawClass);
    }
}
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) ParameterizedTypeImpl(org.evosuite.utils.ParameterizedTypeImpl)

Example 5 with ParameterizedTypeImpl

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

the class GenericClass method changeClassLoader.

/**
 * <p>
 * changeClassLoader
 * </p>
 *
 * @param loader
 *            a {@link java.lang.ClassLoader} object.
 */
public void changeClassLoader(ClassLoader loader) {
    try {
        if (rawClass != null)
            rawClass = getClass(rawClass.getName(), loader);
        if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) type;
            // GenericClass rawType = new GenericClass(pt.getRawType());
            // rawType.changeClassLoader(loader);
            GenericClass ownerType = null;
            if (pt.getOwnerType() != null) {
                ownerType = new GenericClass(pt.getOwnerType());
                // ownerType.type = pt.getOwnerType();
                ownerType.changeClassLoader(loader);
            }
            List<GenericClass> parameterClasses = new ArrayList<GenericClass>();
            for (Type parameterType : pt.getActualTypeArguments()) {
                GenericClass parameter = new GenericClass(parameterType);
                // parameter.type = parameterType;
                parameter.changeClassLoader(loader);
                parameterClasses.add(parameter);
            }
            Type[] parameterTypes = new Type[parameterClasses.size()];
            for (int i = 0; i < parameterClasses.size(); i++) parameterTypes[i] = parameterClasses.get(i).getType();
            this.type = new ParameterizedTypeImpl(rawClass, parameterTypes, ownerType != null ? ownerType.getType() : null);
        } else if (type instanceof GenericArrayType) {
            GenericClass componentClass = getComponentClass();
            componentClass.changeClassLoader(loader);
            this.type = GenericArrayTypeImpl.createArrayType(componentClass.getType());
        } else if (type instanceof WildcardType) {
            Type[] oldUpperBounds = ((WildcardType) type).getUpperBounds();
            Type[] oldLowerBounds = ((WildcardType) type).getLowerBounds();
            Type[] upperBounds = new Type[oldUpperBounds.length];
            Type[] lowerBounds = new Type[oldLowerBounds.length];
            for (int i = 0; i < oldUpperBounds.length; i++) {
                GenericClass bound = new GenericClass(oldUpperBounds[i]);
                // bound.type = oldUpperBounds[i];
                bound.changeClassLoader(loader);
                upperBounds[i] = bound.getType();
            }
            for (int i = 0; i < oldLowerBounds.length; i++) {
                GenericClass bound = new GenericClass(oldLowerBounds[i]);
                // bound.type = oldLowerBounds[i];
                bound.changeClassLoader(loader);
                lowerBounds[i] = bound.getType();
            }
            this.type = new WildcardTypeImpl(upperBounds, lowerBounds);
        } else if (type instanceof TypeVariable<?>) {
            for (TypeVariable<?> newVar : rawClass.getTypeParameters()) {
                if (newVar.getName().equals(((TypeVariable<?>) type).getName())) {
                    this.type = newVar;
                    break;
                }
            }
        } else {
            // GenericTypeReflector.addWildcardParameters(raw_class);
            this.type = addTypeParameters(rawClass);
        }
    } catch (ClassNotFoundException e) {
        logger.warn("Class not found: " + rawClass + " - keeping old class loader ", e);
    } catch (SecurityException e) {
        logger.warn("Class not found: " + rawClass + " - keeping old class loader ", e);
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedTypeImpl(org.evosuite.utils.ParameterizedTypeImpl) 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) CaptureType(com.googlecode.gentyref.CaptureType) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable)

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