Java Reflection Generic Parameter getGenericTypes(Type[] genericParameterTypes)

Here you can find the source of getGenericTypes(Type[] genericParameterTypes)

Description

Returns a list of class's for every generic parameter type given by Method#getGenericParameterTypes() or Class#getGenericInterfaces()

License

Open Source License

Parameter

Parameter Description
genericParameterTypes The genericParameterTypes mentioned above

Return

The list of types as class's

Declaration

public static List<Class<?>> getGenericTypes(Type[] genericParameterTypes) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.*;
import java.util.*;

public class Main {
    /**//  ww w.j  av  a2  s.  c om
     * Returns a list of class's for every generic parameter type
     * given by {@link Method#getGenericParameterTypes()} or {@link Class#getGenericInterfaces()}
     *
     * @param genericParameterTypes The genericParameterTypes mentioned above
     * @return The list of types as class's
     * @see #getGenericType(Type[])
     */
    public static List<Class<?>> getGenericTypes(Type[] genericParameterTypes) {
        List<Class<?>> l = new ArrayList<>();
        if (genericParameterTypes == null)
            return l;

        try {
            for (Type t : genericParameterTypes) {
                ParameterizedType pType = (ParameterizedType) t;
                for (Type type : pType.getActualTypeArguments()) {
                    l.add((Class<?>) type);
                }
            }
        } catch (Exception e) {
            // couldn't fetch them .. just returning an empty list ..
            return new ArrayList<>();
        }
        return l;
    }
}

Related

  1. getGenericParamType1(Type parameterizedType)
  2. getGenericParamTypes(Type genericType)
  3. getGenericType(Parameter param)
  4. getGenericTypeParameter(Type genericType)
  5. getGenericTypes(ParameterizedType t)