Java Reflection Generic Parameter getGenericParamType(Type type)

Here you can find the source of getGenericParamType(Type type)

Description

Get the class of the parameter of the provided parameterized type.

License

Apache License

Parameter

Parameter Description
type the type to return the class of the parameter for

Return

the class of the generic parameter of type

Declaration

public static Class<?> getGenericParamType(Type type) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *  //  w  ww.  ja  va  2 s  . c o  m
 *   http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *  
 *******************************************************************************/

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

public class Main {
    /**
     * Get the class of the parameter of the provided parameterized type. If the
     * type is a Class, then <code>null</code> is returned. If the type is
     * ParameterizedType, then the actual type argument is returned.
     * <p>
     * E.g. if type is <code>String.class</code>, then <code>null</code> is
     * returned. If type is <code>List&lt;String&gt;</code>, then
     * <code>String.class</code> is returned.
     * <p>
     * In case the type has more than one parameter, only the type of the first
     * parameter is returned by this method.
     * 
     * @param type the type to return the class of the parameter for
     * @return the class of the generic parameter of type
     */
    public static Class<?> getGenericParamType(Type type) {
        Class<?> generic = null;
        if (type instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) type;
            Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
            generic = (Class<?>) actualTypeArguments[0];
        }
        return generic;
    }
}

Related

  1. getGenericParameterType(Type type, int index)
  2. getGenericParameterTypes(final Type type)
  3. getGenericParameterTypes(Method m)
  4. getGenericParameterTypes(Type t)
  5. getGenericParamType(Type type)
  6. getGenericParamType1(Type parameterizedType)
  7. getGenericParamTypes(Type genericType)
  8. getGenericType(Parameter param)
  9. getGenericTypeParameter(Type genericType)