Java Reflection Generic Parameter getGenericParamType1(Type parameterizedType)

Here you can find the source of getGenericParamType1(Type parameterizedType)

Description

Get the parameterized type of a generic type GenericType For method, call method.getGenericParameterTypes() or method.getGenericReturnType() for the parameterizedType.

License

Mozilla Public License

Declaration

public static Class getGenericParamType1(Type parameterizedType) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0.  If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 * //from   w ww  . ja v a 2  s .c o m
 * Software distributed under the License is distributed on an "AS IS" basis, 
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for 
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is: Jsoda
 * The Initial Developer of the Original Code is: William Wong (williamw520@gmail.com)
 * Portions created by William Wong are Copyright (C) 2012 William Wong, All Rights Reserved.
 *
 ******************************************************************************/

import java.lang.reflect.*;

public class Main {
    /** Get the parameterized type of a generic type GenericType<ParameterizedType>
     * For method, call method.getGenericParameterTypes() or method.getGenericReturnType() for the parameterizedType.
     * For field, call field.getGenericType() for the parameterizedType.
     * e.g.  List<String> field1 ==> getGenericParamType1(field11.getGenericType()) => String
     * Return null for none found.
     */
    public static Class getGenericParamType1(Type parameterizedType) {
        if (parameterizedType instanceof ParameterizedType) {
            ParameterizedType type = (ParameterizedType) parameterizedType;
            Type[] typeArguments = type.getActualTypeArguments();
            if (typeArguments.length > 0)
                return (Class) typeArguments[0];
        }
        return null;
    }
}

Related

  1. getGenericParameterTypes(final Type type)
  2. getGenericParameterTypes(Method m)
  3. getGenericParameterTypes(Type t)
  4. getGenericParamType(Type type)
  5. getGenericParamType(Type type)
  6. getGenericParamTypes(Type genericType)
  7. getGenericType(Parameter param)
  8. getGenericTypeParameter(Type genericType)
  9. getGenericTypes(ParameterizedType t)