Search in sources :

Example 51 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project roboguice by roboguice.

the class MoreTypes method resolveTypeVariable.

public static Type resolveTypeVariable(Type type, Class<?> rawType, TypeVariable unknown) {
    Class<?> declaredByRaw = declaringClassOf(unknown);
    // we can't reduce this further
    if (declaredByRaw == null) {
        return unknown;
    }
    Type declaredBy = getGenericSupertype(type, rawType, declaredByRaw);
    if (declaredBy instanceof ParameterizedType) {
        int index = indexOf(declaredByRaw.getTypeParameters(), unknown);
        return ((ParameterizedType) declaredBy).getActualTypeArguments()[index];
    }
    return unknown;
}
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)

Example 52 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project roboguice by roboguice.

the class MoreTypes method canonicalizeForKey.

/**
   * Returns an type that's appropriate for use in a key.
   *
   * <p>If the raw type of {@code typeLiteral} is a {@code javax.inject.Provider}, this returns a
   * {@code com.google.inject.Provider} with the same type parameters.
   *
   * <p>If the type is a primitive, the corresponding wrapper type will be returned.
   *
   * @throws ConfigurationException if {@code type} contains a type variable
   */
public static <T> TypeLiteral<T> canonicalizeForKey(TypeLiteral<T> typeLiteral) {
    Type type = typeLiteral.getType();
    if (!isFullySpecified(type)) {
        Errors errors = new Errors().keyNotFullySpecified(typeLiteral);
        throw new ConfigurationException(errors.getMessages());
    }
    if (typeLiteral.getRawType() == javax.inject.Provider.class) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        // the following casts are generally unsafe, but com.google.inject.Provider extends
        // javax.inject.Provider and is covariant
        @SuppressWarnings("unchecked") TypeLiteral<T> guiceProviderType = (TypeLiteral<T>) TypeLiteral.get(Types.providerOf(parameterizedType.getActualTypeArguments()[0]));
        return guiceProviderType;
    }
    @SuppressWarnings("unchecked") TypeLiteral<T> wrappedPrimitives = (TypeLiteral<T>) PRIMITIVE_TO_WRAPPER.get(typeLiteral);
    return wrappedPrimitives != null ? wrappedPrimitives : typeLiteral;
}
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) TypeLiteral(com.google.inject.TypeLiteral) ConfigurationException(com.google.inject.ConfigurationException)

Example 53 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project roboguice by roboguice.

the class KeyTest method testCannotCreateKeysWithTypeVariables.

