Java Reflection Generic Type from Class getGenericClass(Class clazz, int index)

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

Description

get Generic Class

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
public static <T> Class<T> getGenericClass(Class<?> clazz, int index) 

Method Source Code

//package com.java2s;
/*//from w  ww.j  av  a 2  s  . c  om
 * ReflectionUtils.java
 *
 * Copyright (c) 1998 - 2005 BusinessTechnology, Ltd.
 * All rights reserved
 *
 * This program is the proprietary and confidential information
 * of BusinessTechnology, Ltd. and may be used and disclosed only
 * as authorized in a license agreement authorizing and
 * controlling such use and disclosure
 *
 * Millennium ERP system.
 *
 */

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

public class Main {

    @SuppressWarnings("unchecked")
    public static <T> Class<T> getGenericClass(Class<?> clazz, int index) {
        Type[] actualTypeArguments = ((ParameterizedType) clazz.getGenericSuperclass()).getActualTypeArguments();
        if (actualTypeArguments.length < index)
            throw new IndexOutOfBoundsException();
        return (Class<T>) actualTypeArguments[index];
    }
}

Related

  1. getGenericClass(Class clazz)
  2. getGenericClass(Class clazz)
  3. getGenericClass(Class clazz, int index)
  4. getGenericClass(Class clazz, int index)
  5. getGenericClass(Class clazz)
  6. getGenericClass(Class cls)
  7. getGenericClass(final Class class1)
  8. getGenericClass(final Class parametrizedClass, int pos)
  9. getGenericClass(final Class parametrizedClass, int pos)