Here you can find the source of distance(final Class> child, final Class> parent)
Gets derivation distance of an object<br> Possible values are:<br> - -1 = child !instanceof parent<br> - 0 = child is the same class as the parent one<br> - greater than 0 = child is the nth of the parent<br> <br> Copied from<br> https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/reflect/InheritanceUtils.java
Parameter | Description |
---|---|
child | Child class |
parent | Parent class |
public static int distance(final Class<?> child, final Class<?> parent)
//package com.java2s; //License from project: Open Source License public class Main { /**//from ww w .j av a 2 s .c o m * Gets derivation distance of an object<br> * Possible values are:<br> * - -1 = child !instanceof parent<br> * - 0 = child is the same class as the parent one<br> * - greater than 0 = child is the nth of the parent<br> * <br> * Copied from<br> * https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/reflect/InheritanceUtils.java * @param child Child class * @param parent Parent class * @return */ public static int distance(final Class<?> child, final Class<?> parent) { if (child == null || parent == null) { return -1; } if (child.equals(parent)) { return 0; } final Class<?> cParent = child.getSuperclass(); int d = parent.equals(cParent) ? 1 : 0; if (cParent != null) { Class<?>[] interfaces = cParent.getInterfaces(); for (Class<?> i : interfaces) { if (parent.equals(i)) d = 1; } } if (d == 1) { return d; } d += distance(cParent, parent); return d > 0 ? d + 1 : -1; } }