String pkg = MyBaseClass.class.getPackage().getName();
String pkgPath = pkg.replace('.', '/');
List<Class<?>> classes = new ArrayList<Class<?>>();
PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
Resource[] packageClasses = resourceResolver.getResources("classpath*:" + pkgPath + "/**/*.class");
for( Resource res : packageClasses ){
classes.add(Class.forName(pkg + '.' + FilenameUtils.getBaseName(res.getFilename())));
}
for( Class<?> c : classes ){
MyAnnotation myAnnotation = c.getAnnotation(MyAnnotation.class);
if( myAnnotation != null ){
// this class was marked with @MyAnnotation; handle accordingly
}
}
I could devote a whole post to Spring's resource loading capabilities; they are the nicest I've used. The above snippet utilizes Spring's
PathMatchingResourcePatternResolver
, which is able to list resources which match a given ant-style path description. The secret is "classpath*:some/path"
; note the asterisk before the colon. This searches *all* accessible jars on the classpath, instead of stopping in the first one in which we find a match. One gotcha here is that you can't do something like "classpath*:*/service/*Service.class"
; it will choke if you attempt a leading wildcard search.
No comments:
Post a Comment