• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding and instantiating classes implementing a specific interface  RSS feed

 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!