Java Reflection Generic Type getGenericType(Type type)

Here you can find the source of getGenericType(Type type)

Description

get Generic Type

License

Open Source License

Declaration

public static Class<?> getGenericType(Type type) 

Method Source Code


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

public class Main {
    public static Class<?> getGenericType(Type type) {
        return getGenericType(type, 0);
    }//from w ww.  j a v a2  s  .  com

    public static Class<?> getGenericType(Type type, int index) {
        if (index < 0) {
            throw new ArrayIndexOutOfBoundsException(index);
        }
        if (type instanceof ParameterizedType) {
            ParameterizedType pgrt = (ParameterizedType) type;
            return (Class<?>) pgrt.getActualTypeArguments()[index];
        } else {
            throw new ClassCastException(String.format("the genericReturnType[%d]: %s is not a ParameterizedType!",
                    index, type == null ? "null" : type.toString()));
        }
    }
}

Related

  1. getGenericType(Member member, int index)
  2. getGenericType(Method setter)
  3. getGenericType(Object target)
  4. getGenericType(Type genType, int index)
  5. getGenericType(Type t, int index)
  6. getGenericTypeArguments(Method m, int i)
  7. getGenericTypeName(Method method)
  8. getGenericTypes(Constructor targetConstructor, Integer constructorArgOrderingNumber)
  9. getGenericTypes(final Type type)