Java get all advanced interfaces from the base interface

Is it possible to get a list of all the interfaces that extend the base interface without any bean having implemented any interface in java at runtime ?

Example:

interface A {}
interface B extends A{}
interface C extends A{}
interface D extends C{}

public Class<? extends A>[] getAllInterfaces(A.class);

The method getAllInterfaces()should return all interfaces that extend A: {B, C, D}

+4
source share
2 answers

Kind, but not really.

What you need to do is list all the types, and among them find things that are interfaces and extend the interface. (Use Class#isInterfaceand Class#getInterfaces.)

" ". , JVM? , ? , , , .

, - . . : Java - , JVM

+3

, , Reflections .

, , :

public <T> Class<? extends T>[] getAllInterfaces(Class<T> clazz) {

    Reflections reflections = new Reflections(clazz.getClassLoader());

    return reflections.getSubTypesOf(clazz)
            .stream()
            .filter(subClass -> subClass.isInterface())
            .toArray(Class[]::new);
}

, " ". , .

+1

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


All Articles