Java Get All Superclasses of a Class using Apache Commons Lang
Tags: ClassUtils Apache Commons Apache Commons Lang Class Superclass
This Java tutorial shows you how to get a list of all superclasses of a given Class object in Java using 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 Superclasses of a Class in Java
The Apache Commons Lang library provides the method ClassUtils.getAllSuperclasses() to return a List of Class objects, the list of superclasses of a given Class object. You can learn how to use this method in the following example code to get all superclasses of LinkedHashSet class.
GetAllSuperclasses.java
import org.apache.commons.lang3.ClassUtils;
import java.util.LinkedHashSet;
import java.util.List;
public class GetAllSuperclasses {
public static void main(String... args) {
// Get all supper classed of LinkedHashSet class
List<Class<?>> listOfClasses = ClassUtils.getAllSuperclasses(LinkedHashSet.class);
for(Class item : listOfClasses) {
System.out.println(item);
}
}
}
class java.util.HashSet
class java.util.AbstractSet
class java.util.AbstractCollection
class java.lang.Object
Happy Coding 😊