Java Get All Interfaces of a Class using Apache Commons Lang

Tags: ClassUtils Apache Commons Apache Commons Lang Class Interface

This Java tutorial shows you how to get a list of all interfaces that are implemented by a given class and its superclasses using the ClassUtils of Apache Commons Lang library.

How to add Apache Commons Lang 3 library to your Java project

To use the Apache Commons Lang 3 library in the Gradle build project, add the following dependency into the build.gradle file.

implementation 'org.apache.commons:commons-lang3:3.12.0'

To use the Apache Commons Lang 3 library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-lang3</artifactId>
  <version>3.12.0</version>
</dependency>

To have more information about the Apache Commons Lang 3 library you can visit the library home page at commons.apache.org/proper/commons-lang/

How to get All Interfaces of a Class in Java

The Apache Commons Lang library provides the method ClassUtils.getAllInterfaces() to return the List of Class objects, the list of interfaces of a given Class object. You can learn how to use this method in the following example code to get all interfaces of ArrayList class.

GetAllInterfaces.java

import org.apache.commons.lang3.ClassUtils;

import java.util.ArrayList;
import java.util.List;

public class GetAllInterfaces {
    public static void main(String... args) {
        // Get all interfaces of ArrayList class
        List<Class<?>> listOfInterfaces = ClassUtils.getAllInterfaces(ArrayList.class);

        for(Class item : listOfInterfaces) {
            System.out.println(item);
        }
    }
}
The output is:
interface java.util.List
interface java.util.Collection
interface java.lang.Iterable
interface java.util.RandomAccess
interface java.lang.Cloneable
interface java.io.Serializable

Happy Coding 😊