use of java.lang.reflect.ParameterizedType in project stetho by facebook.
the class ObjectMapper method convertArrayToList.
private List<Object> convertArrayToList(Field field, JSONArray array) throws IllegalAccessException, JSONException {
if (List.class.isAssignableFrom(field.getType())) {
ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
Type[] types = parameterizedType.getActualTypeArguments();
if (types.length != 1) {
throw new IllegalArgumentException("Only able to handle a single type in a list " + field.getName());
}
Class arrayClass = (Class) types[0];
List<Object> objectList = new ArrayList<Object>();
for (int i = 0; i < array.length(); ++i) {
if (arrayClass.isEnum()) {
objectList.add(getEnumValue(array.getString(i), arrayClass));
} else if (canDirectlySerializeClass(arrayClass)) {
objectList.add(array.get(i));
} else {
JSONObject jsonObject = array.getJSONObject(i);
if (jsonObject == null) {
objectList.add(null);
} else {
objectList.add(convertValue(jsonObject, arrayClass));
}
}
}
return objectList;
} else {
throw new IllegalArgumentException("only know how to deserialize List<?> on field " + field.getName());
}
}
use of java.lang.reflect.ParameterizedType in project guava by hceylan.
the class TypesTest method testNewParameterizedType.
public void testNewParameterizedType() {
ParameterizedType jvmType = (ParameterizedType) new TypeCapture<HashMap<String, int[][]>>() {
}.capture();
ParameterizedType ourType = Types.newParameterizedType(HashMap.class, String.class, int[][].class);
new EqualsTester().addEqualityGroup(jvmType, ourType).testEquals();
assertEquals(jvmType.toString(), ourType.toString());
assertEquals(jvmType.hashCode(), ourType.hashCode());
assertEquals(HashMap.class, ourType.getRawType());
ASSERT.that(ourType.getActualTypeArguments()).hasContentsInOrder(jvmType.getActualTypeArguments());
assertEquals(Arrays.asList(String.class, Types.newArrayType(Types.newArrayType(int.class))), Arrays.asList(ourType.getActualTypeArguments()));
assertEquals(null, ourType.getOwnerType());
}
use of java.lang.reflect.ParameterizedType in project guava by hceylan.
the class TypeTokenResolutionTest method testResolveType_parameterizedType.
public void testResolveType_parameterizedType() {
Parameterized<?, ?, ?> parameterized = new Parameterized<TypeTokenResolutionTest, Bar, String>() {
};
TypeResolver typeResolver = TypeResolver.accordingTo(parameterized.getClass());
ParameterizedType resolved = (ParameterizedType) typeResolver.resolve(parameterized.parameterizedType());
assertEquals(TypeTokenResolutionTest.class, resolved.getOwnerType());
assertEquals(Bar.class, resolved.getRawType());
ASSERT.that(resolved.getActualTypeArguments()).hasContentsInOrder(String.class);
}
use of java.lang.reflect.ParameterizedType in project guava by hceylan.
the class TypeTokenTest method testGetSupertype_withTypeVariable.
public void testGetSupertype_withTypeVariable() {
ParameterizedType expectedType = Types.newParameterizedType(Iterable.class, Types.newParameterizedType(List.class, ListIterable.class.getTypeParameters()[0]));
assertEquals(expectedType, TypeToken.of(ListIterable.class).getSupertype(Iterable.class).getType());
}
use of java.lang.reflect.ParameterizedType in project guava by hceylan.
the class TypeTokenTest method testResolveType_fromWildcard.
public void testResolveType_fromWildcard() throws Exception {
ParameterizedType withWildcardType = (ParameterizedType) new TypeCapture<Comparable<? extends Iterable<String>>>() {
}.capture();
TypeToken<?> wildcardType = TypeToken.of(withWildcardType.getActualTypeArguments()[0]);
assertEquals(String.class, wildcardType.resolveType(Iterable.class.getTypeParameters()[0]).getType());
}
Aggregations