Search in sources :

Example 76 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project Genius-Android by qiujuer.

the class Reflector method replaceTypeActualArgument.

/**
     * Replace {@link ParameterizedType#getActualTypeArguments()} method return value.
     * In this we use {@link ParameterizeTypeActualArgsDelegate} delegate {@link ParameterizedType};
     * Let {@link ParameterizedType#getActualTypeArguments()} return really class type.
     *
     * @param inType        Type
     * @param resolvedTypes a Map<Type, Type>, {@link #getActualTypeArguments(Class, Class)}
     * @return {@link ParameterizeTypeActualArgsDelegate}
     */
private static Type replaceTypeActualArgument(Type inType, final Map<Type, Type> resolvedTypes) {
    Type outType = inType;
    if (inType instanceof ParameterizedType) {
        final ParameterizedType finalType = ((ParameterizedType) inType);
        final Type[] actualArgs = ((ParameterizedType) inType).getActualTypeArguments();
        for (int i = 0; i < actualArgs.length; i++) {
            Type argType = actualArgs[i];
            while (resolvedTypes.containsKey(argType)) {
                argType = resolvedTypes.get(argType);
            }
            // Do replace ActualArgument
            argType = replaceTypeActualArgument(argType, resolvedTypes);
            actualArgs[i] = argType;
        }
        outType = new ParameterizeTypeActualArgsDelegate(finalType, actualArgs);
    }
    return outType;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) SuppressLint(android.annotation.SuppressLint)

Example 77 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project roboguice by roboguice.

the class TypesTest method testDefensiveCopies.

public void testDefensiveCopies() {
    Type[] arguments = new Type[] { String.class, Integer.class };
    ParameterizedType parameterizedType = Types.newParameterizedType(Map.class, arguments);
    arguments[0] = null;
    assertEquals(String.class, parameterizedType.getActualTypeArguments()[0]);
    parameterizedType.getActualTypeArguments()[1] = null;
    assertEquals(Integer.class, parameterizedType.getActualTypeArguments()[1]);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 78 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project roboguice by roboguice.

the class TypesTest method testEqualsAndHashcode.

public void testEqualsAndHashcode() {
    ParameterizedType parameterizedType = Types.newParameterizedType(Map.class, String.class, Integer.class);
    assertEqualsBothWays(mapStringInteger, parameterizedType);
    assertEquals(mapStringInteger.toString(), parameterizedType.toString());
    GenericArrayType genericArrayType = Types.arrayOf(Types.arrayOf(Types.newParameterizedType(Set.class, String.class)));
    assertEqualsBothWays(setStringArray, genericArrayType);
    assertEquals(setStringArray.toString(), genericArrayType.toString());
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 79 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project roboguice by roboguice.

the class TypesTest method testInnerParameterizedEvenWithZeroArgs.

public void testInnerParameterizedEvenWithZeroArgs() {
    TypeLiteral<Outer<String>.Inner<String>> type = new TypeLiteral<Outer<String>.Inner<String>>() {
    };
    assertEqualsBothWays(outerInner, type.getType());
    ParameterizedType parameterizedType = (ParameterizedType) type.getType();
    assertEquals(0, parameterizedType.getActualTypeArguments().length);
    assertEquals(new TypeLiteral<Outer<String>>() {
    }.getType(), parameterizedType.getOwnerType());
    assertEquals(Outer.Inner.class, parameterizedType.getRawType());
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) TypeLiteral(com.google.inject.TypeLiteral)

Example 80 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project byte-buddy by raphw.

the class AbstractTypeDescriptionGenericTest method testNestedSpecifiedTypeVariableType.

@Test
public void testNestedSpecifiedTypeVariableType() throws Exception {
    TypeDescription.Generic typeDescription = describeType(NestedSpecifiedTypeVariableType.class.getDeclaredField(FOO));
    assertThat(typeDescription.getTypeName(), is(NestedSpecifiedTypeVariableType.class.getDeclaredField(FOO).getGenericType().toString()));
    assertThat(typeDescription.getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
    assertThat(typeDescription.getStackSize(), is(StackSize.SINGLE));
    assertThat(typeDescription.getTypeArguments().size(), is(0));
    Type ownerType = ((ParameterizedType) NestedSpecifiedTypeVariableType.class.getDeclaredField(FOO).getGenericType()).getOwnerType();
    assertThat(typeDescription.getOwnerType(), is(TypeDefinition.Sort.describe(ownerType)));
    assertThat(typeDescription.getOwnerType().getSort(), is(TypeDefinition.Sort.PARAMETERIZED));
    assertThat(typeDescription.getOwnerType().getTypeArguments().size(), is(1));
    assertThat(typeDescription.getOwnerType().getTypeArguments().getOnly().getSort(), is(TypeDefinition.Sort.NON_GENERIC));
    assertThat(typeDescription.getOwnerType().getTypeArguments().getOnly(), is((TypeDefinition) new TypeDescription.ForLoadedType(String.class)));
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Test(org.junit.Test)

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