get Class Generic Type - Android java.lang.reflect

Android examples for java.lang.reflect:ParameterizedType

Description

get Class Generic Type

Demo Code


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

public class Main {
    private static Type getGenericType(int index, Class<?> subclass) {
        Type superclass = subclass.getGenericSuperclass();
        if (!(superclass instanceof ParameterizedType)) {
            return Object.class;
        }//from   www.  ja  v  a2 s .  c  om
        Type[] params = ((ParameterizedType) superclass)
                .getActualTypeArguments();
        if (index >= params.length || index < 0) {
            throw new RuntimeException("Index outof bounds");
        }

        if (!(params[index] instanceof Class)) {
            return Object.class;
        }
        return params[index];
    }
}

Related Tutorials