Search in sources :

Example 31 with TypeVariable

use of java.lang.reflect.TypeVariable in project robovm by robovm.

the class GenericExceptionsTest method testThrowingMethodTypeParameter.

public void testThrowingMethodTypeParameter() throws Exception {
    Method method = ThrowerT.class.getMethod("throwsMethodTypeParameter");
    TypeVariable typeVariable = getOnlyValue(method.getGenericExceptionTypes(), TypeVariable.class);
    assertEquals("X", typeVariable.getName());
    assertEquals(Arrays.<Type>asList(Exception.class), Arrays.asList(typeVariable.getBounds()));
}
Also used : TypeVariable(java.lang.reflect.TypeVariable) Method(java.lang.reflect.Method) IOException(java.io.IOException)

Example 32 with TypeVariable

use of java.lang.reflect.TypeVariable in project robovm by robovm.

the class GenericExceptionsTest method testConstructorThrowingTypeVariable.

public void testConstructorThrowingTypeVariable() throws Exception {
    Constructor constructor = ThrowerT.class.getConstructor();
    TypeVariable typeVariable = getOnlyValue(constructor.getGenericExceptionTypes(), TypeVariable.class);
    assertEquals("T", typeVariable.getName());
    assertEquals(Arrays.<Type>asList(Throwable.class), Arrays.asList(typeVariable.getBounds()));
}
Also used : TypeVariable(java.lang.reflect.TypeVariable) Constructor(java.lang.reflect.Constructor)

Example 33 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 34 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 35 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)

Aggregations

TypeVariable (java.lang.reflect.TypeVariable)424 ParameterizedType (java.lang.reflect.ParameterizedType)342 Type (java.lang.reflect.Type)341 GenericArrayType (java.lang.reflect.GenericArrayType)251 WildcardType (java.lang.reflect.WildcardType)191 Method (java.lang.reflect.Method)63 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)23 GenericDeclaration (java.lang.reflect.GenericDeclaration)19 Test (org.junit.Test)16 Field (java.lang.reflect.Field)15 List (java.util.List)13 Map (java.util.Map)11 CompositeType (org.apache.flink.api.common.typeutils.CompositeType)11 TypeExtractionUtils.isClassType (org.apache.flink.api.java.typeutils.TypeExtractionUtils.isClassType)11 Member (java.lang.reflect.Member)10 MediaType (javax.ws.rs.core.MediaType)10 CaptureType (com.googlecode.gentyref.CaptureType)8 ElementType (java.lang.annotation.ElementType)8 ParameterType (org.apache.cxf.jaxrs.model.ParameterType)8