Java Reflection Interface Get getDeclaredInterfaces(Class claz, Class... assignableClasses)

Here you can find the source of getDeclaredInterfaces(Class claz, Class... assignableClasses)

Description

Returns an array of all declared interfaces of desired class (super-classes comes an account).

License

Open Source License

Parameter

Parameter Description
claz The class to get all interfaces.

Declaration

@SuppressWarnings("unchecked")
public static List<Class<?>> getDeclaredInterfaces(Class claz,
        Class... assignableClasses) 

Method Source Code

//package com.java2s;
/*//from ww w .j  a va2 s  .c om
 * Copyright (c) 2008-2016, GigaSpaces Technologies, Inc. All Rights Reserved.
 *
 * 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 {
    /**
     * Returns an array of all declared interfaces of desired class (super-classes comes an
     * account). If <code>assignableClasses</code> is <code>null</code> all implemented interface
     * will be added to array, otherwise only <b>assignable</b> classes.
     *
     * @param claz The class to get all interfaces.
     **/
    @SuppressWarnings("unchecked")
    public static List<Class<?>> getDeclaredInterfaces(Class claz,
            Class... assignableClasses) {
        ArrayList<Class<?>> interfList = new ArrayList<Class<?>>();

        while (!claz.equals(Object.class)) {
            for (Class cl : claz.getInterfaces()) {
                /* append only assignable interfaces */
                if (assignableClasses.length > 0) {
                    for (Class assignClaz : assignableClasses) {
                        if (assignClaz.isAssignableFrom(cl))
                            interfList.add(cl);
                    }
                } else
                    interfList.add(cl);
            }

            claz = claz.getSuperclass();
        }

        return interfList;
    }
}

Related

  1. getAllInterfaces(Object object)
  2. getAllInterfaces(Set> searchInterfaces, Class clazz)
  3. getAllInterfacesForClass(Class clazz)
  4. getAllInterfacesNames(Class aClass)
  5. getClassHierarchy(Class c, boolean includeInterfaces)
  6. getImplementedInterfaceParents(Class clazz, Set classesResult)
  7. getImplementedInterfaces(Class clazz)
  8. getImplementedInterfaces(Class cl)
  9. getInterfaceInstance(Class interfaceType)