Java Reflection Generic Type getGenericTypes(Constructor targetConstructor, Integer constructorArgOrderingNumber)

Here you can find the source of getGenericTypes(Constructor targetConstructor, Integer constructorArgOrderingNumber)

Description

Gets the generic types for a given constructor argument (specified by its ordering number).

License

Open Source License

Parameter

Parameter Description
targetConstructor the target constructor
constructorArgOrderingNumber the constructor arg ordering number

Return

the generic types

Declaration

public static Type[] getGenericTypes(Constructor<?> targetConstructor, Integer constructorArgOrderingNumber) 

Method Source Code

//package com.java2s;
/*/*from  ww w . j a  va2s  .com*/
 ORG Usurper is a random value object generator library 
 Copyright (C) 2007  Pierre-Antoine Gr?goire
     
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.
     
 This library 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 library; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

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

public class Main {
    /**
     * Gets the generic types for a given constructor argument (specified by its ordering number).
     * 
     * @param targetConstructor the target constructor
     * @param constructorArgOrderingNumber the constructor arg ordering number
     * 
     * @return the generic types
     */
    public static Type[] getGenericTypes(Constructor<?> targetConstructor, Integer constructorArgOrderingNumber) {
        Type[] genericParameterTypes = targetConstructor.getGenericParameterTypes();
        ParameterizedType type = (ParameterizedType) genericParameterTypes[constructorArgOrderingNumber - 1];
        return type.getActualTypeArguments();
    }

    /**
     * Gets the generic types for a given field.
     * 
     * @param attribute the attribute
     * 
     * @return the generic types
     */
    public static Type[] getGenericTypes(Field attribute) {
        ParameterizedType type = (ParameterizedType) attribute.getGenericType();
        return type.getActualTypeArguments();
    }
}

Related

  1. getGenericType(Type genType, int index)
  2. getGenericType(Type t, int index)
  3. getGenericType(Type type)
  4. getGenericTypeArguments(Method m, int i)
  5. getGenericTypeName(Method method)
  6. getGenericTypes(final Type type)