Java Reflection Generic Type from Class getGenericSuperType(Class subclass, Class classWithParameter)

Here you can find the source of getGenericSuperType(Class subclass, Class classWithParameter)

Description

returns either the generic superclass or a generic interface, but only when it is assignable from the typed classWithParameter argument

License

Apache License

Parameter

Parameter Description
T a parameter
subclass a parameter
classWithParameter a parameter

Declaration

public static <T> Type getGenericSuperType(Class<? extends T> subclass, Class<? extends T> classWithParameter) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Type;

public class Main {
    /**/*from  ww w.  ja  v a  2s  .  co  m*/
     * returns either the generic superclass or a generic interface, but only when it is assignable from the typed
     * classWithParameter argument
     *
     * @param <T>
     * @param subclass
     * @param classWithParameter
     * @return
     */
    public static <T> Type getGenericSuperType(Class<? extends T> subclass, Class<? extends T> classWithParameter) {
        if (subclass.getSuperclass() != null && classWithParameter.isAssignableFrom(subclass.getSuperclass())) {
            return subclass.getGenericSuperclass();
        } else {
            int i = 0;
            for (Class in : subclass.getInterfaces()) {
                if (classWithParameter.isAssignableFrom(in)) {
                    return subclass.getGenericInterfaces()[i];
                }
                i++;
            }
            return null;
        }
    }
}

Related

  1. getGenericsClass(Class cls)
  2. getGenericSuperclass(Class clazz)
  3. getGenericSuperclass(Class cls)
  4. getGenericSuperclassActualTypes(Collection types, Class aClass)
  5. getGenericSuperType(Class clz, int index)
  6. getGenericSuperType(Class class1, Class class2)
  7. getGenericType(Class clazz, int index)
  8. getGenericType(Class propertyType)
  9. getGenericType(Class clazz)