45

java.lang.Class.getInterfaces returns all directly implemented interfaces ie doesn't walk the class tree to get all interfaces of all parent types. eg For example the hierarchy

public interface A {}
public interface B {}
public interface C extends B {}

public class Foo implements A {} // Foo.class.getInterfaces() returns [A]
public class Bar implements C {} // Bar.class.getInterfaces() returns [C], note B is not included.

For Bar I would like to get [B, C], but for any arbitrary tree depth.

I could write this myself, but I'm sure a library must exist that does this already, any ideas?

markdsievers
  • 7,151
  • 11
  • 51
  • 83

5 Answers5

52

Apache Commons Lang has method you need: ClassUtils.getAllInterfaces

Steve Chambers
  • 37,270
  • 24
  • 156
  • 208
Alex Gitelman
  • 24,429
  • 7
  • 52
  • 49
22

Guava Solution:

final Set<TypeToken> tt = TypeToken.of(cls).getTypes().interfaces();

This is a much more powerful and cleaner reflection API than the ancient Apache stuff.

Community
  • 1
  • 1
  • +1 I was looking to get all interfaces so I could use the built in [newProxyInstance](https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Proxy.html#newProxyInstance-java.lang.ClassLoader-java.lang.Class:A-java.lang.reflect.InvocationHandler-) method and guava had a different method for creating dynamic proxies that was much more simple: 'Reflection.newProxy'. See [Reflection Explained](https://github.com/google/guava/wiki/ReflectionExplained) – akagixxer Sep 09 '16 at 17:00
8

Don't forget, Spring Framework has many similar util classes like Apache Commons Lang. So there is: org.springframework.util.ClassUtils#getAllInterfaces

Ihor Rybak
  • 3,091
  • 2
  • 25
  • 32
0
public interface A {}

public interface B {}

public interface E extends B{ }

public class C implements A{}

public class D extends C implements E{}

enter image description here

public class App {

    public static void main(String[] args) {

        final List<Class<?>> result = getAllInterfaces(D.class);            
        for (Class<?> clazz : result) {
            System.out.println(clazz);
        }
    }

    public static List<Class<?>> getAllInterfaces(Class<?> clazz) {
        if (clazz == null) {
            System.out.println(">>>>>>>>>> Log : null argument ");
            return new ArrayList<>();
        }
        List<Class<?>> interfacesFound = new ArrayList<>();
        getAllInterfaces(clazz, interfacesFound);
        return interfacesFound;
    }

    private static void getAllInterfaces(Class<?> clazz,
                                         List<Class<?>> interfacesFound) {
        while (clazz != null) {
            Class<?>[] interfaces = clazz.getInterfaces();

            for (int i = 0; i < interfaces.length; i++) {
                if (!interfacesFound.contains(interfaces[i])) {
                    interfacesFound.add(interfaces[i]);
                    getAllInterfaces(interfaces[i], interfacesFound);
                }
            }
            clazz = clazz.getSuperclass();
        }
    }
}
Mami Alexandre
  • 121
  • 2
  • 6
0
public List<Class<?>> getInterfaces(Class<?> type) {
    return Stream.of(type.getInterfaces())
                 .flatMap(interfaceType -> Stream.concat(Stream.of(interfaceType), getInterfaces(interfaceType).stream()))
                 .collect(Collectors.toList());
}
Lovro Pandžić
  • 5,920
  • 4
  • 43
  • 51