Java Reflection Generic Type from Class getGenericClass(Class clazz)

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

Description

get the first generic declaration on a class.

License

Apache License

Parameter

Parameter Description
clazz The class to introspect

Return

the first generic declaration, or Object.class if cannot be determined

Declaration

public static Class getGenericClass(Class clazz) 

Method Source Code


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

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

public class Main {
    /**/*w w w  .j  a v  a 2 s  .  c om*/
     * get the first generic declaration on a class.
     * 
     * @param clazz
     *            The class to introspect
     * @return the first generic declaration, or <code>Object.class</code> if
     *         cannot be determined
     */
    public static Class getGenericClass(Class clazz) {
        Type genType = clazz.getGenericSuperclass();

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

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

        return (Class) params[0];
    }
}

Related

  1. getGenericArgumentType(Class clazz)
  2. getGenericArgumentType(Class currentClass, Class genericSuperClass, int argumentIndex)
  3. getGenericClass(Class clazz)
  4. getGenericClass(Class clazz)
  5. getGenericClass(Class clazz, int index)
  6. getGenericClass(Class clazz, int index)
  7. getGenericClass(Class clazz)