Listing all the subclasses of a specific class in Java

0 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,450 points
369 views

2 answers to this question.

0 votes

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,640 points
0 votes

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 by Sirajul
• 39,540 points

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 in Java by Sirajul
• 39,540 points
112 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,880 points
624 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 in Java by anonymous
3,011 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,050 points
57 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,380 points

edited Nov 6, 2018 by Omkar 10,493 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,110 points
2,318 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,110 points
6,033 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,110 points
723 views
0 votes
1 answer

Why the main() method in Java is always static?

As you might know, static here is ...READ MORE

answered May 8, 2018 in Java by geek.erkami
• 2,640 points
423 views
0 votes
2 answers

Fetch list of in-between dates using Java

java.time Package The new java.time.package in Java 8 incorporates ...READ MORE

answered Aug 21 in Java by Sirajul
• 39,540 points
141 views