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:utils.ReflectionService.java

public static Map createClassModel(Class clazz, int maxDeep) {
    Map map = new HashMap();
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);/*from   ww w.  ja va2s  .co m*/
        String key = field.getName();
        try {
            Class<?> type;
            if (Collection.class.isAssignableFrom(field.getType())) {
                ParameterizedType collectionType = (ParameterizedType) field.getGenericType();
                type = (Class<?>) collectionType.getActualTypeArguments()[0];
            } else {
                type = field.getType();
            }

            //            System.out.println(key + " ReflectionResource.createClassModel ==== putting type: " + field.getType().getName() + ", static: "
            //            + Modifier.isStatic(field.getModifiers()) + ", enum: " + type.isEnum() + ", java.*: " + type.getName().startsWith("java"));

            if (Modifier.isStatic(field.getModifiers())) {
                continue;
            } else if (type.isEnum()) {
                map.put(key, Enum.class.getName());
            } else if (!type.getName().startsWith("java") && !key.startsWith("_") && maxDeep > 0) {
                map.put(key, createClassModel(type, maxDeep - 1));
            } else {
                map.put(key, type.getName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return map;
}

From source file:org.lightadmin.core.util.DomainConfigurationUtils.java

public static Class<?> configurationDomainType(Class clazz) {
    if (isAnnotationBasedConfigurationCandidate(clazz)) {
        final Administration annotation = findAnnotation(clazz, Administration.class);

        return annotation == null ? null : annotation.value();
    }/*  ww  w  .j  a v a2s. c om*/

    if (isSuperClassBasedConfigurationCandidate(clazz)) {
        final ParameterizedType genericSuperclass = (ParameterizedType) clazz.getGenericSuperclass();

        return (Class<?>) genericSuperclass.getActualTypeArguments()[0];
    }

    throw new RuntimeException("Unknown configuration candidate");
}

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

public static String getParameterizedTypeName(ParameterizedType genericType) {
    // manage generics
    String subtypes = Stream.of(genericType.getActualTypeArguments()).map(ClassUtils::getSimpleName)
            .collect(Collectors.joining(GENERICS_SEP));
    return GENERICS_OPEN + subtypes + GENERICS_CLOSE;
}

From source file:com.bosscs.spark.commons.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. j  ava  2 s.  co m
public static Class[] getGenericTypes(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() };
    }
}

From source file:com.liferay.apio.architect.internal.annotation.representor.processor.TypeProcessor.java

private static java.lang.reflect.Type _getGenericType(Method method) {
    ParameterizedType parameterizedType = (ParameterizedType) method.getGenericReturnType();

    return parameterizedType.getActualTypeArguments()[0];
}

From source file:ca.uhn.fhir.util.ReflectionUtil.java

public static Class<?> getGenericCollectionTypeOfField(Field next) {
    Class<?> type;/*from w  ww  . ja va 2  s.  c  o m*/
    ParameterizedType collectionType = (ParameterizedType) next.getGenericType();
    Type firstArg = collectionType.getActualTypeArguments()[0];
    if (ParameterizedType.class.isAssignableFrom(firstArg.getClass())) {
        ParameterizedType pt = ((ParameterizedType) firstArg);
        type = (Class<?>) pt.getRawType();
    } else {
        type = (Class<?>) firstArg;
    }
    return type;
}

From source file:utils.ReflectionService.java

public static JsonNode createJsonNode(Class clazz, int maxDeep) {

    if (maxDeep <= 0 || JsonNode.class.isAssignableFrom(clazz)) {
        return null;
    }/* ww w.  j  a  va2  s . c  o m*/

    ObjectMapper objectMapper = new ObjectMapper();
    ObjectNode objectNode = objectMapper.createObjectNode();

    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        String key = field.getName();
        try {
            // is array or collection
            if (field.getType().isArray() || Collection.class.isAssignableFrom(field.getType())) {
                ParameterizedType collectionType = (ParameterizedType) field.getGenericType();
                Class<?> type = (Class<?>) collectionType.getActualTypeArguments()[0];
                ArrayNode arrayNode = objectMapper.createArrayNode();
                arrayNode.add(createJsonNode(type, maxDeep - 1));
                objectNode.put(key, arrayNode);
            } else {
                Class<?> type = field.getType();
                if (Modifier.isStatic(field.getModifiers())) {
                    continue;
                } else if (type.isEnum()) {
                    objectNode.put(key, Enum.class.getName());
                } else if (!type.getName().startsWith("java") && !key.startsWith("_")) {
                    objectNode.put(key, createJsonNode(type, maxDeep - 1));
                } else {
                    objectNode.put(key, type.getName());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return objectNode;
}

From source file:ca.uhn.fhir.util.ReflectionUtil.java

public static Class<?> getGenericCollectionTypeOfMethodParameter(Method theMethod, int theParamIndex) {
    Class<?> type;//from  w w w . j  a  v  a  2s  .  c o  m
    Type genericParameterType = theMethod.getGenericParameterTypes()[theParamIndex];
    if (Class.class.equals(genericParameterType)) {
        return null;
    }
    ParameterizedType collectionType = (ParameterizedType) genericParameterType;
    Type firstArg = collectionType.getActualTypeArguments()[0];
    if (ParameterizedType.class.isAssignableFrom(firstArg.getClass())) {
        ParameterizedType pt = ((ParameterizedType) firstArg);
        type = (Class<?>) pt.getRawType();
    } else {
        type = (Class<?>) firstArg;
    }
    return type;
}

From source file:ca.uhn.fhir.util.ReflectionUtil.java

/**
 * For a field of type List<Enumeration<Foo>>, returns Foo
 *//*from  w w w. j a  v  a2  s .c o m*/
public static Class<?> getGenericCollectionTypeOfFieldWithSecondOrderForList(Field next) {
    if (!List.class.isAssignableFrom(next.getType())) {
        return getGenericCollectionTypeOfField(next);
    }

    Class<?> type;
    ParameterizedType collectionType = (ParameterizedType) next.getGenericType();
    Type firstArg = collectionType.getActualTypeArguments()[0];
    if (ParameterizedType.class.isAssignableFrom(firstArg.getClass())) {
        ParameterizedType pt = ((ParameterizedType) firstArg);
        Type pt2 = pt.getActualTypeArguments()[0];
        return (Class<?>) pt2;
    }
    type = (Class<?>) firstArg;
    return type;
}

From source file:ca.uhn.fhir.util.ReflectionUtil.java

@SuppressWarnings({ "rawtypes" })
public static Class<?> getGenericCollectionTypeOfMethodReturnType(Method theMethod) {
    Class<?> type;//from   ww  w  .  j  a v  a 2  s.c om
    Type genericReturnType = theMethod.getGenericReturnType();
    if (!(genericReturnType instanceof ParameterizedType)) {
        return null;
    }
    ParameterizedType collectionType = (ParameterizedType) genericReturnType;
    Type firstArg = collectionType.getActualTypeArguments()[0];
    if (ParameterizedType.class.isAssignableFrom(firstArg.getClass())) {
        ParameterizedType pt = ((ParameterizedType) firstArg);
        type = (Class<?>) pt.getRawType();
    } else if (firstArg instanceof TypeVariable<?>) {
        Type decl = ((TypeVariable) firstArg).getBounds()[0];
        return (Class<?>) decl;
    } else if (firstArg instanceof WildcardType) {
        Type decl = ((WildcardType) firstArg).getUpperBounds()[0];
        return (Class<?>) decl;
    } else {
        type = (Class<?>) firstArg;
    }
    return type;
}