use of java.lang.reflect.ParameterizedType in project retrofit by square.
the class RetrofitTest method callCallCustomAdapter.
@Test
public void callCallCustomAdapter() {
final AtomicBoolean factoryCalled = new AtomicBoolean();
final AtomicBoolean adapterCalled = new AtomicBoolean();
class MyCallAdapterFactory extends CallAdapter.Factory {
@Override
public CallAdapter<?, ?> get(final Type returnType, Annotation[] annotations, Retrofit retrofit) {
factoryCalled.set(true);
if (getRawType(returnType) != Call.class) {
return null;
}
return new CallAdapter<Object, Call<?>>() {
@Override
public Type responseType() {
return getParameterUpperBound(0, (ParameterizedType) returnType);
}
@Override
public Call<Object> adapt(Call<Object> call) {
adapterCalled.set(true);
return call;
}
};
}
}
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addCallAdapterFactory(new MyCallAdapterFactory()).build();
CallMethod example = retrofit.create(CallMethod.class);
assertThat(example.getResponseBody()).isNotNull();
assertThat(factoryCalled.get()).isTrue();
assertThat(adapterCalled.get()).isTrue();
}
use of java.lang.reflect.ParameterizedType in project moshi by square.
the class Types method resolveTypeVariable.
static Type resolveTypeVariable(Type context, Class<?> contextRawType, TypeVariable<?> unknown) {
Class<?> declaredByRaw = declaringClassOf(unknown);
// We can't reduce this further.
if (declaredByRaw == null)
return unknown;
Type declaredBy = getGenericSupertype(context, contextRawType, 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 moshi by square.
the class Types method resolve.
static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
// This implementation is made a little more complicated in an attempt to avoid object-creation.
while (true) {
if (toResolve instanceof TypeVariable) {
TypeVariable<?> typeVariable = (TypeVariable<?>) toResolve;
toResolve = resolveTypeVariable(context, contextRawType, typeVariable);
if (toResolve == typeVariable)
return toResolve;
} else if (toResolve instanceof Class && ((Class<?>) toResolve).isArray()) {
Class<?> original = (Class<?>) toResolve;
Type componentType = original.getComponentType();
Type newComponentType = resolve(context, contextRawType, componentType);
return componentType == newComponentType ? original : arrayOf(newComponentType);
} else if (toResolve instanceof GenericArrayType) {
GenericArrayType original = (GenericArrayType) toResolve;
Type componentType = original.getGenericComponentType();
Type newComponentType = resolve(context, contextRawType, componentType);
return componentType == newComponentType ? original : arrayOf(newComponentType);
} else if (toResolve instanceof ParameterizedType) {
ParameterizedType original = (ParameterizedType) toResolve;
Type ownerType = original.getOwnerType();
Type newOwnerType = resolve(context, contextRawType, ownerType);
boolean changed = newOwnerType != ownerType;
Type[] args = original.getActualTypeArguments();
for (int t = 0, length = args.length; t < length; t++) {
Type resolvedTypeArgument = resolve(context, contextRawType, args[t]);
if (resolvedTypeArgument != args[t]) {
if (!changed) {
args = args.clone();
changed = true;
}
args[t] = resolvedTypeArgument;
}
}
return changed ? new ParameterizedTypeImpl(newOwnerType, original.getRawType(), args) : original;
} else if (toResolve instanceof WildcardType) {
WildcardType original = (WildcardType) toResolve;
Type[] originalLowerBound = original.getLowerBounds();
Type[] originalUpperBound = original.getUpperBounds();
if (originalLowerBound.length == 1) {
Type lowerBound = resolve(context, contextRawType, originalLowerBound[0]);
if (lowerBound != originalLowerBound[0]) {
return supertypeOf(lowerBound);
}
} else if (originalUpperBound.length == 1) {
Type upperBound = resolve(context, contextRawType, originalUpperBound[0]);
if (upperBound != originalUpperBound[0]) {
return subtypeOf(upperBound);
}
}
return original;
} else {
return toResolve;
}
}
}
use of java.lang.reflect.ParameterizedType in project moshi by square.
the class AdapterMethodsTest method parameterizedTypeEqualsNotUsed.
/**
* Unfortunately in some versions of Android the implementations of {@link ParameterizedType}
* doesn't implement equals and hashCode. Confirm that we work around that.
*/
@Test
public void parameterizedTypeEqualsNotUsed() throws Exception {
Moshi moshi = new Moshi.Builder().add(new ListOfStringJsonAdapter()).build();
// This class doesn't implement equals() and hashCode() as it should.
ParameterizedType listOfStringType = brokenParameterizedType(0, List.class, String.class);
JsonAdapter<List<String>> jsonAdapter = moshi.adapter(listOfStringType);
assertThat(jsonAdapter.toJson(Arrays.asList("a", "b", "c"))).isEqualTo("\"a|b|c\"");
assertThat(jsonAdapter.fromJson("\"a|b|c\"")).isEqualTo(Arrays.asList("a", "b", "c"));
}
use of java.lang.reflect.ParameterizedType in project rest.li by linkedin.
the class ReflectionUtils method getClass.
/**
* Get the underlying class for a type, or null if the type is a variable type.
*
* @param type the type
* @return the underlying class
*/
public static Class<?> getClass(final Type type) {
if (type instanceof Class) {
return (Class) type;
} else if (type instanceof ParameterizedType) {
return getClass(((ParameterizedType) type).getRawType());
} else if (type instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) type).getGenericComponentType();
Class<?> componentClass = getClass(componentType);
if (componentClass != null) {
return Array.newInstance(componentClass, 0).getClass();
} else {
return null;
}
} else {
return null;
}
}
Aggregations