Java Class Hierarchy Get getDistanceToInterface(Class to, Class from)

Here you can find the source of getDistanceToInterface(Class to, Class from)

Description

get Distance To Interface

License

Apache License

Return

int distance between two passed in classes. This method performs an exhaustive walk up the inheritance chain to compute the distance. This can be a lot of computational effort, therefore the results of this determination should be cached internally.

Declaration

static int getDistanceToInterface(Class<?> to, Class<?> from) 

Method Source Code

//package com.java2s;
/**/*  ww  w.j av  a  2  s. c om*/
 * This utility class has the methods mostly related to reflection related code.
 *
 * @author John DeRegnaucourt (jdereg@gmail.com)
 *         <br>
 *         Copyright (c) Cedar Software LLC
 *         <br><br>
 *         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
 *         <br><br>
 *         http://www.apache.org/licenses/LICENSE-2.0
 *         <br><br>
 *         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.*;

public class Main {
    /**
     * @return int distance between two passed in classes.  This method performs an exhaustive
     * walk up the inheritance chain to compute the distance.  This can be a lot of
     * computational effort, therefore the results of this determination should be cached internally.
     */
    static int getDistanceToInterface(Class<?> to, Class<?> from) {
        Set<Class<?>> possibleCandidates = new LinkedHashSet<Class<?>>();

        Class<?>[] interfaces = from.getInterfaces();
        // is the interface direct inherited or via interfaces extends interface?
        for (Class<?> interfase : interfaces) {
            if (to.equals(interfase)) {
                return 1;
            }
            // because of multi-inheritance from interfaces
            if (to.isAssignableFrom(interfase)) {
                possibleCandidates.add(interfase);
            }
        }

        // it is also possible, that the interface is included in superclasses
        if (from.getSuperclass() != null
                && to.isAssignableFrom(from.getSuperclass())) {
            possibleCandidates.add(from.getSuperclass());
        }

        int minimum = Integer.MAX_VALUE;
        for (Class<?> candidate : possibleCandidates) {
            // Could do that in a non recursive way later
            int distance = getDistanceToInterface(to, candidate);
            if (distance < minimum) {
                minimum = ++distance;
            }
        }
        return minimum;
    }
}

Related

  1. getClassHierarchy(Class beanClass)
  2. getClassHierarchy(Class type)
  3. getClassHierarchy(Object obj, Class ignoreClass)
  4. getDistance(Class a, Class b)
  5. getTypeHierarchy(Class cls)