Example usage for java.lang Class getGenericInterfaces

List of usage examples for java.lang Class getGenericInterfaces

Introduction

In this page you can find the example usage for java.lang Class getGenericInterfaces.

Prototype

public Type[] getGenericInterfaces() 

Source Link

Document

Returns the Type s representing the interfaces directly implemented by the class or interface represented by this object.

Usage

From source file:ReflectUtil.java

/**
 * Returns an array of Type objects representing the actual type arguments
 * to targetType used by clazz.//from   w w  w . j a v a2 s . c  o  m
 * 
 * @param clazz the implementing class (or subclass)
 * @param targetType the implemented generic class or interface
 * @return an array of Type objects or null
 */
public static Type[] getActualTypeArguments(Class<?> clazz, Class<?> targetType) {
    Set<Class<?>> classes = new HashSet<Class<?>>();
    classes.add(clazz);

    if (targetType.isInterface())
        classes.addAll(getImplementedInterfaces(clazz));

    Class<?> superClass = clazz.getSuperclass();
    while (superClass != null) {
        classes.add(superClass);
        superClass = superClass.getSuperclass();
    }

    for (Class<?> search : classes) {
        for (Type type : (targetType.isInterface() ? search.getGenericInterfaces()
                : new Type[] { search.getGenericSuperclass() })) {
            if (type instanceof ParameterizedType) {
                ParameterizedType parameterizedType = (ParameterizedType) type;
                if (targetType.equals(parameterizedType.getRawType()))
                    return parameterizedType.getActualTypeArguments();
            }
        }
    }

    return null;
}

From source file:Main.java

public static ParameterizedType resolveParameterizedType(Type t, Class<?> baseClass) {
    Class<?> raw = getRawType(t);

    if (t instanceof ParameterizedType && baseClass.isAssignableFrom(raw)) {
        return (ParameterizedType) t;
    }/*from w  w w  .  j  a  v  a  2s  .c o m*/

    ParameterizedType pt = null;
    if (raw.getSuperclass() != null && raw.getSuperclass() != Object.class) {
        pt = resolveParameterizedType(raw.getGenericSuperclass(), baseClass);
        if (pt != null)
            return pt;
    }
    if (!raw.isInterface()) {
        for (Type ifs : raw.getGenericInterfaces()) {
            pt = resolveParameterizedType(ifs, baseClass);
            if (pt != null)
                return pt;
        }
    }
    return null;
}

From source file:jp.terasoluna.fw.util.GenericsUtil.java

/**
 * ?????? <code>ParameterizedType</code>???
 * @param <T> ???/*w  w  w.  jav  a 2s . c om*/
 * @param genericClass ??
 * @param ancestorTypeList <code>ParameterizedType</code> ?
 * @param clazz ?
 * @return ?????????<code>true</code> ????????<code>false</code>
 */
protected static <T> boolean checkInterfaceAncestors(Class<T> genericClass,
        List<ParameterizedType> ancestorTypeList, Class<?> clazz) {
    boolean genericTypeFound = false;
    Type[] interfaceTypes = clazz.getGenericInterfaces();
    for (Type interfaceType : interfaceTypes) {
        genericTypeFound = checkParameterizedType(interfaceType, genericClass, ancestorTypeList);
        if (genericTypeFound) {
            return true;
        }
        @SuppressWarnings("rawtypes")
        Class[] interfaces = clazz.getInterfaces();
        for (Class<?> interfaceClass : interfaces) {
            if (checkInterfaceAncestors(genericClass, ancestorTypeList, interfaceClass)) {
                return true;
            }
        }
    }
    return false;
}

From source file:Main.java

/**
 * Searches for ofClass in the inherited classes and interfaces of inClass. If ofClass has been found in the super
 * classes/interfaces of inClass, then the generic type corresponding to inClass and its TypeVariables is returned,
 * otherwise null. For example ://from w w w.  j a  va2s  .  com
 * 
 * <pre>
 * abstract class MyClass implements Serializer&lt;Number&gt; {
 * 
 * }
 * 
 * // type value will be the parameterized type Serializer&lt;Number&gt;
 * Type type = lookupGenericType(Serializer.class, MyClass.class);
 * </pre>
 */
