Java Reflection Generic Parameter getGenericParamTypes(Type genericType)

Here you can find the source of getGenericParamTypes(Type genericType)

Description

get Generic Param Types

License

Open Source License

Declaration

public static Class<?>[] getGenericParamTypes(Type genericType) 

Method Source Code

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

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

public class Main {
    public static Class<?>[] getGenericParamTypes(Type genericType) {
        Objects.requireNonNull(genericType, "Generic type is NULL!");

        try {/* w w w .j av a  2s .  c om*/
            if (genericType instanceof ParameterizedType) {
                Type[] genericTypes = ((ParameterizedType) genericType).getActualTypeArguments();
                Class<?>[] result = new Class<?>[genericTypes.length];

                for (int i = 0; i < genericTypes.length; i++) {
                    if (genericTypes[i] instanceof Class<?>) {
                        result[i] = (Class<?>) genericTypes[i];
                    } else if (genericTypes[i] instanceof ParameterizedType) {
                        Type rawType = ((ParameterizedType) genericTypes[i]).getRawType();
                        result[i] = (Class<?>) rawType;
                    } else {
                        // ignore it
                    }
                }

                return result;
            }
        } catch (Exception e) {
            ; // ignore it
        }

        return new Class<?>[0];
    }
}

Related

  1. getGenericParameterTypes(Method m)
  2. getGenericParameterTypes(Type t)
  3. getGenericParamType(Type type)
  4. getGenericParamType(Type type)
  5. getGenericParamType1(Type parameterizedType)
  6. getGenericType(Parameter param)
  7. getGenericTypeParameter(Type genericType)
  8. getGenericTypes(ParameterizedType t)
  9. getGenericTypes(Type[] genericParameterTypes)