• Post Reply Bookmark Topic Watch Topic
  • New Topic

Gathering names of all classes in a package?  RSS feed

 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the situation. I have a package which contains multiple classes. At one stage in my application, I'd like to have the user choose which class he/she would like to use - each class in that package can parse a different type of file. However, in order to allow them to choose, I need to be able to list all of the classes that are currently in that package. Unfortunately, I don't know how to get that information - I know the package name, but I don't know the names of the classes that are contained in that package.
The key is that I need to remain very flexible. Although I know the names of all classes in that package today, I might need to add a class to that package in a week or two, or maybe in a couple months, or maybe a year from now. I'd like to be able to add that class to the package and have the application "see" it immediately. I'd rather not change that code, if I don't have to.
One possible solution would be to create a text file which contains the names of all of the classes within that package. I could then read the class names from that file and display them for the user. However, that just requires me to keep an extra file lying around.
Any ideas, anyone?
Thanks,
Corey
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Checkout this article at JavaWorld. You may find it helpful.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wild brainstorm: can't you keep your package's classes in a jar file and use java.util.zip.ZipFile and java.util.ZipEntry classes to manage it. Jar files are zipped files are they not?
[ February 16, 2004: Message edited by: Barry Gaunt ]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use the Factory pattern (I found a good description of this pattern on this page through Google...) and register specific classes with the types of files. Kinda how JEditorPane does it... you register HTMLEditorKit with html files, RTFEditorKit with rtf files, etc.

This isn't based off the "package" a file is in, rather it's based off the interface or abstract class that all of your classes implement. But it is very flexible, and you can get all the file types that have a registered handler by just returning the keys of the internal Map that holds the class that is registered to each file type.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!