In Java how do I find a list of classes that implement a given interface at runtime?
-
Suppose I have an interface called Account. public interface Account { public int getAccountNumber(); } Say I then create two implementations: public class BankAccount implements Account { public int getAccountNumber() { return 100; } } public class TelephoneAccount implements Account { public int getAccountNumber() { return 200; } } ... Now I want to create a class with a main method that when executed lists all implementations of the Account interface. It should also pick up any implementations that are stored within JAR files that are on the classpath etc. So far I have tried using reflection and the apache commons discovery project, but have not been successful :-( Any anyone help? :-)
-
Answer:
The Java class loader cannot know whether a class implements an interface without actually loading the class. Therefore you are proposing to load every single class in the class path in order to find out if any of them implements your interface. THIS IS MADNESS. I will take you forever just to load the classes in the Java Runtime Library (although one might argue that it is *very* unlikely that one of those classes implements your interface :-) ). Your algorithm for finding Account types is like this algorithm for finding all the elephants in Africa: - start at the southern tip of Africa - move from east to west and: . . -- grab every animal in arm's reach and check it to see if it is an elephant . . -- if so, put it in your backpack; if not, put it back where it was - when you reach the shore/border move one "arm's reach" north and reverse direction and continue as before. - stop when you reach the northern tip of Africa - inventory the animals in your backpack - DONE There are several fairly major problems with this algorithm (as there are with yours): - it takes too long to execute; your first elephant will die long before you find your last - it requires too much backpack space; how many elephants will a typical backpack hold anyway? - it assumes there are no obstacles to a straight-line path; lakes, cliffs, rivers, mountains, hungry panthers, etc. - it assumes every elephant in Africa will stand still while you execute your search However, YOUR search does not have to be this problematic. Why try to deal with all the wild elephants in Africa? Why not just deal with elephants in zoos? You are the one defining what it means to be an "elephant" (Account). Why not cage the elephants by building the cage into the definition of "elephant"? Why not build an AccountTypeRegistry and require that every account type be registered? It would not be hard to add a getRegistryId() to your Account interface. It would not be hard to set up your program/framework to only work with registered account types. Basically you want to make the "contract" for your Account interface such that anyone who creates an account type but does not register it will miss out on all the goodies. That is how you ensure that you will always be able to find all the account types.
Tim C at Yahoo! Answers Visit the source
Other answers
There is an easy way and a hard way. Hard way: I suppose if you find some way to iterate through all classes possible, you could check each one to see if it extends the interface. Not sure how to list all the classes though, gotta figure that one out. Easy way: eclipse already did it. For example, in the package explorer, right click the interface .java file and select 'show type hierarchy.'
billyboy
Related Q & A:
- How do i find a profile of a person with a yahoo mail address?Best solution by Yahoo! Answers
- How can I find a list of births in Wiesbaden, Germany?Best solution by Yahoo! Answers
- Where can I find a list of the largest forums on the Internet?Best solution by theforumfinder.org
- Where can I find a list of popular forums?Best solution by ChaCha
- Where can I find a list of past track champions from Kentucky motor speedway in Whitesville Kentucky?Best solution by kentuckyspeedway.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.