public final static Type lookupGenericType(Class<?> ofClass, Class<?> inClass) {
    if (ofClass == null || inClass == null || !ofClass.isAssignableFrom(inClass))
        return null;
    if (ofClass.equals(inClass))
        return inClass;

    if (ofClass.isInterface()) {
        // lets look if the interface is directly implemented by fromClass
        Class<?>[] interfaces = inClass.getInterfaces();

        for (int i = 0; i < interfaces.length; i++) {
            // do they match?
            if (ofClass.equals(interfaces[i])) {
                return inClass.getGenericInterfaces()[i];
            } else {
                Type superType = lookupGenericType(ofClass, interfaces[i]);
                if (superType != null)
                    return superType;
            }
        }
    }

    // ok it's not one of the directly implemented interfaces, lets try extended class
    Class<?> superClass = inClass.getSuperclass();
    if (ofClass.equals(superClass))
        return inClass.getGenericSuperclass();
    return lookupGenericType(ofClass, inClass.getSuperclass());
}

From source file:org.modelmapper.internal.util.TypeResolver.java

private static Map<TypeVariable<?>, Type> getTypeVariableMap(final Class<?> targetType) {
    Reference<Map<TypeVariable<?>, Type>> ref = typeVariableCache.get(targetType);
    Map<TypeVariable<?>, Type> map = ref != null ? ref.get() : null;

    if (map == null) {
        map = new HashMap<TypeVariable<?>, Type>();

        // Populate interfaces
        buildTypeVariableMap(targetType.getGenericInterfaces(), map);

        // Populate super classes and interfaces
        Type genericType = targetType.getGenericSuperclass();
        Class<?> type = targetType.getSuperclass();
        while (type != null && !Object.class.equals(type)) {
            if (genericType instanceof ParameterizedType)
                buildTypeVariableMap((ParameterizedType) genericType, map);
            buildTypeVariableMap(type.getGenericInterfaces(), map);

            genericType = type.getGenericSuperclass();
            type = type.getSuperclass();
        }/*  w  ww.j  a v  a  2s  . com*/

        // Populate enclosing classes
        type = targetType;
        while (type.isMemberClass()) {
            genericType = type.getGenericSuperclass();
            if (genericType instanceof ParameterizedType)
                buildTypeVariableMap((ParameterizedType) genericType, map);

            type = type.getEnclosingClass();
        }

        typeVariableCache.put(targetType, new WeakReference<Map<TypeVariable<?>, Type>>(map));
    }

    return map;
}

From source file:GenericsUtil.java

private static void gatherTypeVariables(final Class<?> clazz, final Type type,
        final Map<TypeVariable<?>, Type> map) {
    if (clazz == null) {
        return;/*from  w ww.  ja  v a  2  s.c  o m*/
    }
    gatherTypeVariables(type, map);

    final Class<?> superClass = clazz.getSuperclass();
    final Type superClassType = clazz.getGenericSuperclass();
    if (superClass != null) {
        gatherTypeVariables(superClass, superClassType, map);
    }
    final Class<?>[] interfaces = clazz.getInterfaces();
    final Type[] interfaceTypes = clazz.getGenericInterfaces();
    for (int i = 0; i < interfaces.length; ++i) {
        gatherTypeVariables(interfaces[i], interfaceTypes[i], map);
    }
}

From source file:GenericsUtil.java

private static Map<TypeVariable<?>, Type> getTypeVariableMap(final Class<?> clazz) {
    if (clazz == null) {
        return Collections.emptyMap();
    }/*from  www . j ava  2 s. c  o m*/
    final Map<TypeVariable<?>, Type> map = new LinkedHashMap<TypeVariable<?>, Type>();
    final Class<?> superClass = clazz.getSuperclass();
    final Type superClassType = clazz.getGenericSuperclass();
    if (superClass != null) {
        gatherTypeVariables(superClass, superClassType, map);
    }
    final Class<?>[] interfaces = clazz.getInterfaces();
    final Type[] interfaceTypes = clazz.getGenericInterfaces();
    for (int i = 0; i < interfaces.length; ++i) {
        gatherTypeVariables(interfaces[i], interfaceTypes[i], map);
    }
    return map;
}

