Example usage for java.lang Class getGenericSuperclass

List of usage examples for java.lang Class getGenericSuperclass

Introduction

In this page you can find the example usage for java.lang Class getGenericSuperclass.

Prototype

public Type getGenericSuperclass() 

Source Link

Document

Returns the Type representing the direct superclass of the entity (class, interface, primitive type or void) represented by this Class .

Usage

From source file:Main.java

/**
 * Resgata o tipo generico da classe//from www .  j av  a 2 s.c o  m
 * @param clazz Classe generics
 * @return Generic Type
 */
public static Class<?> getTypeGenericInstance(Class<?> clazz) {
    ParameterizedType parameterized = (ParameterizedType) clazz.getGenericSuperclass();
    Type[] arguments = parameterized.getActualTypeArguments();
    return (Class<?>) arguments[0];
}

From source file:Main.java

public static Type[] getGenericType(Class<?> clazz, Class<?> interfaceClazz) {
    Type st = clazz.getGenericSuperclass();
    Type[] ret = getActualTypeArguments(interfaceClazz, st);

    if (ret != null)
        return ret;

    for (Type t : clazz.getGenericInterfaces()) {
        ret = getActualTypeArguments(interfaceClazz, t);
        if (ret != null)
            return ret;
    }//from  w w w  .ja va2 s .  c om

    Class<?> s = clazz.getSuperclass();
    if (s == null || clazz.equals(s.getClass()))
        return new Type[0];

    return getGenericType(s, interfaceClazz);
}

From source file:Main.java

/**
 * Resgata o tipo generico da classe pelo seu index
 * @param clazz Classe generics/*  ww  w .ja va  2s  . co  m*/
 * @param i index do generic.
 * @return Generic Type
 */
public static Class<?> getTypeGenericInstance(Class<?> clazz, int i) {
    ParameterizedType parameterized = (ParameterizedType) clazz.getGenericSuperclass();
    Type[] arguments = parameterized.getActualTypeArguments();
    return (Class<?>) arguments[i];
}

From source file:Main.java

public static Class getSuperClassGenricType(Class clazz, int index) throws IndexOutOfBoundsException {

    Type genType = clazz.getGenericSuperclass();

    if (!(genType instanceof ParameterizedType)) {
        return Object.class;
    }/*ww  w  .  jav a  2s .  c o  m*/

    Type[] params = ((ParameterizedType) genType).getActualTypeArguments();

    if (index >= params.length || index < 0) {
        return Object.class;
    }
    if (!(params[index] instanceof Class)) {
        return Object.class;
    }
    return (Class) params[index];
}

From source file:Main.java

public static <T> Class<T> getTypeByIndex(Class<T> clz, int index) {
    if (clz == null) {
        return null;
    }/* w ww.  j a  v a2  s  .  c o  m*/
    Type type = clz.getGenericSuperclass();
    if (type == null) {
        return null;
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        Type[] types = parameterizedType.getActualTypeArguments();
        if (types != null && types.length > 0) {
            if (index >= 0 && index < types.length) {
                return (Class<T>) types[index];
            }
        }
    }
    return null;
}

From source file:Main.java

@NonNull
public static Class getListTypeClass(List<?> list) {
    Class elementType;/*from w ww  . j a v  a 2  s  .  co  m*/

    if (isEmpty(list)) {
        final Class<? extends List> listClass = list.getClass();
        final ParameterizedType genericSuperclass = (ParameterizedType) listClass.getGenericSuperclass();
        elementType = (Class) genericSuperclass.getActualTypeArguments()[0];
    } else {
        elementType = list.get(0).getClass();
    }

    return elementType;
}

From source file:GenericReflectionTest.java

public static void printClass(Class<?> cl) {
    System.out.print(cl);/*w  w  w  . j  a v  a 2 s. c om*/
    printTypes(cl.getTypeParameters(), "<", ", ", ">", true);
    Type sc = cl.getGenericSuperclass();
    if (sc != null) {
        System.out.print(" extends ");
        printType(sc, false);
    }
    printTypes(cl.getGenericInterfaces(), " implements ", ", ", "", false);
    System.out.println();
}

From source file:net.sourceforge.atunes.utils.ReflectionUtils.java

/**
 * Returns type arguments of a clazz/* ww w.j  a  v a2s .  c  o  m*/
 * 
 * @param clazz
 * @return
 */
public static Type[] getTypeArgumentsOfParameterizedType(final Class<?> clazz) {
    return ((ParameterizedType) clazz.getGenericSuperclass()).getActualTypeArguments();
}

From source file:com.luna.common.utils.ReflectUtils.java

/**
 * ??//from   ww  w  .ja  v  a 2s  . co m
 *
 * @param clazz
 * @param index
 * @param <T>
 * @return
 */
public static <T> Class<T> findParameterizedType(Class<?> clazz, int index) {
    Type parameterizedType = clazz.getGenericSuperclass();
    //CGLUB subclass target object()
    if (!(parameterizedType instanceof ParameterizedType)) {
        parameterizedType = clazz.getSuperclass().getGenericSuperclass();
    }
    if (!(parameterizedType instanceof ParameterizedType)) {
        return null;
    }
    Type[] actualTypeArguments = ((ParameterizedType) parameterizedType).getActualTypeArguments();
    if (actualTypeArguments == null || actualTypeArguments.length == 0) {
        return null;
    }
    return (Class<T>) actualTypeArguments[0];
}

From source file:com.nabla.wapp.server.general.Util.java

public static Class getGenericDeclaration(Class clazz, int position) {
    Assert.argumentNotNull(clazz);//from www .  ja va  2  s.  c  o  m
    Assert.state(position >= 0);

    Type classGenType = clazz.getGenericSuperclass();

    // special CGLIB workaround -- get generic superclass of superclass
    if (clazz.getName().contains("$$EnhancerByCGLIB$$")) {
        classGenType = clazz.getSuperclass().getGenericSuperclass();
    }

    if (classGenType instanceof ParameterizedType) {
        Type[] params = ((ParameterizedType) classGenType).getActualTypeArguments();

        if ((params != null) && (params.length > position)) {
            return (Class) params[position];
        }
    }

    for (Type ifGenType : clazz.getGenericInterfaces()) {
        if (ifGenType instanceof ParameterizedType) {
            Type[] params = ((ParameterizedType) ifGenType).getActualTypeArguments();

            if ((params != null) && (params.length > position)) {
                return (Class) params[position];
            }
        }
    }

    if (log.isErrorEnabled())
        log.error("fail to find class of generic parameter " + position + " of class '" + clazz.getSimpleName()
                + "'");

    return null;
}