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