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));
}
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));
}
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);
}
}
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;
}
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;
}
Aggregations