Search in sources :

Example 81 with GenericArrayType

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

Example 82 with GenericArrayType

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);
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 83 with GenericArrayType

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();
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) GenericsType(org.codehaus.groovy.ast.GenericsType) ElementType(java.lang.annotation.ElementType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 84 with GenericArrayType

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

Example 85 with GenericArrayType

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

Aggregations

GenericArrayType (java.lang.reflect.GenericArrayType)137 ParameterizedType (java.lang.reflect.ParameterizedType)131 Type (java.lang.reflect.Type)105 TypeVariable (java.lang.reflect.TypeVariable)78 WildcardType (java.lang.reflect.WildcardType)76 Field (java.lang.reflect.Field)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Method (java.lang.reflect.Method)4 Collection (java.util.Collection)4 Test (org.junit.Test)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 ElementType (java.lang.annotation.ElementType)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 GenericType (javax.ws.rs.core.GenericType)3 XmlAccessType (javax.xml.bind.annotation.XmlAccessType)3 XmlType (javax.xml.bind.annotation.XmlType)3 Holder (javax.xml.ws.Holder)3 EqualsTester (com.google.common.testing.EqualsTester)2