Java Reflection Interface Get getAllInterfaces(Class cls)

Here you can find the source of getAllInterfaces(Class cls)

Description

Gets a List of all interfaces implemented by the given class and its superclasses.

The order is determined by looking through each interface in turn as declared in the source file and following its hierarchy up.

License

Open Source License

Parameter

Parameter Description
cls the class to look up, may be <code>null</code>

Return

the List of interfaces in order, null if null input

Declaration

public static List getAllInterfaces(Class cls) 

Method Source Code

//package com.java2s;
/*// w  w  w  .j a  v a  2 s.c  om
 * Copyright (c) 2007-2016 AREasy Runtime
 *
 * This library, AREasy Runtime and API for BMC Remedy AR System, is free software ("Licensed Software");
 * you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either version 2.1 of the License,
 * or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * including but not limited to, the implied warranty of MERCHANTABILITY, NONINFRINGEMENT,
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 */

import java.util.*;

public class Main {
    /**
     * <p>Gets a <code>List</code> of all interfaces implemented by the given
     * class and its superclasses.</p>
     * <p/>
     * <p>The order is determined by looking through each interface in turn as
     * declared in the source file and following its hierarchy up. Then each
     * superclass is considered in the same way. Later duplicates are ignored,
     * so the order is maintained.</p>
     *
     * @param cls the class to look up, may be <code>null</code>
     * @return the <code>List</code> of interfaces in order,
     *         <code>null</code> if null input
     */
    public static List getAllInterfaces(Class cls) {
        if (cls == null)
            return null;

        List list = new ArrayList();
        while (cls != null) {
            Class[] interfaces = cls.getInterfaces();
            for (int i = 0; i < interfaces.length; i++) {
                if (list.contains(interfaces[i]) == false) {
                    list.add(interfaces[i]);
                }
                List superInterfaces = getAllInterfaces(interfaces[i]);
                for (Iterator it = superInterfaces.iterator(); it.hasNext();) {
                    Class intface = (Class) it.next();
                    if (list.contains(intface) == false) {
                        list.add(intface);
                    }
                }
            }

            cls = cls.getSuperclass();
        }

        return list;
    }
}

Related

  1. getAllClassesAndInterfaces(Class startClass)
  2. getAllInterfaces(Class clazz)
  3. getAllInterfaces(Class clazz)
  4. getAllInterfaces(Class clazz)
  5. getAllInterfaces(Class cls)
  6. getAllInterfaces(Class base)
  7. getAllInterfaces(Class clazz)
  8. getAllInterfaces(Class clazz)
  9. getAllInterfaces(Class clazz)