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 super-classes.

License

Apache License

Parameter

Parameter Description
cls the class to look up, may be null .

Return

the List of interfaces in order, null if null input.

Declaration

public static List<Class<?>> getAllInterfaces(Class<?> cls) 

Method Source Code

//package com.java2s;
/*/*from w w w . j a v a2  s. com*/
 * Copyright (c) 2014  Haixing Hu
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Gets a {@code List} of all interfaces implemented by the given class
     * and its super-classes.
     * <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.
     *
     * @param cls
     *          the class to look up, may be {@code null}.
     * @return the {@code List} of interfaces in order, {@code null} if
     *         null input.
     * @since 1.0.0
     */
    public static List<Class<?>> getAllInterfaces(Class<?> cls) {
        if (cls == null) {
            return null;
        }
        final List<Class<?>> list = new ArrayList<Class<?>>();
        while (cls != null) {
            final Class<?>[] interfaces = cls.getInterfaces();
            for (final Class<?> interface1 : interfaces) {
                if (list.contains(interface1) == false) {
                    list.add(interface1);
                }
                final List<Class<?>> superInterfaces = getAllInterfaces(interface1);
                for (final Class<?> intface : superInterfaces) {
                    if (list.contains(intface) == false) {
                        list.add(intface);
                    }
                }
            }
            cls = cls.getSuperclass();
        }
        return list;
    }
}

Related

  1. getAllInterfaces(Class base)
  2. getAllInterfaces(Class clazz)
  3. getAllInterfaces(Class clazz)
  4. getAllInterfaces(Class clazz)
  5. getAllInterfaces(Class cls)
  6. getAllInterfaces(Class type)
  7. getAllInterfaces(Object object)
  8. getAllInterfaces(Set> searchInterfaces, Class clazz)
  9. getAllInterfacesForClass(Class clazz)