The degree of connection between the Java object and the classes / interfaces?

Is there any way through the standard library or some existing library to determine the degree of connection between the two classes / interfaces in Java?

Let's say I have an object and a list of classes / interfaces. Now I basically want to know one class of this list that has the shortest path of the inheritance tree to this object.

I already looked through the java.lang.reflect and Class packages, but could not find anything that would make it easy to access such information. Perhaps this is already part of another library?

+3
source share
5 answers

I do not know anything ready to use.

Reflection, .

- .

  • , :

    • , , ?
    • , ?
      ? ()?...
  • , , (, , ) .....

+1

Reflection , , , . - , .

+3

. , .

+1

. , . . , , . , , , , , , .

public class InheritenceDepth {

/**
 * Obtains a list of all the possible inheritance paths from the given targetClass
 * to the specified potentialAncestorClass.  If the targetClass does not extend or implement
 * the potentialAncestorClass the return list will be empty.
 */
public static List<InheritancePath> classInheritancePaths(Class<?> targetClass, Class<?> potentialAncestorClass){
    List<InheritancePath> returnList = new ArrayList<InheritancePath>();
    if(potentialAncestorClass.isAssignableFrom(targetClass)){

        if(potentialAncestorClass.equals(targetClass)){
            returnList.add(new InheritancePath(potentialAncestorClass));
        }

        if(targetClass.getSuperclass() != null){
            // try superclass
            List<InheritancePath> pathsFromSuperClass = 
                classInheritancePaths(targetClass.getSuperclass(), potentialAncestorClass);
            if(!pathsFromSuperClass.isEmpty()){
                for(InheritancePath path : pathsFromSuperClass){
                    path.add(targetClass);
                    returnList.add(path);
                }
            }
        }

        // try interfaces
        for(Class<?> interf : targetClass.getInterfaces()){
            List<InheritancePath> pathsFromInterface = 
                classInheritancePaths(interf, potentialAncestorClass);
            if(!pathsFromInterface.isEmpty()){
                for(InheritancePath path : pathsFromInterface){
                    path.add(targetClass);
                    returnList.add(path);
                }
            }
        }
    }
    return returnList;
}

/**
 * Represents the path from a base class to a superclass
 */
public static final class InheritancePath implements Iterable<Class<?>>{
    private List<Class<?>> path = new ArrayList<Class<?>>();
    public InheritancePath(Class<?> root){
        path.add(root);
    }

    void add(Class<?> pathElement){
        path.add(0, pathElement);
    }

    public Iterator<Class<?>> iterator(){
        return path.iterator();
    }

    public int depth(){
        return path.size();
    }

    public String toString(){
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < path.size(); i++){
            sb.append(path.get(i).getName());
            if(i < path.size() - 1){
                sb.append(" -> ");
            }
        }
        return sb.toString();
    }
}

public static void main(String[] args) {
    List<InheritancePath> paths = classInheritancePaths(ConcurrentLinkedQueue.class, Collection.class);

    for(InheritancePath path : paths){
        System.out.println(path);
    }
}

}

+1

. , , , . ..

FooBar3 FooBar2 FooBar.

public static void main(String[] args) {
    List<Class<?>> l = new ArrayList<Class<?>>() {{
        add(FooBar2.class);
        add(FooBar.class);
    } };
    System.out.println(getClosestParent(new FooBar3(), l));
 }

public static Class getClosestParent(Object o, List<Class<?>> classes) {
    List<Class<?>> related = getRelated(o, classes);
    Collections.sort(related, new Comparator<Class<?>>() {
        public int compare(Class<?> o1, Class<?> o2) {
            if (o1.isAssignableFrom(o2)) {
                return -1;
            } else if (o2.isAssignableFrom(o1)) {
                return 1;
            }
            return 0;
        }
    });
    return related.get(0);
}

public static List<Class<?>> getRelated(Object o, List<Class<?>> classes) {
    List<Class<?>> filtered = new ArrayList<Class<?>>();
    for (Class<?> aClass : classes) {
        if (aClass.isAssignableFrom(o.getClass())) {
            filtered.add(aClass);
        }

    }
    return filtered;
}
0
source

Source: https://habr.com/ru/post/1727381/


All Articles