use of java.lang.reflect.GenericArrayType in project guava by hceylan.
the class TypeTokenResolutionTest method testResolveToGenericArrayType.
public void testResolveToGenericArrayType() {
GenericArrayType arrayType = (GenericArrayType) new Holder<List<int[][]>[]>() {
}.getContentType();
ParameterizedType listType = (ParameterizedType) arrayType.getGenericComponentType();
assertEquals(List.class, listType.getRawType());
assertEquals(Types.newArrayType(int[].class), listType.getActualTypeArguments()[0]);
}
use of java.lang.reflect.GenericArrayType in project guava by hceylan.
the class TypeToken method toGenericType.
/**
* Returns the type token representing the generic type declaration of {@code cls}. For example:
* {@code TypeToken.getGenericType(Iterable.class)} returns {@code Iterable<T>}.
*
* <p>If {@code cls} isn't parameterized and isn't a generic array, the type token of the class is
* returned.
*/
@VisibleForTesting
static <T> TypeToken<? extends T> toGenericType(Class<T> cls) {
if (cls.isArray()) {
Type arrayOfGenericType = Types.newArrayType(// If we are passed with int[].class, don't turn it to GenericArrayType
toGenericType(cls.getComponentType()).runtimeType);
// array is covariant
@SuppressWarnings("unchecked") TypeToken<? extends T> result = (TypeToken<? extends T>) of(arrayOfGenericType);
return result;
}
TypeVariable<Class<T>>[] typeParams = cls.getTypeParameters();
if (typeParams.length > 0) {
// Like, it's Iterable<T> for Iterable.class
@SuppressWarnings("unchecked") TypeToken<? extends T> type = (TypeToken<? extends T>) of(Types.newParameterizedType(cls, typeParams));
return type;
} else {
return of(cls);
}
}
use of java.lang.reflect.GenericArrayType in project groovy-core by groovy.
the class Java5 method configureGenericArray.
private ClassNode configureGenericArray(GenericArrayType genericArrayType) {
Type component = genericArrayType.getGenericComponentType();
ClassNode node = configureType(component);
return node.makeArray();
}
use of java.lang.reflect.GenericArrayType in project guava by google.
the class TypeTokenResolutionTest method testResolveToGenericArrayType.
public void testResolveToGenericArrayType() {
GenericArrayType arrayType = (GenericArrayType) new Holder<List<int[][]>[]>() {
}.getContentType();
ParameterizedType listType = (ParameterizedType) arrayType.getGenericComponentType();
assertEquals(List.class, listType.getRawType());
assertEquals(Types.newArrayType(int[].class), listType.getActualTypeArguments()[0]);
}
use of java.lang.reflect.GenericArrayType in project j2objc by google.
the class GenericArrayTypeTest method testParameterizedComponentType.
public void testParameterizedComponentType() throws Exception {
@SuppressWarnings("unchecked") Class<? extends B> clazz = GenericArrayTypeTest.B.class;
Field field = clazz.getDeclaredField("array");
Type genericType = field.getGenericType();
assertInstanceOf(GenericArrayType.class, genericType);
GenericArrayType arrayType = (GenericArrayType) genericType;
Type componentType = arrayType.getGenericComponentType();
assertInstanceOf(ParameterizedType.class, componentType);
ParameterizedType parameteriezdType = (ParameterizedType) componentType;
assertEquals(clazz, parameteriezdType.getRawType());
assertEquals(clazz.getTypeParameters()[0], parameteriezdType.getActualTypeArguments()[0]);
}
Aggregations