Listing all the subclasses of a specific class in Java

+2 votes

How do I list down all the subclasses of a specific class or all the implementers of a specific interface in Java? Using Eclipse, I am able to do so but I want to know how to perform this programmatically? If you have any suggestions, please do put them.

Jun 7, 2018 in Java by code.reaper12
• 3,470 points
2,491 views

3 answers to this question.

+1 vote

There is a simple library available for this purpose: org.reflections

This will scan your classpath, index the metadata, allow you to query it on runtime and may save and collect that information for many modules within your project.

Using Reflections you can query your metadata such as:

  • get all subtypes of some type

  • get all types/constructors/methods/fields annotated with some annotation, optionally with annotation parameters matching

  • get all resources matching a regular expression

  • get all methods with specific signature including parameters, parameter annotations, and return type

  • get all methods parameter names

  • get all fields/methods/constructors usages in code

You just need to call the following to fulfill your task:

reflections.getSubTypesOf(aClazz))

answered Jun 7, 2018 by geek.erkami
• 2,680 points
+1 vote

This is not possible to do using only the built-in Java Reflections API.

A project exists that does the necessary scanning and indexing of your classpath so you can get access this information...

Reflections

A Java runtime metadata analysis, in the spirit of Scannotations

Reflections scans your classpath, indexes the metadata, allows you to query it on runtime and may save and collect that information for many modules within your project.

Using Reflections you can query your metadata for:

· get all subtypes of some type

· get all types annotated with some annotation

· get all types annotated with some annotation, including annotation parameters matching

· get all methods annotated with some

Source : Stackoverflow

answered Aug 21, 2019 by Sirajul
• 55,300 points
+2 votes

Try with BurningWave core library. Here an example that solves your question:

https://github.com/burningwave/core/wiki/Finding-all-classes-that-extend-a-base-class
 

answered Dec 29, 2019 by anonymous
• 370 points

edited Jan 9 by anonymous

Related Questions In Java

0 votes
2 answers

Get all the permutations of a string in Java

You could use recursion to do this.  Try ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 55,300 points
230 views
0 votes
2 answers

How can I get the filenames of all files in a folder which may or may not contain duplicates

List<String> results = new ArrayList<String>(); File[] files = ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,890 points
715 views
0 votes
2 answers

Include all the jars in a directory within the Java classpath

Just use lib/*   not lib/*.jar   That's the ...READ MORE

answered Apr 6, 2019 in Java by anonymous
6,425 views
0 votes
2 answers

What is the use of final class in java?

In Java, items with the final modifier cannot be ...READ MORE

answered Oct 5, 2018 in Java by Daisy
• 8,090 points
121 views
0 votes
2 answers

What is the easiest way to iterate through the characters of a string in Java?

There are two approaches to this: for(int i ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 55,300 points
250 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,420 points

edited Nov 6, 2018 by Omkar 26,485 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,510 points
3,951 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,510 points
9,368 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,510 points
1,708 views
+1 vote
2 answers

Retrieve all the implementations of an interface in Java

Take a look to this example: https://github.com/burningwave/co ...READ MORE

answered Dec 20, 2019 in Java by Roberto
• 370 points

edited Jul 6 by Roberto 7,580 views