Example usage for java.lang.reflect ParameterizedType getActualTypeArguments

List of usage examples for java.lang.reflect ParameterizedType getActualTypeArguments

Introduction

In this page you can find the example usage for java.lang.reflect ParameterizedType getActualTypeArguments.

Prototype

Type[] getActualTypeArguments();

Source Link

Document

Returns an array of Type objects representing the actual type arguments to this type.

Usage

From source file:Main.java

@NonNull
public static Class getListTypeClass(List<?> list) {
    Class elementType;/*from  ww  w .  jav  a  2 s.co  m*/

    if (isEmpty(list)) {
        final Class<? extends List> listClass = list.getClass();
        final ParameterizedType genericSuperclass = (ParameterizedType) listClass.getGenericSuperclass();
        elementType = (Class) genericSuperclass.getActualTypeArguments()[0];
    } else {
        elementType = list.get(0).getClass();
    }

    return elementType;
}

From source file:com.netflix.hystrix.contrib.javanica.utils.TypeHelper.java

/**
 * Unwinds parametrized type into plain list that contains all parameters for the given type including nested parameterized types,
 * for example calling the method for the following type
 * <code>//  w w  w.ja  va  2  s  . c  om
 * GType<GType<GDoubleType<GType<GDoubleType<Parent, Parent>>, Parent>>>
 * </code>
 * will return list of 8 elements:
 * <code>
 * [GType, GType, GDoubleType, GType, GDoubleType, Parent, Parent, Parent]
 * </code>
 * if the given type is not parametrized then returns list with one element which is given type passed into method.
 *
 * @param type the parameterized type
 * @return list of {@link Type}
 */
@ParametersAreNonnullByDefault
public static List<Type> getAllParameterizedTypes(Type type) {
    Validate.notNull(type, "type cannot be null");
    List<Type> types = new ArrayList<Type>();
    TreeTraverser<Type> typeTraverser = new TreeTraverser<Type>() {
        @Override
        public Iterable<Type> children(Type root) {
            if (root instanceof ParameterizedType) {
                ParameterizedType pType = (ParameterizedType) root;
                return Arrays.asList(pType.getActualTypeArguments());

            }
            return Collections.emptyList();
        }
    };
    for (Type t : typeTraverser.breadthFirstTraversal(type)) {
        types.add(t);
    }
    return types;
}

From source file:com.comcast.cereal.impl.ReflectionHelper.java

public static Class<?> getGenericClass(Type type, int arg) {
    if ((null == type) || !ParameterizedType.class.isAssignableFrom(type.getClass())) {
        return null;
    }//  w w  w . j  a  v a2s .  c  o m

    try {
        ParameterizedType paramType = (ParameterizedType) type;
        return (Class<?>) paramType.getActualTypeArguments()[arg];
    } catch (Throwable throwable) {
        return null;
    }
}

From source file:com.github.jasonruckman.sidney.core.JavaTypeRefBuilder.java

public static TypeRef buildTypeRef(Type type, TypeBindings parentBindings, Field field) {
    JavaType jt;//from  w  ww. j a  va 2 s  . com
    TypeBindings typeBindings;

    typeBindings = Types.binding(type, parentBindings);
    jt = Types.type(type, parentBindings);
    TypeRef ref;
    if (field == null) {
        ref = new TypeRef(jt.getRawClass());
    } else {
        Hint hint = field.getAnnotation(Hint.class);
        if (hint != null) {
            ref = new TypeRef.TypeFieldRef(hint.value(), field);
        } else {
            ref = new TypeRef.TypeFieldRef(jt.getRawClass(), field);
        }
    }

    for (Field subField : Fields.getAllFields(jt.getRawClass())) {
        Type subType = subField.getGenericType();
        TypeRef subRef = buildTypeRef(subType, typeBindings, subField);
        ref.addField((TypeRef.TypeFieldRef) subRef);
    }

    if (ParameterizedType.class.isAssignableFrom(type.getClass())) {
        ParameterizedType t = (ParameterizedType) type;
        for (Type actualTypeArg : t.getActualTypeArguments()) {
            ref.addTypeParameter(buildTypeRef(actualTypeArg, parentBindings, null));
        }
    } else if (TypeVariable.class.isAssignableFrom(type.getClass())) {
        TypeVariable t = (TypeVariable) type;
        for (Type typeBound : t.getBounds()) {
            ref.addTypeParameter(buildTypeRef(typeBound, parentBindings, null));
        }
    } else if (GenericArrayType.class.isAssignableFrom(type.getClass())) {
        GenericArrayType t = (GenericArrayType) type;
        ref.addTypeParameter(buildTypeRef(t.getGenericComponentType(), parentBindings, null));
    }

    if (jt.isArrayType() && !GenericArrayType.class.isAssignableFrom(type.getClass())) {
        ArrayType arrType = (ArrayType) jt;
        ref.addTypeParameter(buildTypeRef(arrType.getContentType().getRawClass(), null, null));
    }

    return ref;
}

