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:com.dapeng.dao.orm.GenericsUtils.java

/**
 * ??, Class?./*from w w  w.j  a v  a2 s  . c  o  m*/
 * , Object.class.
 * 
 * public UserDao extends HibernateDao<User,Long>
 *
 * @param clazz clazz The class to introspect
 * @param index the Index of the generic ddeclaration,start from 0.
 * @return the index generic declaration, or Object.class if cannot be determined
 */
public static Class getSuperClassGenricType(final Class clazz, final int index) {

    Type genType = clazz.getGenericSuperclass();

    if (!(genType instanceof ParameterizedType)) {
        logger.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType");
        return Object.class;
    }

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

    if (index >= params.length || index < 0) {
        logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: "
                + params.length);
        return Object.class;
    }
    if (!(params[index] instanceof Class)) {
        logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
        return Object.class;
    }

    return (Class) params[index];
}

From source file:org.jcurl.core.ui.TaskExecutor.java

/** Find the presence of a generic type parameter. */
@SuppressWarnings("unchecked")
static Class<Executor> findMessageTypeParam(final Class<? extends Task> clz) {
    if (Object.class.equals(clz.getGenericSuperclass())) {
        final ParameterizedType pt = (ParameterizedType) clz.getGenericInterfaces()[0];
        return (Class<Executor>) pt.getActualTypeArguments()[0];
    }//from  w ww . ja v  a  2s. com
    final ParameterizedType pt = (ParameterizedType) clz.getGenericSuperclass();
    return (Class<Executor>) pt.getActualTypeArguments()[0];
}

From source file:com.thinkmore.framework.utils.ReflectionUtil.java

/**
 * ??,Class?.//from   w  w  w . j a va2 s. c  o m
 * , Object.class.
 * 
 * public UserDao extends HibernateDao<User,Long>
 *
 * @param clazz clazz The class to introspect
 * @param index the Index of the generic ddeclaration,start from 0.
 * @return the index generic declaration, or Object.class if cannot be determined
 */
@SuppressWarnings({ "rawtypes" })
public static Class getSuperClassGenricType(final Class clazz, final int index) {

    Type genType = clazz.getGenericSuperclass();

    if (!(genType instanceof ParameterizedType)) {
        logger.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType");
        return Object.class;
    }

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

    if (index >= params.length || index < 0) {
        logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: "
                + params.length);
        return Object.class;
    }
    if (!(params[index] instanceof Class)) {
        logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
        return Object.class;
    }

    return (Class) params[index];
}

From source file:com.micmiu.mvc.ferriswheel.utils.ReflectionUtils.java

/**
 * ??// ww  w  .  j  a va  2 s.  c o  m
 *
 * @param clazz
 * @param index
 * @param <T>
 * @return Class<T>
 */
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[index];
}

From source file:com.manpowergroup.cn.core.utils.ReflectionUtils.java

/**
 * ??,Class?.//  w  ww.  j a  va  2 s.co  m
 * 
 * public UserDao extends HibernateDao<User,Long>
 *
 * @param clazz clazz The class to introspect
 * @param index the Index of the generic ddeclaration,start from 0.
 * @return the index generic declaration, or Object.class if cannot be determined
 */

@SuppressWarnings({ "rawtypes" })
public static Class getSuperClassGenricType(final Class clazz, final int index) {

    Type genType = clazz.getGenericSuperclass();

    if (!(genType instanceof ParameterizedType)) {
        logger.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType");
        return Object.class;
    }

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

    if (index >= params.length || index < 0) {
        logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: "
                + params.length);
        return Object.class;
    }
    if (!(params[index] instanceof Class)) {
        logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
        return Object.class;
    }

    return (Class) params[index];
}

From source file:io.github.sparta.helpers.reflex.Reflections.java

/**
 * ??, Class?./*from w w  w.j a  v a  2s . c  o m*/
 * , Object.class.
 * <p/>
 * public UserDao extends HibernateDao<User,Long>
 *
 * @param clazz clazz The class to introspect
 * @param index the Index of the generic ddeclaration,start from 0.
 * @return the index generic declaration, or Object.class if cannot be determined
 */
