Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

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

public class Main {
    public static <T> Class<T> getTypeByIndex(Class<T> clz, int index) {
        if (clz == null) {
            return null;
        }
        Type type = clz.getGenericSuperclass();
        if (type == null) {
            return null;
        }
        if (type instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) type;
            Type[] types = parameterizedType.getActualTypeArguments();
            if (types != null && types.length > 0) {
                if (index >= 0 && index < types.length) {
                    return (Class<T>) types[index];
                }
            }
        }
        return null;
    }
}