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

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

Introduction

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

Prototype

public static Type[] getImplicitBounds(final TypeVariable<?> typeVariable) 

Source Link

Document

Returns an array containing the sole type of Object if TypeVariable#getBounds() returns an empty array.

Usage

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

private void calculateExactType(TypeVariable<?> type) {
    logger.info("Calculating exact type for typevariable " + type);

    Type[] bounds = TypeUtils.getImplicitBounds(type);
    Type exactType = bounds[0];/*from w  ww .ja v a  2  s  .  c  o m*/
    for (VariableReference var : typeMap.get(type)) {
        Type candidateType = var.getType();

        logger.info("Candidate type: " + candidateType);
        if (TypeUtils.isAssignable(candidateType, exactType)) {
            exactType = candidateType;
        }
    }
    logger.info("Result: " + exactType);
}

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

private void determineExactType(ConstructorStatement constructorStatement) {
    GenericConstructor constructor = constructorStatement.getConstructor();
    logger.debug("Inferring types for: " + constructorStatement.getCode() + " at position "
            + constructorStatement.getPosition());

    Map<TypeVariable<?>, Type> typeMap = constructor.getOwnerClass().getTypeVariableMap();
    if (constructor.getOwnerClass().hasTypeVariables()) {
        // if (!typeMap.isEmpty()) {
        logger.info("Has types: " + constructor.getOwnerClass());
        logger.info("Initial type map: " + typeMap);
        for (TypeVariable<?> var : typeMap.keySet()) {
            typeMap.put(var, null);
        }//from  w  ww .java2s  . c o m
        Type[] parameterTypes = constructor.getGenericParameterTypes(); //.getParameterTypes();
        List<VariableReference> parameterValues = constructorStatement.getParameterReferences();
        determineVariablesFromParameters(parameterValues, parameterTypes, typeMap);

        for (int pos = constructorStatement.getPosition() + 1; pos < test.size(); pos++) {
            if (test.getStatement(pos) instanceof MethodStatement) {
                MethodStatement ms = (MethodStatement) test.getStatement(pos);
                if (ms.isStatic())
                    continue;
                if (!ms.getCallee().equals(constructorStatement.getReturnValue()))
                    continue;

                logger.info("Found relevant statement: " + ms.getCode());
                parameterTypes = ms.getMethod().getGenericParameterTypes();
                parameterValues = ms.getParameterReferences();
                determineVariablesFromParameters(parameterValues, parameterTypes, typeMap);
            }
        }
        logger.info("Setting types based on map: " + typeMap);
        GenericClass owner = constructor.getOwnerClass();
        List<TypeVariable<?>> variables = owner.getTypeVariables();
        List<Type> types = new ArrayList<Type>();
        for (TypeVariable<?> var : variables) {
            Type type = typeMap.get(var);
            if (type == null) {
                types.add(new WildcardTypeImpl(TypeUtils.getImplicitBounds(var), new Type[] {}));
            } else {
                Class<?> paramClass = GenericTypeReflector.erase(type);
                if (paramClass.isPrimitive()) {
                    types.add(ClassUtils.primitiveToWrapper(paramClass));
                } else {
                    types.add(typeMap.get(var));
                }
            }
        }

        constructorStatement.setConstructor(constructor.copyWithNewOwner(owner.getWithParameterTypes(types)));
        logger.info("New type: " + constructorStatement);
        updateMethodCallsOfGenericOwner(constructorStatement.getReturnValue());
    } else {
        logger.info("Type map empty");

    }
}