Java Reflection Generic Type getGenericType(Type t, int index)

Here you can find the source of getGenericType(Type t, int index)

Description

get Generic Type

License

Open Source License

Declaration

public static Class getGenericType(Type t, int index) 

Method Source Code

//package com.java2s;
/**************************************************************************************
 * Copyright (C) 2008 EsperTech, Inc. All rights reserved.                            *
 * http://esper.codehaus.org                                                          *
 * http://www.espertech.com                                                           *
 * ---------------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the GPL license       *
 * a copy of which has been included with this distribution in the license.txt file.  *
 **************************************************************************************/

import java.lang.reflect.*;

public class Main {
    public static Class getGenericType(Type t, int index) {
        if (t == null) {
            return null;
        }//from  w w w  .  j a  v a2 s .  com
        if (!(t instanceof ParameterizedType)) {
            return null;
        }
        ParameterizedType ptype = (ParameterizedType) t;
        if ((ptype.getActualTypeArguments() == null) || (ptype.getActualTypeArguments().length < (index + 1))) {
            return Object.class;
        }
        Type typeParam = ptype.getActualTypeArguments()[index];
        if (!(typeParam instanceof Class)) {
            return Object.class;
        }
        return (Class) typeParam;
    }
}

Related

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