Here you can find the source of getGenericParameterType0(Class> cls, Class> genericSuper, int paramIndex)
private static Type getGenericParameterType0(Class<?> cls, Class<?> genericSuper, int paramIndex)
//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; } }