use of java.lang.reflect.TypeVariable in project spring-framework by spring-projects.
the class ResolvableType method forClassWithGenerics.
/**
* Return a {@link ResolvableType} for the specified {@link Class} with pre-declared generics.
* @param clazz the class (or interface) to introspect
* @param generics the generics of the class
* @return a {@link ResolvableType} for the specific class and generics
* @see #forClassWithGenerics(Class, Class...)
*/
public static ResolvableType forClassWithGenerics(Class<?> clazz, ResolvableType... generics) {
Assert.notNull(clazz, "Class must not be null");
Assert.notNull(generics, "Generics array must not be null");
TypeVariable<?>[] variables = clazz.getTypeParameters();
Assert.isTrue(variables.length == generics.length, "Mismatched number of generics specified");
Type[] arguments = new Type[generics.length];
for (int i = 0; i < generics.length; i++) {
ResolvableType generic = generics[i];
Type argument = (generic != null ? generic.getType() : null);
arguments[i] = (argument != null ? argument : variables[i]);
}
ParameterizedType syntheticType = new SyntheticParameterizedType(clazz, arguments);
return forType(syntheticType, new TypeVariablesVariableResolver(variables, generics));
}
use of java.lang.reflect.TypeVariable in project spring-framework by spring-projects.
the class ResolvableType method resolveVariable.
private ResolvableType resolveVariable(TypeVariable<?> variable) {
if (this.type instanceof TypeVariable) {
return resolveType().resolveVariable(variable);
}
if (this.type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) this.type;
TypeVariable<?>[] variables = resolve().getTypeParameters();
for (int i = 0; i < variables.length; i++) {
if (ObjectUtils.nullSafeEquals(variables[i].getName(), variable.getName())) {
Type actualType = parameterizedType.getActualTypeArguments()[i];
return forType(actualType, this.variableResolver);
}
}
if (parameterizedType.getOwnerType() != null) {
return forType(parameterizedType.getOwnerType(), this.variableResolver).resolveVariable(variable);
}
}
if (this.variableResolver != null) {
return this.variableResolver.resolveVariable(variable);
}
return null;
}
use of java.lang.reflect.TypeVariable in project robovm by robovm.
the class WildcardTypeTest method checkReturnType.
@SuppressWarnings("unchecked")
private void checkReturnType(Method method) {
Type genericReturnType = method.getGenericReturnType();
assertEquals(getTypeParameter(method), genericReturnType);
assertTrue(genericReturnType instanceof TypeVariable);
TypeVariable<Method> returnTypeVariable = (TypeVariable<Method>) genericReturnType;
assertEquals(method, returnTypeVariable.getGenericDeclaration());
Type[] bounds = returnTypeVariable.getBounds();
assertLenghtOne(bounds);
Type bound = bounds[0];
assertEquals(BoundedWildcardsGenericMethods.class, bound);
}
use of java.lang.reflect.TypeVariable in project robovm by robovm.
the class GenericArrayTypeTest method testGetGenericComponentType.
public void testGetGenericComponentType() throws Exception {
@SuppressWarnings("unchecked") Class<? extends A> clazz = GenericArrayTypeTest.A.class;
Field field = clazz.getDeclaredField("array");
Type genericType = field.getGenericType();
assertInstanceOf(GenericArrayType.class, genericType);
Type componentType = ((GenericArrayType) genericType).getGenericComponentType();
assertEquals(getTypeParameter(clazz), componentType);
assertInstanceOf(TypeVariable.class, componentType);
TypeVariable<?> componentTypeVariable = (TypeVariable<?>) componentType;
assertEquals("T", componentTypeVariable.getName());
assertEquals(clazz, componentTypeVariable.getGenericDeclaration());
}
use of java.lang.reflect.TypeVariable in project cdap by caskdata.
the class PipelineTypeValidator method validateTypes.
/**
* Takes in an unresolved type list and resolves the types and verifies if the types are assignable.
* Ex: An unresolved type could be : String, T, List<T>, List<String>
* The above will resolve to : String, String, List<String>, List<String>
* And the assignability will be checked : String --> String && List<String> --> List<String>
* which is true in the case above.
*/
@VisibleForTesting
static void validateTypes(List<Type> unresTypeList) {
Preconditions.checkArgument(unresTypeList.size() % 2 == 0, "ETL Stages validation expects even number of types");
List<Type> resTypeList = Lists.newArrayListWithCapacity(unresTypeList.size());
// Add the source output to resolved type list as the first resolved type.
resTypeList.add(unresTypeList.get(0));
try {
// Resolve the second type using just the first resolved type.
Type nType = (new TypeResolver()).where(unresTypeList.get(1), resTypeList.get(0)).resolveType(unresTypeList.get(1));
resTypeList.add(nType);
} catch (IllegalArgumentException e) {
// If unable to resolve type, add the second type as is, to the resolved list.
resTypeList.add(unresTypeList.get(1));
}
for (int i = 2; i < unresTypeList.size(); i++) {
// ActualType is previous resolved type; FormalType is previous unresolved type;
// ToResolveType is current unresolved type;
// Ex: Actual = String; Formal = T; ToResolve = List<T>; ==> newType = List<String>
Type actualType = resTypeList.get(i - 1);
Type formalType = unresTypeList.get(i - 1);
Type toResolveType = unresTypeList.get(i);
try {
Type newType;
// newType should be List<String>. Hence resolve only from the previous resolved type (Actual)
if ((toResolveType instanceof TypeVariable) || (toResolveType instanceof GenericArrayType)) {
newType = (new TypeResolver()).where(toResolveType, actualType).resolveType(toResolveType);
} else {
newType = (new TypeResolver()).where(formalType, actualType).resolveType(toResolveType);
}
resTypeList.add(newType);
} catch (IllegalArgumentException e) {
// If resolution failed, add the type as is to the resolved list.
resTypeList.add(toResolveType);
}
}
// transform (which takes in type on its left and emits the type on its right).
for (int i = 0; i < resTypeList.size(); i += 2) {
Type firstType = resTypeList.get(i);
Type secondType = resTypeList.get(i + 1);
// Check if secondType can accept firstType
Preconditions.checkArgument(TypeToken.of(secondType).isAssignableFrom(firstType), "Types between stages didn't match. Mismatch between %s -> %s", firstType, secondType);
}
}
Aggregations