Java Reflection Generic Type getGenericType(Member member, int index)

Here you can find the source of getGenericType(Member member, int index)

Description

Returns the actual generic type of a class's type parameter of the member.

License

Open Source License

Parameter

Parameter Description
member the member
index the index number of the generic parameter
X the type of the class

Return

the class of generic type

Declaration

@SuppressWarnings("unchecked")
public static <X> Class<X> getGenericType(Member member, int index) 

Method Source Code

//package com.java2s;
/*/* w w  w  .  j  a  v a 2  s. c  om*/
 * Copyright (c) 2012-2013, Batu Alp Ceylan
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */

import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;

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

public class Main {
    /**
     * Returns the actual generic type of a class's type parameter of the <code>member</code>.
     * <p>
     * if the <code>member</code> is a field then field's generic types are checked. Otherwise the <code>member</code> is treated a a method
     * and its return type's is checked.
     * <p>
     * 
     * @param member
     *            the member
     * @param index
     *            the index number of the generic parameter
     * @return the class of generic type
     * 
     * @param <X>
     *            the type of the class
     * 
     * @since 2.0.1
     */
    @SuppressWarnings("unchecked")
    public static <X> Class<X> getGenericType(Member member, int index) {
        Type type;

        if (member instanceof Field) {
            final Field field = (Field) member;
            type = field.getGenericType();

        } else {
            final Method method = (Method) member;
            type = method.getGenericReturnType();
        }

        // if not a parameterized type return null
        if (!(type instanceof ParameterizedType)) {
            return null;
        }

        final ParameterizedType parameterizedType = (ParameterizedType) type;

        final Type[] types = parameterizedType.getActualTypeArguments();

        return (Class<X>) ((types != null) && (index < types.length) ? types[index] : null);
    }
}

Related

  1. getGenerics(Type genericType)
  2. getGenerics(Type t)
  3. getGenericString(AccessibleObject ao)
  4. getGenericSuperType(Type t)
  5. getGenericType(@Nullable Type genericType)
  6. getGenericType(Method setter)
  7. getGenericType(Object target)
  8. getGenericType(Type genType, int index)
  9. getGenericType(Type t, int index)