/** Test for issue 186 */
public void testCannotCreateKeysWithTypeVariables() throws NoSuchMethodException {
    ParameterizedType listOfTType = (ParameterizedType) getClass().getDeclaredMethod("parameterizedWithVariable", List.class).getGenericParameterTypes()[0];
    TypeLiteral<?> listOfT = TypeLiteral.get(listOfTType);
    try {
        Key.get(listOfT);
        fail("Guice should not allow keys for java.util.List<T>");
    } catch (ConfigurationException e) {
        assertContains(e.getMessage(), "java.util.List<T> cannot be used as a key; It is not fully specified.");
    }
    TypeVariable tType = (TypeVariable) listOfTType.getActualTypeArguments()[0];
    TypeLiteral<?> t = TypeLiteral.get(tType);
    try {
        Key.get(t);
        fail("Guice should not allow keys for T");
    } catch (ConfigurationException e) {
        assertContains(e.getMessage(), "T cannot be used as a key; It is not fully specified.");
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) TypeVariable(java.lang.reflect.TypeVariable) List(java.util.List)

Example 54 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project retrofit by square.

the class RxJava2CallAdapterFactory method get.

@Override
public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
    Class<?> rawType = getRawType(returnType);
    if (rawType == Completable.class) {
        // can only be created with a single configuration.
        return new RxJava2CallAdapter(Void.class, scheduler, isAsync, false, true, false, false, false, true);
    }
    boolean isFlowable = rawType == Flowable.class;
    boolean isSingle = rawType == Single.class;
    boolean isMaybe = rawType == Maybe.class;
    if (rawType != Observable.class && !isFlowable && !isSingle && !isMaybe) {
        return null;
    }
    boolean isResult = false;
    boolean isBody = false;
    Type responseType;
    if (!(returnType instanceof ParameterizedType)) {
        String name = isFlowable ? "Flowable" : isSingle ? "Single" : isMaybe ? "Maybe" : "Observable";
        throw new IllegalStateException(name + " return type must be parameterized" + " as " + name + "<Foo> or " + name + "<? extends Foo>");
    }
    Type observableType = getParameterUpperBound(0, (ParameterizedType) returnType);
    Class<?> rawObservableType = getRawType(observableType);
    if (rawObservableType == Response.class) {
        if (!(observableType instanceof ParameterizedType)) {
            throw new IllegalStateException("Response must be parameterized" + " as Response<Foo> or Response<? extends Foo>");
        }
        responseType = getParameterUpperBound(0, (ParameterizedType) observableType);
    } else if (rawObservableType == Result.class) {
        if (!(observableType instanceof ParameterizedType)) {
            throw new IllegalStateException("Result must be parameterized" + " as Result<Foo> or Result<? extends Foo>");
        }
        responseType = getParameterUpperBound(0, (ParameterizedType) observableType);
        isResult = true;
    } else {
        responseType = observableType;
        isBody = true;
    }
    return new RxJava2CallAdapter(responseType, scheduler, isAsync, isResult, isBody, isFlowable, isSingle, isMaybe, false);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Observable(io.reactivex.Observable)

Example 55 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project retrofit by square.

the class RxJavaCallAdapterFactory method get.

@Override
public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
    Class<?> rawType = getRawType(returnType);
    boolean isSingle = rawType == Single.class;
    boolean isCompletable = "rx.Completable".equals(rawType.getCanonicalName());
    if (rawType != Observable.class && !isSingle && !isCompletable) {
        return null;
    }
    if (isCompletable) {
        return new RxJavaCallAdapter(Void.class, scheduler, isAsync, false, true, false, true);
    }
    boolean isResult = false;
    boolean isBody = false;
    Type responseType;
    if (!(returnType instanceof ParameterizedType)) {
        String name = isSingle ? "Single" : "Observable";
        throw new IllegalStateException(name + " return type must be parameterized" + " as " + name + "<Foo> or " + name + "<? extends Foo>");
    }
    Type observableType = getParameterUpperBound(0, (ParameterizedType) returnType);
    Class<?> rawObservableType = getRawType(observableType);
    if (rawObservableType == Response.class) {
        if (!(observableType instanceof ParameterizedType)) {
            throw new IllegalStateException("Response must be parameterized" + " as Response<Foo> or Response<? extends Foo>");
        }
        responseType = getParameterUpperBound(0, (ParameterizedType) observableType);
    } else if (rawObservableType == Result.class) {
        if (!(observableType instanceof ParameterizedType)) {
            throw new IllegalStateException("Result must be parameterized" + " as Result<Foo> or Result<? extends Foo>");
        }
        responseType = getParameterUpperBound(0, (ParameterizedType) observableType);
        isResult = true;
    } else {
        responseType = observableType;
        isBody = true;
    }
    return new RxJavaCallAdapter(responseType, scheduler, isAsync, isResult, isBody, isSingle, false);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Aggregations

ParameterizedType (java.lang.reflect.ParameterizedType)704 Type (java.lang.reflect.Type)532 GenericArrayType (java.lang.reflect.GenericArrayType)226 WildcardType (java.lang.reflect.WildcardType)182 TypeVariable (java.lang.reflect.TypeVariable)137 ArrayList (java.util.ArrayList)94 Method (java.lang.reflect.Method)71 Test (org.junit.Test)59 List (java.util.List)55 Field (java.lang.reflect.Field)49 Map (java.util.Map)47 HashMap (java.util.HashMap)42 Collection (java.util.Collection)26 MediaType (javax.ws.rs.core.MediaType)23 Annotation (java.lang.annotation.Annotation)18 HashSet (java.util.HashSet)13 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)13 ImmutableList (com.google.common.collect.ImmutableList)12 TypeLiteral (com.google.inject.TypeLiteral)12 IOException (java.io.IOException)11