Class discovery in Java

In Smalltalk (a.k.a OOP’s Latin), it takes a single call to get all the classes that inherit from class X:

X subclasses

It sounds like a bit of an exotic task, but actually it’s a single-liner for a plug-ins mechanism. Just load a class that extends “MyPlugIn” into the Smalltalk image and Voila! Your plug-in is loaded.

I thought that this mechanism is missing from Java (a.k.a Smalltalk-wannabe), until I found out that the good fellows from the Apache foundation did that too, and named it commons-discovery. The equivalent Java command is a bit more complicated:

DiscoverClass discoverClass = new DiscoverClass();
Class x = (Class)discoverClass.find(X.class);

But it’s still a neat trick. Kudos!

Posted Tuesday, September 2nd, 2008 under Programming.

Tags: , ,

Leave a Reply

Security Code: