Spring HERE
Spring again HERE
In the end I wrote the following:
public class FileSystemBasedClassFinder {
public List<Class<?>> findClasses(File dir, String packageName) {
String path = FdbAnnotationHelper.LocalUtil.makePathFromClassName(packageName);
File pDir = new File(dir,path);
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".class");
}
};
File[] files = pDir.listFiles(filter);
List<Class<?>> classList = new ArrayList<Class<?>>();
for (File file : files) {
String f = file.getAbsolutePath().substring(dir.getAbsolutePath().length()+1);
String className = LocalUtil.makeClassNameFromPath(f);
try {
Class<?> c =Class.forName(className);
classList.add(c);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return classList;
}
To use it:
File dir = new File("target/classes"); // file system directory
List<Class<?>> classList = new FileSystemBasedClassFinder().findClasses(dir,"com.jgk.somepackage");
LocalUtil:
static class LocalUtil {
static String makePathFromClassName(String c) {
String pathName = c.replaceAll("\.", "/");
//System.out.println(c+" vs. "+pathName);
return pathName;
}
static String makePathFromClass(Class<?> clazz) {
String c = clazz.getName();
return makePathFromClassName(c);
}
public static String makeClassNameFromPath(String path) {
if(path.endsWith(".class")) {
path = path.substring(0,path.lastIndexOf(".class"));
}
path=path.replaceAll("\/", ".");
return path;
}
}