Search in sources :

Example 81 with TypeVariable

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));
}
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) TypeVariable(java.lang.reflect.TypeVariable)

Example 82 with TypeVariable

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;
}
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) TypeVariable(java.lang.reflect.TypeVariable)

Example 83 with TypeVariable

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);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) Method(java.lang.reflect.Method)

Example 84 with TypeVariable

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());
}
Also used : Field(java.lang.reflect.Field) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 85 with TypeVariable

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);
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) TypeResolver(co.cask.cdap.etl.common.guice.TypeResolver) GenericArrayType(java.lang.reflect.GenericArrayType) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

TypeVariable (java.lang.reflect.TypeVariable)218 Type (java.lang.reflect.Type)169 ParameterizedType (java.lang.reflect.ParameterizedType)168 GenericArrayType (java.lang.reflect.GenericArrayType)122 WildcardType (java.lang.reflect.WildcardType)100 Method (java.lang.reflect.Method)44 ArrayList (java.util.ArrayList)18 HashMap (java.util.HashMap)11 GenericDeclaration (java.lang.reflect.GenericDeclaration)8 List (java.util.List)8 Map (java.util.Map)8 ElementType (java.lang.annotation.ElementType)7 Field (java.lang.reflect.Field)7 IOException (java.io.IOException)6 AccessibleObject (java.lang.reflect.AccessibleObject)6 MediaType (javax.ws.rs.core.MediaType)6 CompositeType (org.apache.flink.api.common.typeutils.CompositeType)6 TypeExtractionUtils.isClassType (org.apache.flink.api.java.typeutils.TypeExtractionUtils.isClassType)6 Test (org.junit.Test)6 VisibleForTesting (com.google.common.annotations.VisibleForTesting)5