Forums Register Login

Finding and instantiating classes implementing a specific interface

+Pie Number of slices to send: Send
Hey Ranchers!

I have the following situation:
I have some dozen classes implementing a common interface, all in the same package. Now, I need one instance of every class and add it to a list. It would require a lot of work to do this by hand, and I would have to change this method every time I add or remove classes.
Is there any way to have this done automatically?
+Pie Number of slices to send: Send
 

Jan Hoppmann wrote:
Is there any way to have this done automatically?



In the general case no, because you can't in general know how a given ClassLoader will find its classes. For instance, if you have a URLClassLoader where one or more of the URLs are http or htp or basically anything other than file, there's no way to search them.

For the common case of a URLClassLoader where all the URLs are file, yes, you can search those directories for .class files, load each class, and test whether it implements that interface. Unless you're writing an IDE or profiler or something, though, that's a lot of work for very little gain.

A better approach would be to use a config file or something that lists the classes, or, worst case, lists some directories to search for these classes.

Also, if you want to automatically reflectively instantiate these classes, you'll have to document a requirement for one or more constructor signatures, and if any of those sigs take args, you'll have to have some way to figure out what to use for those args, or provide some valid defaults. Note that you can't enforce this at compile time, so you'll have to be prepared to handle the exception that gets thrown when you try to instantiate an object using a non-existent c'tor.
+Pie Number of slices to send: Send
Thankfully, none of the classes' constructors take arguments.
Flexibility would be another thing - if this can be done at runtime, it would be possible to plug in new classes without having to recompile and restart the application.

But if it really is too much work, I'll think about a config file.
+Pie Number of slices to send: Send
 

Jan Hoppmann wrote:Thankfully, none of the classes' constructors take arguments.
Flexibility would be another thing - if this can be done at runtime, it would be possible to plug in new classes without having to recompile and restart the application.

But if it really is too much work, I'll think about a config file.



You can do it at runtime without having to automatically find them. You can re-read the config file on demand, or take input of the names or locations of the classes at any point without restarting. And it's not too hard to automatically search if you can provide a list of places to search. But simply "find all of them anywhere that you can access" is not tenable in the general case.
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 519 times.
Similar Threads
Multiple Implementations of @Local interface
Abstract Class vs. Interface
USE of Interface
should an interface implement Serializable
Interfaces Question(s)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:18:13.