From source file:org.openlegacy.utils.ReflectionUtil.java

public static Class<?> getListType(Field field) {
    try {//from w w  w  .j  a va2s.co  m
        if (!(field.getGenericType() instanceof ParameterizedType)) {
            return null;
        }

        ParameterizedType type = (ParameterizedType) field.getGenericType();
        if (type.getActualTypeArguments().length == 0) {
            return null;
        }
        if (!(type.getActualTypeArguments()[0] instanceof Class)) {
            return null;
        }
        Class<?> listType = (Class<?>) type.getActualTypeArguments()[0];
        return listType;
    } catch (Exception e) {
        throw (new IllegalArgumentException(e));
    }

}

From source file:py.una.pol.karaku.util.KarakuReflectionUtils.java

/**
 * Retorna el tipo parmetrico implementado por <i>root</i> de un nivel de
 * jerarqua./*from  ww w. jav  a 2s . c om*/
 * 
 * @param leaf
 *            clase raz (implementacin final)
 * @param index
 *            nmero de clase paramtrica
 * @return clase paramtrica.
 */
@SuppressWarnings("unchecked")
@Nonnull
public static <T> Class<T> getParameterizedClass(@Nonnull Object leaf, int index) {

    ParameterizedType type = (ParameterizedType) leaf.getClass().getGenericSuperclass();

    if (type.getActualTypeArguments().length <= index) {
        throw new KarakuRuntimeException(
                "Cant get the parameterizedClass of claas " + leaf.getClass().getName());
    }
    Class<T> clazz = (Class<T>) type.getActualTypeArguments()[index];
    if (clazz != null) {
        return clazz;
    } else {
        // no va a pasar.
        throw new KarakuRuntimeException(
                "Cant get the parameterizedClass of claas " + leaf.getClass().getName());
    }
}

From source file:com.google.api.server.spi.config.jsonwriter.JacksonResourceSchemaProvider.java

private static boolean hasUnresolvedType(TypeToken<?> type) {
    Type javaType = type.getType();
    if (javaType instanceof ParameterizedType) {
        ParameterizedType p = (ParameterizedType) javaType;
        for (Type t : p.getActualTypeArguments()) {
            if (Types.isWildcardType(type.resolveType(t))) {
                logger.warning("skipping field of type " + type + " because it is unresolved");
                return true;
            }//w  w w  .j av a2s  .  c om
        }
    }
    return false;
}

From source file:ch.ifocusit.plantuml.utils.ClassUtils.java

public static Set<Class> getGenericTypes(ParameterizedType type) {
    return Stream.of(type.getActualTypeArguments()).filter(Class.class::isInstance).map(Class.class::cast)
            .collect(Collectors.toSet());
}

From source file:com.bstek.dorado.util.clazz.TypeInfo.java

public static TypeInfo parse(ParameterizedType type, boolean aggregated) throws ClassNotFoundException {
    Class<?> classType;//from w w  w  .jav  a2  s .  co  m
    try {
        classType = (Class<?>) type.getActualTypeArguments()[0];
    } catch (Exception e) {
        classType = Object.class;
    }
    return new TypeInfo(classType, aggregated);
}

From source file:kina.utils.AnnotationUtils.java

/**
 * Returns the list of generic types associated to the provided field (if any).
 *
 * @param field the field instance to process.
 * @return the list of generic types associated to the provided field (if any).
 *//*from  ww w  .ja  va 2 s.  c  om*/
public static Class[] getGenericTypes(java.lang.reflect.Field field) {
    try {
        ParameterizedType type = (ParameterizedType) field.getGenericType();
        Type[] types = type.getActualTypeArguments();

        Class<?>[] res = new Class<?>[types.length];

        for (int i = 0; i < types.length; i++) {
            res[i] = (Class<?>) types[i];
        }

        return res;

    } catch (ClassCastException e) {
        return new Class<?>[] { (Class<?>) field.getGenericType() };
    }
}