Search in sources :

Example 46 with WildcardType

use of java.lang.reflect.WildcardType in project evosuite by EvoSuite.

the class TestGenericClass method testWildcardComparableBoundaries.

@Test
public void testWildcardComparableBoundaries() {
    WildcardType objectType = new WildcardTypeImpl(new Type[] { Comparable.class }, new Type[] {});
    GenericClass integerClass = new GenericClass(Integer.class);
    GenericClass comparableClass = new GenericClass(Comparable.class);
    GenericClass dateClass = new GenericClass(java.util.Date.class);
    GenericClass sqlDateClass = new GenericClass(java.sql.Date.class);
    Assert.assertTrue(integerClass.satisfiesBoundaries(objectType));
    Assert.assertTrue(comparableClass.satisfiesBoundaries(objectType));
    Assert.assertTrue(dateClass.satisfiesBoundaries(objectType));
    Assert.assertTrue(sqlDateClass.satisfiesBoundaries(objectType));
}
Also used : WildcardType(java.lang.reflect.WildcardType) WildcardTypeImpl(org.evosuite.utils.generic.WildcardTypeImpl) GenericClass(org.evosuite.utils.generic.GenericClass) Test(org.junit.Test)

Example 47 with WildcardType

use of java.lang.reflect.WildcardType in project evosuite by EvoSuite.

the class TestGenericClass method testWildcardDateBothBoundaries2.

@Test
public void testWildcardDateBothBoundaries2() {
    WildcardType objectType = new WildcardTypeImpl(new Type[] { Comparable.class }, new Type[] { java.util.Date.class });
    GenericClass integerClass = new GenericClass(Integer.class);
    GenericClass comparableClass = new GenericClass(Comparable.class);
    GenericClass dateClass = new GenericClass(java.util.Date.class);
    GenericClass sqlDateClass = new GenericClass(java.sql.Date.class);
    Assert.assertFalse(integerClass.satisfiesBoundaries(objectType));
    Assert.assertFalse(comparableClass.satisfiesBoundaries(objectType));
    Assert.assertTrue(dateClass.satisfiesBoundaries(objectType));
    // Does not satisfy lower boundary
    Assert.assertFalse(sqlDateClass.satisfiesBoundaries(objectType));
}
Also used : WildcardType(java.lang.reflect.WildcardType) WildcardTypeImpl(org.evosuite.utils.generic.WildcardTypeImpl) GenericClass(org.evosuite.utils.generic.GenericClass) Test(org.junit.Test)

Example 48 with WildcardType

use of java.lang.reflect.WildcardType in project javaparser by javaparser.

the class ReflectionFactory method typeUsageFor.

public static ResolvedType typeUsageFor(java.lang.reflect.Type type, TypeSolver typeSolver) {
    if (type instanceof java.lang.reflect.TypeVariable) {
        java.lang.reflect.TypeVariable<?> tv = (java.lang.reflect.TypeVariable<?>) type;
        boolean declaredOnClass = tv.getGenericDeclaration() instanceof java.lang.reflect.Type;
        ResolvedTypeParameterDeclaration typeParameter = new ReflectionTypeParameter(tv, declaredOnClass, typeSolver);
        return new ResolvedTypeVariable(typeParameter);
    } else if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;
        ResolvedReferenceType rawType = typeUsageFor(pt.getRawType(), typeSolver).asReferenceType();
        List<java.lang.reflect.Type> actualTypes = new ArrayList<>();
        actualTypes.addAll(Arrays.asList(pt.getActualTypeArguments()));
        // we consume the actual types
        rawType = rawType.transformTypeParameters(tp -> typeUsageFor(actualTypes.remove(0), typeSolver)).asReferenceType();
        return rawType;
    } else if (type instanceof Class) {
        Class<?> c = (Class<?>) type;
        if (c.isPrimitive()) {
            if (c.getName().equals(Void.TYPE.getName())) {
                return ResolvedVoidType.INSTANCE;
            } else {
                return ResolvedPrimitiveType.byName(c.getName());
            }
        } else if (c.isArray()) {
            return new ResolvedArrayType(typeUsageFor(c.getComponentType(), typeSolver));
        } else {
            return new ReferenceTypeImpl(typeDeclarationFor(c, typeSolver), typeSolver);
        }
    } else if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = (GenericArrayType) type;
        return new ResolvedArrayType(typeUsageFor(genericArrayType.getGenericComponentType(), typeSolver));
    } else if (type instanceof WildcardType) {
        WildcardType wildcardType = (WildcardType) type;
        if (wildcardType.getLowerBounds().length > 0 && wildcardType.getUpperBounds().length > 0) {
            if (wildcardType.getUpperBounds().length == 1 && wildcardType.getUpperBounds()[0].getTypeName().equals("java.lang.Object")) {
            // ok, it does not matter
            }
        }
        if (wildcardType.getLowerBounds().length > 0) {
            if (wildcardType.getLowerBounds().length > 1) {
                throw new UnsupportedOperationException();
            }
            return ResolvedWildcard.superBound(typeUsageFor(wildcardType.getLowerBounds()[0], typeSolver));
        }
        if (wildcardType.getUpperBounds().length > 0) {
            if (wildcardType.getUpperBounds().length > 1) {
                throw new UnsupportedOperationException();
            }
            return ResolvedWildcard.extendsBound(typeUsageFor(wildcardType.getUpperBounds()[0], typeSolver));
        }
        return ResolvedWildcard.UNBOUNDED;
    } else {
        throw new UnsupportedOperationException(type.getClass().getCanonicalName() + " " + type);
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) Arrays(java.util.Arrays) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) com.github.javaparser.symbolsolver.model.typesystem(com.github.javaparser.symbolsolver.model.typesystem) WildcardType(java.lang.reflect.WildcardType) AccessSpecifier(com.github.javaparser.ast.AccessSpecifier) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ArrayList(java.util.ArrayList) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) List(java.util.List) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ParameterizedType(java.lang.reflect.ParameterizedType) com.github.javaparser.resolution.types(com.github.javaparser.resolution.types) Modifier(java.lang.reflect.Modifier) ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) ArrayList(java.util.ArrayList) List(java.util.List)

