Java Class Hierarchy Get getDistance(Class a, Class b)

Here you can find the source of getDistance(Class a, Class b)

Description

get Distance

License

Apache License

Parameter

Parameter Description
a Class source class
b Class target class

Return

inheritance distance between two classes, or Integer.MAX_VALUE if they are not related. Each step upward in the inheritance from one class to the next (calling class.getSuperclass()) is counted as 1. This can be a lot of computational effort, therefore the results of this determination should be cached.

Declaration

public static int getDistance(Class a, Class b) 

Method Source Code

//package com.java2s;
/**/*from w w  w  .j a  va2  s .c  o  m*/
 * 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 {
    /**
     * @param a Class source class
     * @param b Class target class
     * @return inheritance distance between two classes, or Integer.MAX_VALUE if they are not related. Each
     * step upward in the inheritance from one class to the next (calling class.getSuperclass()) is counted
     * as 1. This can be a lot of computational effort, therefore the results of this determination should be cached.
     */
    public static int getDistance(Class a, Class b) {
        if (a.isInterface()) {
            return getDistanceToInterface(a, b);
        }
        Class curr = b;
        int distance = 0;

        while (curr != a) {
            distance++;
            curr = curr.getSuperclass();
            if (curr == null) {
                return Integer.MAX_VALUE;
            }
        }

        return distance;
    }

    /**
     * @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. getDistanceToInterface(Class to, Class from)
  5. getTypeHierarchy(Class cls)