As can be seen on pages like
Classpath Scanning: Hidden Spring Gems « It works on my machine! the way to class path scanning so far has been to use the Spring framework's feature for oneself.
There are some drawbacks to that approach however. For one thing you need to depend on the Spring jars, just for class path scanning. If you don’t need them for anything else this is quite cumbersome. For another the API you need to use this way is not really straight-forward.
But now there is a better way for class path scanning. Making this non-trivial issue absolutely easy for everybody. It’s called eXtcos, which is short for Extensible Component Scanner. This little library is inspired by the Spring feature but is a small library solely focused on the task of class path scanning.
It provides an easy to use query language for requesting the classes. This is a refreshing change to the boiler plate code you need to use the Spring classes for your own needs.
Imagine you want to find all the classes annotated with a @PreferencePage annotation in the “sample” package. Your code with eXtcos is just
Code:
ClasspathScanner scanner = new ClasspathScanner();
Set<Class> classes = scanner.getClasses(new ClassQuery() {
protected void query() {
select().
from(“sample”).
returning(allAnnotatedWith(PreferencePage.class));
}
});
That’s all.
In case you want to get several different kinds of classes that’s possible with just one query as well. Consider the following listing looking up all classes annotated with the @PreferencePage annotation and all classes extending the JComponent class in two different sets.
Code:
ClasspathScanner scanner = new ClasspathScanner();
final Set<Class> jComponentStore = new ArraySet<Class>();
Set<Class> classes = scanner.getClasses(new ClassQuery() {
protected void query() {
select().
from(“sample”).
andStore(thoseExtending(JComponent.class).into(jComponentStore)).
returning(allAnnotatedWith(PreferencePage.class));
}
});
Like the look and feel of it? Download it now and test drive it yourself. Just go to
Extensible Component Scanner | Get Extensible Component Scanner at SourceForge.net. After testing, please leave a comment and spread the word. It’s completely free software.