Java Reflection Interface Get getAllInterfacesForClass(Class clazz)

Here you can find the source of getAllInterfacesForClass(Class clazz)

Description

Return all interfaces that the given class implements as array, including ones implemented by superclasses.

License

Open Source License

Parameter

Parameter Description
clazz the class to analyse for interfaces

Return

all interfaces that the given object implements as array

Declaration

public static Class[] getAllInterfacesForClass(Class clazz) 

Method Source Code

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

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**//from ww  w .  j a va  2 s  .c om
     * Return all interfaces that the given class implements as array, including
     * ones implemented by superclasses.
     * <p>
     * If the class itself is an interface, it gets returned as sole interface.
     * @param clazz
     *            the class to analyse for interfaces
     * @return all interfaces that the given object implements as array
     */
    public static Class[] getAllInterfacesForClass(Class clazz) {
        return getAllInterfacesForClass(clazz, null);
    }

    /**
     * Return all interfaces that the given class implements as array, including
     * ones implemented by superclasses.
     * <p>
     * If the class itself is an interface, it gets returned as sole interface.
     * @param clazz
     *            the class to analyse for interfaces
     * @param classLoader
     *            the ClassLoader that the interfaces need to be visible in (may
     *            be <code>null</code> when accepting all declared interfaces)
     * @return all interfaces that the given object implements as array
     */
    public static Class[] getAllInterfacesForClass(Class clazz, ClassLoader classLoader) {
        // Assert.notNull(clazz, "Class must not be null");
        if (clazz.isInterface()) {
            return new Class[] { clazz };
        }
        List interfaces = new ArrayList();
        while (clazz != null) {
            for (int i = 0; i < clazz.getInterfaces().length; i++) {
                Class ifc = clazz.getInterfaces()[i];
                if (!interfaces.contains(ifc) && (classLoader == null || isVisible(ifc, classLoader))) {
                    interfaces.add(ifc);
                }
            }
            clazz = clazz.getSuperclass();
        }
        return (Class[]) interfaces.toArray(new Class[interfaces.size()]);
    }

    /**
     * Check whether the given class is visible in the given ClassLoader.
     * @param clazz
     *            the class to check (typically an interface)
     * @param classLoader
     *            the ClassLoader to check against (may be <code>null</code>, in
     *            which case this method will always return <code>true</code>)
     */
    public static boolean isVisible(Class clazz, ClassLoader classLoader) {
        if (classLoader == null) {
            return true;
        }
        try {
            Class actualClass = classLoader.loadClass(clazz.getName());
            return (clazz == actualClass);
            // Else: different interface class found...
        } catch (ClassNotFoundException ex) {
            // No interface class found...
            return false;
        }
    }
}

Related

  1. getAllInterfaces(Class cls)
  2. getAllInterfaces(Class cls)
  3. getAllInterfaces(Class type)
  4. getAllInterfaces(Object object)
  5. getAllInterfaces(Set> searchInterfaces, Class clazz)
  6. getAllInterfacesNames(Class aClass)
  7. getClassHierarchy(Class c, boolean includeInterfaces)
  8. getDeclaredInterfaces(Class claz, Class... assignableClasses)
  9. getImplementedInterfaceParents(Class clazz, Set classesResult)