Search in sources :

Example 11 with GenericArrayType

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

the class Reflector method getClass.

/**
     * Get the underlying class for a type, or null if the type is a variable
     * type.
     *
     * @param type the type
     * @return the underlying class
     */
public static Class<?> getClass(Type type) {
    if (type instanceof Class) {
        return (Class) type;
    } else if (type instanceof ParameterizedType) {
        return getClass(((ParameterizedType) type).getRawType());
    } else if (type instanceof GenericArrayType) {
        Type componentType = ((GenericArrayType) type).getGenericComponentType();
        Class<?> componentClass = getClass(componentType);
        if (componentClass != null) {
            return Array.newInstance(componentClass, 0).getClass();
        } else {
            return null;
        }
    } else {
        return null;
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 12 with GenericArrayType

use of java.lang.reflect.GenericArrayType 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 13 with GenericArrayType

use of java.lang.reflect.GenericArrayType 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)

Example 14 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project elasticsearch by elastic.

the class InjectorImpl method createTypeLiteralBinding.

/**
     * Converts a binding for a {@code Key<TypeLiteral<T>>} to the value {@code TypeLiteral<T>}. It's
     * a bit awkward because we have to pull out the inner type in the type literal.
     */
private <T> BindingImpl<TypeLiteral<T>> createTypeLiteralBinding(Key<TypeLiteral<T>> key, Errors errors) throws ErrorsException {
    Type typeLiteralType = key.getTypeLiteral().getType();
    if (!(typeLiteralType instanceof ParameterizedType)) {
        throw errors.cannotInjectRawTypeLiteral().toException();
    }
    ParameterizedType parameterizedType = (ParameterizedType) typeLiteralType;
    Type innerType = parameterizedType.getActualTypeArguments()[0];
    // this proves problematic, we can probably fix TypeLiteral to support type variables
    if (!(innerType instanceof Class) && !(innerType instanceof GenericArrayType) && !(innerType instanceof ParameterizedType)) {
        throw errors.cannotInjectTypeLiteralOf(innerType).toException();
    }
    // by definition, innerType == T, so this is safe
    @SuppressWarnings("unchecked") TypeLiteral<T> value = (TypeLiteral<T>) TypeLiteral.get(innerType);
    InternalFactory<TypeLiteral<T>> factory = new ConstantFactory<>(Initializables.of(value));
    return new InstanceBindingImpl<>(this, key, SourceProvider.UNKNOWN_SOURCE, factory, emptySet(), value);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) InstanceBindingImpl(org.elasticsearch.common.inject.internal.InstanceBindingImpl) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 15 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project morphia by mongodb.

the class MappedField method toClass.

protected Class toClass(final Type t) {
    if (t == null) {
        return null;
    } else if (t instanceof Class) {
        return (Class) t;
    } else if (t instanceof GenericArrayType) {
        final Type type = ((GenericArrayType) t).getGenericComponentType();
        Class aClass;
        if (type instanceof ParameterizedType) {
            aClass = (Class) ((ParameterizedType) type).getRawType();
        } else if (type instanceof TypeVariable) {
            aClass = ReflectionUtils.getTypeArgument(persistedClass, (TypeVariable<?>) type);
            if (aClass == null) {
                aClass = Object.class;
            }
        } else {
            aClass = (Class) type;
        }
        return Array.newInstance(aClass, 0).getClass();
    } else if (t instanceof ParameterizedType) {
        return (Class) ((ParameterizedType) t).getRawType();
    } else if (t instanceof WildcardType) {
        return (Class) ((WildcardType) t).getUpperBounds()[0];
    }
    throw new RuntimeException("Generic TypeVariable not supported!");
}
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) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) DBObject(com.mongodb.DBObject) 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