Java Reflection Generic Type from Class getGenericInterfaceParamType(Class cls, Class rawType)

Here you can find the source of getGenericInterfaceParamType(Class cls, Class rawType)

Description

Returns the Type of parameter of the generic interface of the class.

License

Apache License

Parameter

Parameter Description
cls a parameter
rawType a parameter

Return

java.lang.reflect.Type

Declaration

public static Type getGenericInterfaceParamType(Class<?> cls, Class<?> rawType) 

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
 *  /*from   w ww. j  a  v  a2 s.c om*/
 *   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 {
    /**
     * Returns the Type of parameter of the generic interface of the class.
     * <p>
     * E.g. Let A be class that implements <tt>List&lt;String&gt;</tt>. Calling
     * <tt>getGenericInterfaceType(A.class, List.class)</tt> will return
     * <tt>String.class</tt>.
     * <p>
     * In case the interface has more than one parameter, only the type of the
     * first parameter is returned by this method.
     * 
     * @param cls
     * @param rawType
     * @return java.lang.reflect.Type
     */
    public static Type getGenericInterfaceParamType(Class<?> cls, Class<?> rawType) {
        while (cls != null) {
            Type[] interfaces = cls.getGenericInterfaces();
            for (Type type : interfaces) {
                if (type instanceof ParameterizedType) {
                    ParameterizedType pType = (ParameterizedType) type;
                    if (pType.getRawType() == rawType) {
                        return pType.getActualTypeArguments()[0];
                    } else {
                        continue;
                    }
                }
                // look through the base interfaces of the current interface
                Type interfaceType = getGenericInterfaceParamType((Class<?>) type, rawType);
                if (interfaceType != null) {
                    return interfaceType;
                }
            }
            cls = cls.getSuperclass();
        }
        // if we're done with the recursive calls, perhaps developer
        // did not parameterize their interface
        return null;
    }
}

Related

  1. getGenericClassByIndex(Type genericType, int index)
  2. getGenericClassType(Class clazz, Class filterClass)
  3. getGenericDeclaringType(Class baseClass, Class declaringClass)
  4. getGenericFirstClass(Type type)
  5. getGenericInterface(final Class sourceClass, final Class genericInterface)
  6. getGenericInterfacesActualTypes(Collection types, Class aClass)
  7. getGenericParameter(Class clazz, int index)
  8. getGenericParameter(Type class1, int number)
  9. getGenericParameterClass(Class actualClass, int parameterIndex)