Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class Main {
    /**
     * Resgata o tipo generico da classe
     * @param clazz Classe generics
     * @return Generic Type
     */
    public static Class<?> getTypeGenericInstance(Class<?> clazz) {
        ParameterizedType parameterized = (ParameterizedType) clazz.getGenericSuperclass();
        Type[] arguments = parameterized.getActualTypeArguments();
        return (Class<?>) arguments[0];
    }

    /**
     * Resgata o tipo generico da classe pelo seu index
     * @param clazz Classe generics
     * @param i index do generic.
     * @return Generic Type
     */
    public static Class<?> getTypeGenericInstance(Class<?> clazz, int i) {
        ParameterizedType parameterized = (ParameterizedType) clazz.getGenericSuperclass();
        Type[] arguments = parameterized.getActualTypeArguments();
        return (Class<?>) arguments[i];
    }
}