Posted by dgriffit
on October 13, 2004 at 10:01 AM PDT
At present, the only way to find and load a class dynamically is (I believe) Class.forName("my.class.name"). This makes useful plugin architectures possible, but not very clean. The problem is that you have to know the name of the class before you load it, which usually means specifying the name in some configuration resource somewhere, or (worse) hard-coding it. When you are creating an architecture which may contain a large number of pluggable components which you wish to dynamically load, this gets cumbersome, and introduces unnecessary dependencies. Consider the command pattern on a system with hundreds of commands, for instance.
What I would like to see instead is a method something like
Class Class.classesMatching(ClassPattern ...)
where ClassPatterns could exist for matching classes on a variety of criteria (name, package name, interfaces implemented, superclasses inherited, etc.) This method would return all classes loadable by the appropriate Classloader which match the given ClassPatterns. Plugin architectures become very simple to write and essentially auto-configuring. Obviously there are a lot of issues to resolve with this idea (security, instrumentation) but I think the basic idea has a lot of power.