Java Reflection Generic Type from Class getGenericTypes(final Class iClass)

Here you can find the source of getGenericTypes(final Class iClass)

Description

Returns the declared generic types of a class.

License

Apache License

Parameter

Parameter Description
iClass Class to examine

Return

The array of Type if any, otherwise null

Declaration

public static Type[] getGenericTypes(final Class<?> iClass) 

Method Source Code

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

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

public class Main {
    /**//from w ww  .  j a  va2s.  co  m
     * Returns the declared generic types of a class.
     * 
     * @param iClass
     *          Class to examine
     * @return The array of Type if any, otherwise null
     */
    public static Type[] getGenericTypes(final Class<?> iClass) {
        final Type genericType = iClass.getGenericInterfaces()[0];
        if (genericType != null && genericType instanceof ParameterizedType) {
            final ParameterizedType pt = (ParameterizedType) genericType;
            if (pt.getActualTypeArguments() != null
                    && pt.getActualTypeArguments().length > 1)
                return pt.getActualTypeArguments();
        }
        return null;
    }
}

Related

  1. getGenericTypeOfInterface(Class clazz, Class specificInterface)
  2. getGenericTypeOfParameter(Class clazz, String method, int parameterIndex)
  3. getGenericTypeParameter(Class aClass, Class genericClass, int index)
  4. getGenericTypes(Class c)
  5. getGenericTypes(Class clazz)
  6. getGenericTypesImpl(ParameterizedType ptype, List> classes)