Java Reflection Generic Type from Class getGenericParameterType0(Class cls, Class genericSuper, int paramIndex)

Here you can find the source of getGenericParameterType0(Class cls, Class genericSuper, int paramIndex)

Description

get Generic Parameter Type

License

Apache License

Declaration

private static Type getGenericParameterType0(Class<?> cls,
            Class<?> genericSuper, int paramIndex) 

Method Source Code

//package com.java2s;
/*/*from  www  .  j ava2 s. co m*/
 * 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
 *
 *      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.Array;

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

public class Main {
    private static Type getGenericParameterType0(Class<?> cls,
            Class<?> genericSuper, int paramIndex) {
        if (!genericSuper.isAssignableFrom(cls))
            return null;
        if (genericSuper.isInterface()) {
            for (Type type : cls.getGenericInterfaces()) {
                final Class<?> clazz = getClass(type);
                if (genericSuper.isAssignableFrom(clazz)) {
                    if (genericSuper.equals(clazz))
                        return type instanceof ParameterizedType ? ((ParameterizedType) type)
                                .getActualTypeArguments()[paramIndex]
                                : null;
                    else {
                        for (Class<?> iface : cls.getInterfaces()) {
                            final Type res = getGenericParameterType0(
                                    iface, genericSuper, paramIndex);
                            if (res != null)
                                return res;
                        }
                    }
                }
            }
            return null;
        } else {
            Type type = cls.getGenericSuperclass();
            assert genericSuper.isAssignableFrom(getClass(type));
            if (genericSuper.equals(getClass(type)))
                return type instanceof ParameterizedType ? ((ParameterizedType) type)
                        .getActualTypeArguments()[paramIndex] : null;
            else
                return getGenericParameterType0(cls.getSuperclass(),
                        genericSuper, paramIndex);
        }
    }

    public static Class<?> getClass(Type type) {
        if (type instanceof Class)
            return (Class<?>) type;
        if (type instanceof ParameterizedType)
            return (Class<?>) ((ParameterizedType) type).getRawType();
        if (type instanceof GenericArrayType)
            return Array
                    .newInstance(
                            (Class<?>) ((GenericArrayType) type)
                                    .getGenericComponentType(),
                            0).getClass();
        return null;
    }
}

Related

  1. getGenericParameter(Type class1, int number)
  2. getGenericParameterClass(Class actualClass, int parameterIndex)
  3. getGenericParameterClass(Class clazz)
  4. getGenericParameters(Class clazz)
  5. getGenericParameterType(Class clazz, Integer parameterIndex)
  6. getGenerics(Class classType)
  7. getGenericsClass(Class cls)
  8. getGenericSuperclass(Class clazz)
  9. getGenericSuperclass(Class cls)