Java Reflection Interface Get equalInterfaces(Object obj1, Object obj2)

Here you can find the source of equalInterfaces(Object obj1, Object obj2)

Description

Returns true if the interfaces implemented by obj1's class are the same (and in the same order) as obj2's class.

License

Apache License

Parameter

Parameter Description
obj1 object1 for comparison
obj2 object2 for comparison

Return

true of obj1's class implements the same interfaces as obj2's one

Declaration

public static boolean equalInterfaces(Object obj1, Object obj2) 

Method Source Code

//package com.java2s;
/*/*w w w.  ja v  a 2  s  .c  o m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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;

public class Main {
    /**
     * Returns true if the interfaces implemented by obj1's class
     * are the same (and in the same order) as obj2's class.
     *
     * @param obj1 object1 for comparison
     * @param obj2 object2 for comparison
     * @return true of obj1's class implements the same interfaces as obj2's one
     */
    public static boolean equalInterfaces(Object obj1, Object obj2) {
        if (obj1 == obj2) {
            return true;
        }

        if ((obj1 == null || obj2 == null) && obj1 != obj2) {
            return false;
        }
        Class[] intf1 = getAllInterfaces(obj1);
        Class[] intf2 = getAllInterfaces(obj2);

        if (intf1.length != intf2.length) {
            return false;
        } else {
            for (int i = 0; i < intf1.length; i++) {
                if (intf1[i] != intf2[i]) {
                    return false;
                }
            }
            return true;
        }
    }

    /**
     * Returns an array containing all interfaces implemented
     * by the given object or null if class is null.
     *
     * @param obj object for getting interfaces
     * @return an array containing all interfaces implemented by obj
     *         or null if obj is null
     */
    public static Class[] getAllInterfaces(Object obj) {
        if (obj == null) {
            return null;
        }
        ArrayList list = new ArrayList();
        getAllInterfaces(list, obj.getClass());
        return (Class[]) list.toArray(new Class[list.size()]);
    }

    /**
     * Fills the given array list with all interfaces implemented
     * by the given class.
     *
     * @param list array list for filling
     * @param class class for getting interfaces
     */
    private static void getAllInterfaces(ArrayList list, Class cl) {
        Class superclass = cl.getSuperclass();

        if (superclass != null) {
            getAllInterfaces(list, superclass);
        }
        Class[] interfaces = cl.getInterfaces();

        for (int i = 0; i < interfaces.length; i++) {
            Class intf = interfaces[i];

            if (!(list.contains(intf))) {
                list.add(intf);
            }
        }
    }
}

Related

  1. addInterfaceName(String name)
  2. getAllClassesAndInterfaces(Class startClass)
  3. getAllInterfaces(Class clazz)
  4. getAllInterfaces(Class clazz)
  5. getAllInterfaces(Class clazz)