Java Reflection Generic Type getGenericTypeArguments(Method m, int i)

Here you can find the source of getGenericTypeArguments(Method m, int i)

Description

get Generic Type Arguments

License

Open Source License

Parameter

Parameter Description
m Method we want to probe generic type arguments.
i the i'th parameter of the method.

Return

an array of parameterized Types for the i'th argument or an empty array.

Declaration

public static Type[] getGenericTypeArguments(Method m, int i) 

Method Source Code

//package com.java2s;
/**/*  w  w  w  . j  a  v a 2  s .c o  m*/
 * Copyright (c) 2014-2015 by Wen Yu.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Any modifications to this file must keep this entire header intact.
 */

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

public class Main {
    /**
     * @param m Method we want to probe generic type arguments.
     * @param i the i'th parameter of the method.
     * @return an array of parameterized Types for the i'th argument or an empty array. 
     */
    public static Type[] getGenericTypeArguments(Method m, int i) {
        try {
            Type t = m.getGenericParameterTypes()[i];

            if (t instanceof ParameterizedType) {
                ParameterizedType pt = (ParameterizedType) t;
                return pt.getActualTypeArguments();
            }
        } catch (Exception e) {
            System.out.println("Error probing generic type arguments!");
        }

        return new Type[] {};
    }
}

Related

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