Search in sources :

Example 1 with CaptureType

use of com.googlecode.gentyref.CaptureType in project evosuite by EvoSuite.

the class ParameterizedTypeImpl method toString.

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    String clazz = rawType.getName();
    if (ownerType != null) {
        sb.append(GenericTypeReflector.getTypeName(ownerType)).append('.');
        String prefix = (ownerType instanceof ParameterizedType) ? ((Class<?>) ((ParameterizedType) ownerType).getRawType()).getName() + '$' : ((Class<?>) ownerType).getName() + '$';
        if (clazz.startsWith(prefix))
            clazz = clazz.substring(prefix.length());
    }
    sb.append(clazz);
    if (actualTypeArguments.length != 0) {
        sb.append('<');
        for (int i = 0; i < actualTypeArguments.length; i++) {
            Type arg = actualTypeArguments[i];
            if (i != 0)
                sb.append(", ");
            if (arg instanceof CaptureType) {
                CaptureType captureType = (CaptureType) arg;
                if (captureType.getLowerBounds().length == 0)
                    sb.append("?");
                else
                    sb.append(captureType.getLowerBounds()[0].toString());
            } else if (arg == null) {
                sb.append("null");
            } else {
                sb.append(GenericTypeReflector.getTypeName(arg));
            }
        }
        sb.append('>');
    }
    return sb.toString();
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) CaptureType(com.googlecode.gentyref.CaptureType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) CaptureType(com.googlecode.gentyref.CaptureType)

Example 2 with CaptureType

use of com.googlecode.gentyref.CaptureType in project evosuite by EvoSuite.

the class TestCodeVisitor method getTypeParameterName.

public String getTypeParameterName(Type type) {
    if (type instanceof Class<?>) {
        return getClassName((Class<?>) type);
    } else if (type instanceof ParameterizedType) {
        return getTypeName((ParameterizedType) type);
    } else if (type instanceof WildcardType) {
        String ret = "?";
        boolean first = true;
        for (Type bound : ((WildcardType) type).getLowerBounds()) {
            // If there are lower bounds we need to state them, even if Object
            if (// || GenericTypeReflector.erase(bound).equals(Object.class))
            bound == null)
                continue;
            if (!first)
                ret += ", ";
            ret += " super " + getTypeParameterName(bound);
            first = false;
        }
        for (Type bound : ((WildcardType) type).getUpperBounds()) {
            if (bound == null || (!(bound instanceof CaptureType) && GenericTypeReflector.erase(bound).equals(Object.class)))
                continue;
            if (!first)
                ret += ", ";
            ret += " extends " + getTypeParameterName(bound);
            first = false;
        }
        return ret;
    } else if (type instanceof TypeVariable) {
        return "?";
    } else if (type instanceof CaptureType) {
        CaptureType captureType = (CaptureType) type;
        if (captureType.getLowerBounds().length == 0)
            return "?";
        else
            return getTypeName(captureType.getLowerBounds()[0]);
    } else if (type instanceof GenericArrayType) {
        return getTypeName(((GenericArrayType) type).getGenericComponentType()) + "[]";
    } else {
        throw new RuntimeException("Unsupported type:" + type + ", class" + type.getClass());
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) CaptureType(com.googlecode.gentyref.CaptureType) WildcardType(java.lang.reflect.WildcardType) 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) GenericClass(org.evosuite.utils.generic.GenericClass) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 3 with CaptureType

use of com.googlecode.gentyref.CaptureType in project evosuite by EvoSuite.

the class TestFactory method satisfyParameters.

/**
 * Satisfy a list of parameters by reusing or creating variables
 *
 * @param test
 * @param parameterTypes
 * @param parameterList
 * @param position
 * @param recursionDepth
 * @return
 * @throws ConstructionFailedException
 */
public List<VariableReference> satisfyParameters(TestCase test, VariableReference callee, List<Type> parameterTypes, List<Parameter> parameterList, int position, int recursionDepth, boolean allowNull, boolean excludeCalleeGenerators, boolean canReuseExistingVariables) throws ConstructionFailedException {
    if (callee == null && excludeCalleeGenerators) {
        throw new IllegalArgumentException("Exclude generators on null callee");
    }
    List<VariableReference> parameters = new ArrayList<>();
    logger.debug("Trying to satisfy {} parameters at position {}", parameterTypes.size(), position);
    for (int i = 0; i < parameterTypes.size(); i++) {
        Type parameterType = parameterTypes.get(i);
        Parameter parameter = null;
        boolean allowNullForParameter = allowNull;
        if (parameterList != null)
            parameter = parameterList.get(i);
        logger.debug("Current parameter type: {}", parameterType);
        if (parameterType instanceof CaptureType) {
            // TODO: This should not really happen in the first place
            throw new ConstructionFailedException("Cannot satisfy capture type");
        }
        GenericClass parameterClass = new GenericClass(parameterType);
        if (parameterClass.hasTypeVariables()) {
            logger.debug("Parameter has type variables, replacing with wildcard");
            parameterType = parameterClass.getWithWildcardTypes().getType();
        }
        int previousLength = test.size();
        VariableReference var = null;
        if (Properties.HONOUR_DATA_ANNOTATIONS && (parameterList != null)) {
            if (GenericUtils.isAnnotationTypePresent(parameter.getAnnotations(), GenericUtils.NONNULL)) {
                allowNullForParameter = false;
            }
        }
        if (canReuseExistingVariables) {
            logger.debug("Can re-use variables");
            var = createOrReuseVariable(test, parameterType, position, recursionDepth, callee, allowNullForParameter, excludeCalleeGenerators, true);
        } else {
            logger.debug("Cannot re-use variables: attempt at creating new one");
            var = createVariable(test, parameterType, position, recursionDepth, callee, allowNullForParameter, excludeCalleeGenerators, true, false);
            if (var == null) {
                throw new ConstructionFailedException("Failed to create variable for type " + parameterType + " at position " + position);
            }
        }
        assert !(!allowNullForParameter && ConstraintHelper.isNull(var, test));
        if (var.getStPosition() < position && ConstraintHelper.getLastPositionOfBounded(var, test) >= position) {
            String msg = "Bounded variable issue when calling satisfyParameters()";
            AtMostOnceLogger.warn(logger, msg);
            throw new ConstructionFailedException(msg);
        }
        // double check
        if (!var.isAssignableTo(parameterType)) {
            throw new ConstructionFailedException("Error: " + var + " is not assignable to " + parameterType);
        }
        parameters.add(var);
        int currentLength = test.size();
        position += currentLength - previousLength;
    }
    logger.debug("Satisfied {} parameters", parameterTypes.size());
    return parameters;
}
Also used : CaptureType(com.googlecode.gentyref.CaptureType) CaptureType(com.googlecode.gentyref.CaptureType) GenericClass(org.evosuite.utils.generic.GenericClass) ArrayList(java.util.ArrayList) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Aggregations

CaptureType (com.googlecode.gentyref.CaptureType)3 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 GenericClass (org.evosuite.utils.generic.GenericClass)2 GenericArrayType (java.lang.reflect.GenericArrayType)1 TypeVariable (java.lang.reflect.TypeVariable)1 WildcardType (java.lang.reflect.WildcardType)1 ArrayList (java.util.ArrayList)1 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)1