Java Reflection Generic Type from Class getGenericInterface(final Class sourceClass, final Class genericInterface)

Here you can find the source of getGenericInterface(final Class sourceClass, final Class genericInterface)

Description

get Generic Interface

License

Open Source License

Declaration

public static Type getGenericInterface(final Class sourceClass, final Class genericInterface) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static Type getGenericInterface(final Class sourceClass, final Class genericInterface) {
        Type[] types = sourceClass.getGenericInterfaces();
        for (Type type : types) {
            if (type instanceof Class) {
                if (genericInterface.isAssignableFrom((Class) type)) {
                    return type;
                }/*  w  w w. j  a v  a  2s  . c  o  m*/
            } else if (type instanceof ParameterizedType) {
                if (genericInterface.isAssignableFrom((Class) ((ParameterizedType) type).getRawType())) {
                    return type;
                }
            }
        }
        return null;
    }
}

Related

  1. getGenericClass(Type type)
  2. getGenericClassByIndex(Type genericType, int index)
  3. getGenericClassType(Class clazz, Class filterClass)
  4. getGenericDeclaringType(Class baseClass, Class declaringClass)
  5. getGenericFirstClass(Type type)
  6. getGenericInterfaceParamType(Class cls, Class rawType)
  7. getGenericInterfacesActualTypes(Collection types, Class aClass)
  8. getGenericParameter(Class clazz, int index)
  9. getGenericParameter(Type class1, int number)