Java Reflection Generic Type from Class getGenericDeclaringType(Class baseClass, Class declaringClass)

Here you can find the source of getGenericDeclaringType(Class baseClass, Class declaringClass)

Description

get Generic Declaring Type

License

Apache License

Declaration

private static Type getGenericDeclaringType(Class<?> baseClass, Class<?> declaringClass) 

Method Source Code

//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");
    }
}

Related

  1. getGenericClass(Object object, int index)
  2. getGenericClass(Object source, ParameterizedType type)
  3. getGenericClass(Type type)
  4. getGenericClassByIndex(Type genericType, int index)
  5. getGenericClassType(Class clazz, Class filterClass)
  6. getGenericFirstClass(Type type)
  7. getGenericInterface(final Class sourceClass, final Class genericInterface)
  8. getGenericInterfaceParamType(Class cls, Class rawType)
  9. getGenericInterfacesActualTypes(Collection types, Class aClass)