Java Reflection Generic Type from Class getGenericParameter(Type class1, int number)

Here you can find the source of getGenericParameter(Type class1, int number)

Description

Extract generic parameter from super class

License

Apache License

Parameter

Parameter Description
class1 a parameter
number a parameter

Declaration

public static Class getGenericParameter(Type class1, int number) 

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  av  a 2s  . co  m
     * Extract generic parameter from super class
     * @param class1
     * @param number
     * @return
     */
    public static Class getGenericParameter(Type class1, int number) {
        //      assertArg("Not ParameterizedType", class1 instanceof ParameterizedType);
        ParameterizedType type = (ParameterizedType) class1;
        Type arg = type.getActualTypeArguments()[number];
        if (arg instanceof Class) {
            return (Class) arg;
        } else if (arg instanceof ParameterizedType) {
            return (Class) ((ParameterizedType) arg).getRawType();
        } else {
            throw new UnsupportedOperationException();
        }

    }
}

Related

  1. getGenericFirstClass(Type type)
  2. getGenericInterface(final Class sourceClass, final Class genericInterface)
  3. getGenericInterfaceParamType(Class cls, Class rawType)
  4. getGenericInterfacesActualTypes(Collection types, Class aClass)
  5. getGenericParameter(Class clazz, int index)
  6. getGenericParameterClass(Class actualClass, int parameterIndex)
  7. getGenericParameterClass(Class clazz)
  8. getGenericParameters(Class clazz)
  9. getGenericParameterType(Class clazz, Integer parameterIndex)