Example 49 with WildcardType

use of java.lang.reflect.WildcardType in project evosuite by EvoSuite.

the class GenericClass method satisfiesBoundaries.

/**
 * Determine whether the boundaries of the type variable are satisfied by
 * this class
 *
 * @param typeVariable
 * @return
 */
public boolean satisfiesBoundaries(TypeVariable<?> typeVariable, Map<TypeVariable<?>, Type> typeMap) {
    boolean isAssignable = true;
    // logger.debug("Checking class: " + type + " against type variable " + typeVariable+" with map "+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()) {
            // ==> A -> C
            if (ownerVariableMap.get(var) instanceof TypeVariable<?>) {
                // Other type variable, i.e., the one this is currently pointing to
                TypeVariable<?> value = (TypeVariable<?>) ownerVariableMap.get(var);
                if (ownerVariableMap.containsKey(value)) {
                    Type other = ownerVariableMap.get(value);
                    if (other instanceof TypeVariable<?>) {
                        // If the value (C) is also a typevariable, check we don't have a recursion here
                        if (ownerVariableMap.containsKey(other)) {
                            Type x = ownerVariableMap.get(other);
                            if (x == var || x == value || x == other) {
                                continue;
                            }
                        }
                    }
                    if (var != other && value != other) {
                        ownerVariableMap.put(var, other);
                        changed = true;
                    }
                }
            }
        }
    }
    GenericClass concreteClass = new GenericClass(GenericUtils.replaceTypeVariables(type, ownerVariableMap));
    for (Type theType : typeVariable.getBounds()) {
        // Special case: Enum is defined as Enum<T extends Enum>
        if (GenericTypeReflector.erase(theType).equals(Enum.class)) {
            // if this is an enum then it's ok.
            if (isEnum()) {
                continue;
            } else {
                // If it's not an enum, it cannot be assignable to enum!
                // logger.debug("Class " + toString() + " is not an enum.");
                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) && !(boundType instanceof WildcardType)) {
            // 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)) {
                    // + instanceType);
                    continue;
                } else {
                // logger.debug("Is not assignable: " + boundType + " and "
                // + instanceType);
                }
            }
            isAssignable = false;
            break;
        }
    }
    // logger.debug("Result: is assignable " + isAssignable);
    return isAssignable;
}
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)

Example 50 with WildcardType

use of java.lang.reflect.WildcardType 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)

Aggregations

WildcardType (java.lang.reflect.WildcardType)236 ParameterizedType (java.lang.reflect.ParameterizedType)208 Type (java.lang.reflect.Type)180 GenericArrayType (java.lang.reflect.GenericArrayType)160 TypeVariable (java.lang.reflect.TypeVariable)134 Test (org.junit.Test)24 GenericClass (org.evosuite.utils.generic.GenericClass)14 Method (java.lang.reflect.Method)10 WildcardTypeImpl (org.evosuite.utils.generic.WildcardTypeImpl)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)8 Test (org.junit.jupiter.api.Test)8 CaptureType (com.googlecode.gentyref.CaptureType)7 JSONException (com.alibaba.fastjson.JSONException)5 JSONType (com.alibaba.fastjson.annotation.JSONType)5 CatalogType (org.spongepowered.api.CatalogType)4 GenericDeclaration (java.lang.reflect.GenericDeclaration)3 HashSet (java.util.HashSet)3 List (java.util.List)3 MediaType (javax.ws.rs.core.MediaType)3