Java Reflection Generic Type from Class getGenericParameterClass(Class clazz)

Here you can find the source of getGenericParameterClass(Class clazz)

Description

Gets the first generic between class parents and return's its first parameter type

License

Open Source License

Parameter

Parameter Description
clazz a parameter

Declaration

public static String getGenericParameterClass(Class clazz) 

Method Source Code


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

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

public class Main {
    /**// w ww. j  a  va2 s  . c  om
     * Gets the first generic between class parents and return's its first parameter type
     * @param clazz
     * @return
     */
    public static String getGenericParameterClass(Class clazz) {

        Type type = clazz.getGenericSuperclass();
        while (!(type instanceof ParameterizedType)) {
            clazz = clazz.getSuperclass();
            if (clazz == null) {
                return null;
            }
            type = clazz.getGenericSuperclass();
        }
        String genericTypeName = ((ParameterizedType) type).getActualTypeArguments()[0].getTypeName();

        return genericTypeName;
    }
}

Related

  1. getGenericInterfaceParamType(Class cls, Class rawType)
  2. getGenericInterfacesActualTypes(Collection types, Class aClass)
  3. getGenericParameter(Class clazz, int index)
  4. getGenericParameter(Type class1, int number)
  5. getGenericParameterClass(Class actualClass, int parameterIndex)
  6. getGenericParameters(Class clazz)
  7. getGenericParameterType(Class clazz, Integer parameterIndex)
  8. getGenericParameterType0(Class cls, Class genericSuper, int paramIndex)
  9. getGenerics(Class classType)