Search in sources :

Example 81 with ParameterizedType

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

the class AbstractTypeDescriptionGenericTest method testNestedStaticTypeVariableType.

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

Example 82 with ParameterizedType

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

the class TypeLiteral method getSuperclassTypeParameter.

/**
   * Returns the type from super class's type parameter in {@link MoreTypes#canonicalize(Type)
   * canonical form}.
   */
static Type getSuperclassTypeParameter(Class<?> subclass) {
    Type superclass = MoreTypes.getGenericSuperclass(subclass);
    if (superclass instanceof Class) {
        throw new RuntimeException("Missing type parameter.");
    }
    ParameterizedType parameterized = (ParameterizedType) superclass;
    return canonicalize(parameterized.getActualTypeArguments()[0]);
}
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 83 with ParameterizedType

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

the class MiniGuice method requireKey.

private void requireKey(Key key, Object requiredBy) {
    if (key.type instanceof ParameterizedType && (((ParameterizedType) key.type).getRawType() == Provider.class || ((ParameterizedType) key.type).getRawType() == javax.inject.Provider.class)) {
        Type type = ((ParameterizedType) key.type).getActualTypeArguments()[0];
        key = new Key(type, key.annotation);
    }
    requiredKeys.add(new RequiredKey(key, requiredBy));
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Provider(javax.inject.Provider)

Example 84 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project centipede by paulhoule.

the class TestAnnotationExtraction method testDefaults.

@Test
public void testDefaults() {
    // 8 primitive types get defaults from the JLS
    assertEquals((byte) 0, defaultValueFor(Byte.TYPE));
    assertEquals((short) 0, defaultValueFor(Short.TYPE));
    assertEquals(0, defaultValueFor(Integer.TYPE));
    assertEquals(0L, defaultValueFor(Long.TYPE));
    assertEquals(0.0f, defaultValueFor(Float.TYPE));
    assertEquals(0.0, defaultValueFor(Double.TYPE));
    assertEquals(false, defaultValueFor(Boolean.TYPE));
    assertEquals('\0', defaultValueFor(Character.TYPE));
    // For a string we get the empty String
    assertEquals("", defaultValueFor(String.class));
    assertEquals(new ArrayList(), defaultValueFor(List.class));
    ParameterizedType t = (ParameterizedType) ((new ArrayList<Exception>()).getClass().getGenericSuperclass());
    assertEquals(new ArrayList<Exception>(), defaultValueFor(t));
    assertEquals(new ArrayList<Long>(), defaultValueFor(t));
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 85 with ParameterizedType

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

the class Types method appendGenericType.

public static void appendGenericType(StringBuilder out, Type type) {
    if (type instanceof TypeVariable) {
        out.append(((TypeVariable) type).getName());
    } else if (type instanceof ParameterizedType) {
        out.append(type.toString());
    } else if (type instanceof GenericArrayType) {
        Type simplified = ((GenericArrayType) type).getGenericComponentType();
        appendGenericType(out, simplified);
        out.append("[]");
    } else if (type instanceof Class) {
        Class c = (Class<?>) type;
        if (c.isArray()) {
            String[] as = c.getName().split("\\[");
            int len = as.length - 1;
            if (as[len].length() > 1) {
                out.append(as[len].substring(1, as[len].length() - 1));
            } else {
                char ch = as[len].charAt(0);
                if (ch == 'I') {
                    out.append("int");
                } else if (ch == 'B') {
                    out.append("byte");
                } else if (ch == 'J') {
                    out.append("long");
                } else if (ch == 'F') {
                    out.append("float");
                } else if (ch == 'D') {
                    out.append("double");
                } else if (ch == 'S') {
                    out.append("short");
                } else if (ch == 'C') {
                    out.append("char");
                } else if (ch == 'Z') {
                    out.append("boolean");
                } else if (ch == 'V') {
                    out.append("void");
                }
            }
            for (int i = 0; i < len; i++) {
                out.append("[]");
            }
        } else {
            out.append(c.getName());
        }
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

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