Java Reflection Generic Type from Class getGenericSuperclass(Class clazz)

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

Description

get Generic Superclass

License

LGPL

Declaration

@SuppressWarnings("rawtypes")
public static Class[] getGenericSuperclass(Class<?> clazz) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

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

public class Main {

    @SuppressWarnings("rawtypes")
    public static Class[] getGenericSuperclass(Class<?> clazz) {
        try {/*w  ww .  j  ava 2 s  .  co m*/
            Type typeGeneric = clazz.getGenericSuperclass();
            if (typeGeneric != null) {
                if (typeGeneric instanceof ParameterizedType) {
                    return getGeneric((ParameterizedType) typeGeneric);
                }
            }
        } catch (Exception e) {
        }
        return null;
    }

    @SuppressWarnings("rawtypes")
    public static Class[] getGeneric(ParameterizedType type) {
        try {
            if (type != null) {
                Type[] typeArgs = type.getActualTypeArguments();
                if (typeArgs != null && typeArgs.length > 0) {
                    Class[] args = new Class[typeArgs.length];
                    for (int i = 0; i < typeArgs.length; i++) {
                        Type arg = typeArgs[i];
                        args[i] = (Class) arg;
                    }
                    return args;
                }
            }
        } catch (Exception e) {
        }
        return null;
    }
}

Related

  1. getGenericParameters(Class clazz)
  2. getGenericParameterType(Class clazz, Integer parameterIndex)
  3. getGenericParameterType0(Class cls, Class genericSuper, int paramIndex)
  4. getGenerics(Class classType)
  5. getGenericsClass(Class cls)
  6. getGenericSuperclass(Class cls)
  7. getGenericSuperclassActualTypes(Collection types, Class aClass)
  8. getGenericSuperType(Class clz, int index)
  9. getGenericSuperType(Class subclass, Class classWithParameter)