@SuppressWarnings("rawtypes")
public static Class getSuperClassGenricType(final Class clazz, final int index) {

    Type genType = clazz.getGenericSuperclass();

    if (!(genType.getClass().isAssignableFrom(ParameterizedType.class))) {
        log.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType");
        return Object.class;
    }

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

    if (index >= params.length || index < 0) {
        log.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: "
                + params.length);
        return Object.class;
    }
    if (!(params[index].getClass().isAssignableFrom(Class.class))) {
        log.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
        return Object.class;
    }

    return (Class) params[index];
}

From source file:org.guess.core.utils.ReflectionUtils.java

/**
 * ??, Class?.//from  ww w.ja v a  2  s .  c o  m
 * , Object.class.
 * 
 * public UserDao extends HibernateDao<User,Long>
 *
 * @param clazz clazz The class to introspect
 * @param index the Index of the generic ddeclaration,start from 0.
 * @return the index generic declaration, or Object.class if cannot be determined
 */
public static Class getSuperClassGenricType(final Class clazz, final int index) {

    Type genType = clazz.getGenericSuperclass();

    if (!(genType instanceof ParameterizedType)) {
        logger.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType");
        return Object.class;
    }

    Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
    if (index >= params.length || index < 0) {
        logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: "
                + params.length);
        return Object.class;
    }
    if (!(params[index] instanceof Class)) {
        logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
        return Object.class;
    }

    //      for (Type type : params) {
    //         Class clz = (Class)type;
    //         System.out.println(clz.getName());
    //      }
    return (Class) params[index];
}

From source file:com.kangyonggan.cms.util.Reflections.java

/**
 * ??, Class?./*from  w ww .  j  a  va2  s. c o  m*/
 * , Object.class.
 * <p>
 * public UserDao extend HibernateDao<User,Long>
 *
 * @param clazz clazz The class to introspect
 * @param index the Index of the generic ddeclaration,start from 0.
 * @return the index generic declaration, or Object.class if cannot be determined
 */
public static Class getClassGenricType(final Class clazz, final int index) {

    Type genType = clazz.getGenericSuperclass();

    if (!(genType instanceof ParameterizedType)) {
        return Object.class;
    }

    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:com.dosport.system.utils.ReflectionUtils.java

/**
 * ??, Class?. , Object.class.//w ww.ja va 2 s  . c o  m
 * 
 * public UserDao extends HibernateDao<User,Long>
 * 
 * @param clazz
 *            clazz The class to introspect
 * @param index
 *            the Index of the generic ddeclaration,start from 0.
 * @return the index generic declaration, or Object.class if cannot be
 *         determined
 */
@SuppressWarnings("rawtypes")
public static Class getSuperClassGenricType(final Class clazz, final int index) {

    Type genType = clazz.getGenericSuperclass();

    if (!(genType instanceof ParameterizedType)) {
        logger.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType");
        return Object.class;
    }

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

    if (index >= params.length || index < 0) {
        logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: "
                + params.length);
        return Object.class;
    }
    if (!(params[index] instanceof Class)) {
        logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
        return Object.class;
    }

    return (Class) params[index];
}

From source file:com.cn.util.Reflections.java

/**
 * ??, Class?. , Object.class.//from  ww  w .j a  va2  s. c  o  m
 * 
 * public UserDao extends HibernateDao<User,Long>
 * 
 * @param clazz
 *            clazz The class to introspect
 * @param index
 *            the Index of the generic ddeclaration,start from 0.
 * @return the index generic declaration, or Object.class if cannot be
 *         determined
 */
@SuppressWarnings("rawtypes")
public static Class getClassGenricType(final Class clazz, final int index) {

    Type genType = clazz.getGenericSuperclass();

    if (!(genType instanceof ParameterizedType)) {
        return Object.class;
    }

    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];
}