Search in sources :

Example 61 with ParameterizedType

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());
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONObject(org.json.JSONObject)

Example 62 with ParameterizedType

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());
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) EqualsTester(com.google.common.testing.EqualsTester)

Example 63 with ParameterizedType

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);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType)

Example 64 with ParameterizedType

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());
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 65 with ParameterizedType

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());
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType)

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