I want to recursively get the superclass and interface of a certain class

Write a program design document-or rather, I had the opportunity to create a program design document from the source code, but the design document says, "Write all the parent class names and all the interfaces implemented in the created Java class together. It was something like. It's a hassle to make this foolishly by hand. So I decided to write a program called __ "Recursely find the parent class and interface from the class name" __. Below is a sample of it.



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

public class Main {
    public static void main(String[] args) throws ClassNotFoundException {
        //Create a class object from the class name.
        Class<?> pivotClass = Class.forName("java.util.ArrayList");
        
        //Follow the superclass recursively.
        List<Class<?>> classes = new ArrayList<>();
        Class<?> superClass = pivotClass;
        while (superClass != null) {
            classes.add(superClass);
            superClass = superClass.getSuperclass();
        }
        
        //Get a list of recursively acquired superclasses.
        for (Class<?> clazz : classes) {
            System.out.println(clazz.getName());
        }
        /* java.util.ArrayList
           java.util.AbstractList
           java.util.AbstractCollection
            java.lang.Object */        
        
        //Get a list of implemented interfaces&Output.
        Class<?>[] interfaces = pivotClass.getInterfaces();
        for (Class<?> interfaze : interfaces) {
            System.out.println(interfaze.getName());
        }
        /* java.util.List
           java.util.RandomAccess
           java.lang.Cloneable
           java.io.Serializable */
    }
}

It's a little strange that the class itself can be treated as a class, but it's useful once you get used to it. Hurray for metaprogramming (´ ・ ω ・ `)

Recommended Posts

I want to recursively get the superclass and interface of a certain class
I want to get a list of the contents of a zip file and its uncompressed size
I want to call a method of another class
I want to call a method and count the number
I want to give a class name to the select attribute
I want to recursively search the class list under the package
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
I tried to express the result of before and after of Date class with a number line
[Ruby] I want to extract only the value of the hash and only the key
I want to pass the argument of Annotation and the argument of the calling method to aspect
I want to get the field name of the [Java] field. (Old tale tone)
I want to output the day of the week
I want to var_dump the contents of the intent
I want to get the value in Ruby
I want to get the value of Cell transparently regardless of CellType (Apache POI)
I want to control the start / stop of servers and databases with Alexa
What I tried when I wanted to get all the fields of a bean
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
I want to add a delete function to the comment function
[Ruby] I want to make a program that displays today's day of the week!
I want to use PowerMock in a class that combines parameterized tests and ordinary tests
[Rails] I want to display the link destination of link_to in a separate tab
I want to get the information of the class that inherits the UserDetails class of the user who is logged in with Spring Boot.
I tried to summarize the basics of kotlin and java
I want to bring Tomcat to the server and start the application
I want to expand the clickable part of the link_to method
I want to make a specific model of ActiveRecord ReadOnly
I want to change the log output settings of UtilLoggingJdbcLogger
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I tried JAX-RS and made a note of the procedure
I want to create a form to select the [Rails] category
I want to narrow down the display of docker ps
[jsoup] How to get the full amount of a document
[Ruby] I want to reverse the order of the hash table
I want to temporarily disable the swipe gesture of UIPageViewController
[Java] I thought about the merits and uses of "interface"
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
I want to recursively search for files under a specific directory
I want to give edit and delete permissions only to the poster
I want to create a chat screen for the Swift chat app!
[Rails] How to get the URL of the transition source and redirect
I want to understand the flow of Spring processing request parameters
I want to return to the previous screen with kotlin and java!
The story of Collectors.groupingBy that I want to keep for posterity
I made a gem to post the text of org-mode to qiita
Method to add the number of years and get the end of the month
I want to limit the input by narrowing the range of numbers
I tried to summarize the methods of Java String and StringBuilder
I want to control the default error message of Spring Boot
I made a tool to output the difference of CSV file
I want to change the value of Attribute in Selenium of Ruby
[Android] I want to get the listener from the button in ListView
[Swift] When you want to know if the number of characters in a String matches a certain number ...
I want to judge the necessity of testing by comparing the difference of class files when refactoring Java
[Controller] I want to retrieve the numerical value of a specific column from the DB (my memo)
[Spring Boot] I want to add my own property file and get the value with env.getProperty ().
I want to develop a web application!