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 8, 2018 in Java by code.reaper12
• 3,500 points
12,922 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))

Hope this helps!!

To know more about Java, join our Java course online today.

Thank you

answered Jun 8, 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
• 59,230 points
+3 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
• 460 points

edited Jan 9, 2020 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
• 59,230 points
1,802 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,910 points
1,634 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
14,292 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,120 points
1,573 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
• 59,230 points
1,475 views
+5 votes
4 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,450 points

edited Nov 7, 2018 by Omkar 79,304 views
+1 vote
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,500 points
7,920 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,500 points
16,940 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,500 points

edited Aug 4, 2023 by Khan Sarfaraz 4,356 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 21, 2019 in Java by Roberto
• 460 points

edited Jul 7, 2020 by Roberto 36,445 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP