Here you can find the source of getGenericDeclaringType(Class> baseClass, Class> declaringClass)
private static Type getGenericDeclaringType(Class<?> baseClass, Class<?> declaringClass)
//package com.java2s; /*/* w ww . j a va 2 s .c o m*/ * Copyright 2011 cruxframework.org. * * 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.Array; import java.lang.reflect.GenericArrayType; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; public class Main { private static Type getGenericDeclaringType(Class<?> baseClass, Class<?> declaringClass) { if (baseClass.equals(declaringClass)) { return baseClass; } if (declaringClass.isInterface()) { Type[] interfaces = baseClass.getGenericInterfaces(); for (Type type : interfaces) { if (type instanceof ParameterizedType) { if (((ParameterizedType) type).getRawType().equals(declaringClass)) { return type; } } } } else { Class<?> superclass = baseClass.getSuperclass(); if (superclass != null && superclass.equals(declaringClass)) { return baseClass.getGenericSuperclass(); } } return null; } public static Class<?> getRawType(Type type) { if (type instanceof Class<?>) { // type is a normal class. return (Class<?>) type; } else if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; Type rawType = parameterizedType.getRawType(); return (Class<?>) rawType; } else if (type instanceof GenericArrayType) { final GenericArrayType genericArrayType = (GenericArrayType) type; final Class<?> componentRawType = getRawType(genericArrayType.getGenericComponentType()); return Array.newInstance(componentRawType, 0).getClass(); } else if (type instanceof TypeVariable) { final TypeVariable<?> typeVar = (TypeVariable<?>) type; if (typeVar.getBounds() != null && typeVar.getBounds().length > 0) { return getRawType(typeVar.getBounds()[0]); } } throw new RuntimeException("Unable to determine base class from Type"); } }