From source file:antre.TypeResolver.java

private static Map<TypeVariable<?>, Type> getTypeVariableMap(final Class<?> targetType) {
    Reference<Map<TypeVariable<?>, Type>> ref = typeVariableCache.get(targetType);
    Map<TypeVariable<?>, Type> map = ref != null ? ref.get() : null;

    if (map == null) {
        map = new HashMap<TypeVariable<?>, Type>();

        // Populate interfaces
        buildTypeVariableMap(targetType.getGenericInterfaces(), map);

        // Populate super classes and interfaces
        Type genericType = targetType.getGenericSuperclass();
        Class<?> type = targetType.getSuperclass();
        while (type != null && !Object.class.equals(type)) {
            if (genericType instanceof ParameterizedType)
                buildTypeVariableMap((ParameterizedType) genericType, map);
            buildTypeVariableMap(type.getGenericInterfaces(), map);

            genericType = type.getGenericSuperclass();
            type = type.getSuperclass();
        }/*from   w w  w .j ava 2s .co  m*/

        // Populate enclosing classes
        type = targetType;
        while (type.isMemberClass()) {
            genericType = type.getGenericSuperclass();
            if (genericType instanceof ParameterizedType)
                buildTypeVariableMap((ParameterizedType) genericType, map);

            type = type.getEnclosingClass();
        }

        if (cacheEnabled)
            typeVariableCache.put(targetType, new WeakReference<Map<TypeVariable<?>, Type>>(map));
    }

    return map;
}

From source file:io.werval.runtime.util.TypeResolver.java

private static Map<TypeVariable<?>, Type> getTypeVariableMap(final Class<?> targetType) {
    Reference<Map<TypeVariable<?>, Type>> ref = CACHE.get(targetType);
    Map<TypeVariable<?>, Type> map = ref != null ? ref.get() : null;

    if (map == null) {
        map = new HashMap<>();

        // Populate interfaces
        buildTypeVariableMap(targetType.getGenericInterfaces(), map);

        // Populate super classes and interfaces
        Type genericType = targetType.getGenericSuperclass();
        Class<?> type = targetType.getSuperclass();
        while (type != null && !Object.class.equals(type)) {
            if (genericType instanceof ParameterizedType) {
                buildTypeVariableMap((ParameterizedType) genericType, map);
            }/* www  .ja va 2 s .  c o  m*/
            buildTypeVariableMap(type.getGenericInterfaces(), map);

            genericType = type.getGenericSuperclass();
            type = type.getSuperclass();
        }

        // Populate enclosing classes
        type = targetType;
        while (type.isMemberClass()) {
            genericType = type.getGenericSuperclass();
            if (genericType instanceof ParameterizedType) {
                buildTypeVariableMap((ParameterizedType) genericType, map);
            }

            type = type.getEnclosingClass();
        }

        if (cacheEnabled) {
            CACHE.put(targetType, new WeakReference<>(map));
        }
    }

    return map;
}

From source file:antre.TypeResolver.java

/**
 * Resolves the generic Type for the {@code targetType} by walking the type hierarchy upwards from
 * the {@code initialType}.//w w  w .  j  a v  a2 s  . co  m
 */
public static Type resolveGenericType(Type initialType, Class<?> targetType) {
    Class<?> rawType;
    if (initialType instanceof ParameterizedType)
        rawType = (Class<?>) ((ParameterizedType) initialType).getRawType();
    else
        rawType = (Class<?>) initialType;

    if (targetType.equals(rawType))
        return initialType;

    Type result;
    if (targetType.isInterface()) {
        for (Type superInterface : rawType.getGenericInterfaces())
            if (superInterface != null && !superInterface.equals(Object.class))
                if ((result = resolveGenericType(superInterface, targetType)) != null)
                    return result;
    }

    Type superType = rawType.getGenericSuperclass();
    if (superType != null && !superType.equals(Object.class))
        if ((result = resolveGenericType(superType, targetType)) != null)
            return result;

    return null;
}