Java Reflection Generic Parameter getGenericTypeParameter(Type genericType)

Here you can find the source of getGenericTypeParameter(Type genericType)

Description

Get the type parameter out of a generic type.

License

Apache License

Parameter

Parameter Description
genericType a parameter

Return

the type parameter of Object.class

Declaration

static Class getGenericTypeParameter(Type genericType) 

Method Source Code


//package com.java2s;
/*// w  w w.  ja  v  a2s . c  o  m
 *    Copyright 2012 Juan Alberto L?pez Cavallotti
 *
 * Licensed 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
 *
 * 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.*;

public class Main {
    /**
     * Get the type parameter out of a generic type.
     * @param genericType
     * @return the type parameter of Object.class
     */
    static Class getGenericTypeParameter(Type genericType) {
        ParameterizedType fieldParamType = null;

        if (genericType instanceof ParameterizedType) {
            fieldParamType = (ParameterizedType) genericType;
        }

        if (fieldParamType == null) {
            return Object.class;
        }

        Class typeParameter = (Class) fieldParamType.getActualTypeArguments()[0];

        return typeParameter;
    }
}

Related

  1. getGenericParamType(Type type)
  2. getGenericParamType(Type type)
  3. getGenericParamType1(Type parameterizedType)
  4. getGenericParamTypes(Type genericType)
  5. getGenericType(Parameter param)
  6. getGenericTypes(ParameterizedType t)
  7. getGenericTypes(Type[] genericParameterTypes)