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:org.cube.core.common.utils.Reflections.java

/**
 * ??, Class?. , Object.class./*from w w  w  .  ja  v a  2  s.  c om*/
 * 
 * @param clazz
 * @param index
 * @return
 */
@SuppressWarnings("unchecked")
public static Class getSuperClassGenricType(Class clazz, int index) {

    Type genType = clazz.getGenericSuperclass();

    if (!(genType instanceof ParameterizedType)) {
        logger.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType");
        return Object.class;
    } else {
        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:net.cloudkit.enterprises.infrastructure.utilities.ReflectionHelper.java

/**
 * ??, Class?.//from   w w w .  ja  v a 2s .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<?> getClassGenricType(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.kangyonggan.api.common.util.Reflections.java

/**
 * ??, Class?.//from ww w  . j a  va  2 s.  c om
 * , 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)) {
        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] instanceof Class)) {
        log.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
        return Object.class;
    }

    return (Class) params[index];
}

From source file:com.ghy.common.util.reflection.ReflectionUtils.java

/**
 * ??, Class?./*from w  ww .  j  a v a2 s  .com*/
 * , 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:Main.java

public static ParameterizedType resolveParameterizedType(Type t, Class<?> baseClass) {
    Class<?> raw = getRawType(t);

    if (t instanceof ParameterizedType && baseClass.isAssignableFrom(raw)) {
        return (ParameterizedType) t;
    }/*from  ww w . j  a v a2s  .  c  o m*/

    ParameterizedType pt = null;
    if (raw.getSuperclass() != null && raw.getSuperclass() != Object.class) {
        pt = resolveParameterizedType(raw.getGenericSuperclass(), baseClass);
        if (pt != null)
            return pt;
    }
    if (!raw.isInterface()) {
        for (Type ifs : raw.getGenericInterfaces()) {
            pt = resolveParameterizedType(ifs, baseClass);
            if (pt != null)
                return pt;
        }
    }
    return null;
}

From source file:com.expressui.core.util.ReflectionUtil.java

/**
 * Gets a generic argument of the given class, based on arg index.
 *
 * @param clazz    class from which to extract generic argument
 * @param argIndex index of the declared argument type
 * @return type of generic argument/*from w  ww. j a  v  a  2  s  .c  o m*/
 */
public static Class getGenericArgumentType(Class clazz, int argIndex) {
    Type type = clazz.getGenericSuperclass();

    if (type != null && type instanceof ParameterizedType) {
        if (((ParameterizedType) type).getActualTypeArguments()[argIndex] instanceof Class) {
            return (Class) ((ParameterizedType) type).getActualTypeArguments()[argIndex];
        } else {
            return null;
        }
    } else {
        if (!(type instanceof Class) || type.equals(Object.class)) {
            return null;
        } else {
            return getGenericArgumentType((Class) type, argIndex);
        }
    }
}

From source file:GenericsUtil.java

private static void gatherTypeVariables(final Class<?> clazz, final Type type,
        final Map<TypeVariable<?>, Type> map) {
    if (clazz == null) {
        return;//from   w  w  w .j  a va2 s  . c o m
    }
    gatherTypeVariables(type, map);

    final Class<?> superClass = clazz.getSuperclass();
    final Type superClassType = clazz.getGenericSuperclass();
    if (superClass != null) {
        gatherTypeVariables(superClass, superClassType, map);
    }
    final Class<?>[] interfaces = clazz.getInterfaces();
    final Type[] interfaceTypes = clazz.getGenericInterfaces();
    for (int i = 0; i < interfaces.length; ++i) {
        gatherTypeVariables(interfaces[i], interfaceTypes[i], map);
    }
}

From source file:GenericsUtil.java

private static Map<TypeVariable<?>, Type> getTypeVariableMap(final Class<?> clazz) {
    if (clazz == null) {
        return Collections.emptyMap();
    }/* w w  w.ja  v  a 2  s  .  c om*/
    final Map<TypeVariable<?>, Type> map = new LinkedHashMap<TypeVariable<?>, Type>();
    final Class<?> superClass = clazz.getSuperclass();
    final Type superClassType = clazz.getGenericSuperclass();
    if (superClass != null) {
        gatherTypeVariables(superClass, superClassType, map);
    }
    final Class<?>[] interfaces = clazz.getInterfaces();
    final Type[] interfaceTypes = clazz.getGenericInterfaces();
    for (int i = 0; i < interfaces.length; ++i) {
        gatherTypeVariables(interfaces[i], interfaceTypes[i], map);
    }
    return map;
}

From source file:com.rapid.develop.core.utils.ReflectionUtils.java

/**
 * ??,Class?.//w  w  w  .  j  a  v a  2 s  .  co m
 * public BookManager extends GenricManager&lt;Book&gt;
 *
 * @param clazz clazz The class to introspect
 * @param index the Index of the generic ddeclaration,start from 0.
 */
public static Class getSuperClassGenricType(Class clazz, int index) throws IndexOutOfBoundsException {
    return getGenericType(clazz.getGenericSuperclass(), index);
}

From source file:com.feilong.commons.core.lang.reflect.TypeUtil.java

/**
 *  superclass parameterized type./*from  w  ww  . j a  v a2s.c  om*/
 *
 * @param klass
 *            the klass
 * @return the superclass parameterized type
 * @throws NullPointerException
 *             the null pointer exception
 * @see java.lang.Class#getGenericSuperclass()
 */
private static ParameterizedType getGenericSuperclassParameterizedType(Class<?> klass)
        throws NullPointerException {

    if (Validator.isNullOrEmpty(klass)) {
        throw new NullPointerException("klass can't be null/empty!");
    }

    Type type = klass.getGenericSuperclass(); //com.feilong.commons.core.lang.reflect.res.BaseSolrRepositoryImpl<com.feilong.commons.core.lang.reflect.res.SkuItem, java.lang.Long>

    while (!(type instanceof ParameterizedType) && Object.class != klass) {
        klass = klass.getSuperclass();
        type = klass.getGenericSuperclass();
    }

    return (ParameterizedType) type;
}