Scanning classes with VFS
grossetieg Sep 9, 2010 7:17 AMHi,
What is the right way to scan classes in a package with VFS ?
I've tried something like :
List<Class<? extends Enum<?>>> result = new ArrayList<Class<? extends Enum<?>>>();
ClassLoader cld = Thread.currentThread().getContextClassLoader();
if (null != cld) {
final String pkgPath = packageName.replace('.', '/');
URL resource = cld.getResource(pkgPath);
VirtualFile file = VFS.getChild(resource);
List<VirtualFile> children = file.getChildrenRecursively();
for (VirtualFile child : children) {
if (child.isFile()) {
Class<?> clazz = Class.forName(packageName.concat(".").concat(child.getName().split("\\.")[0]));
if (clazz.isEnum()) {
result.add((Class<Enum<?>>) clazz);
}
}
}
}
But the package isn't a directory and getChildrenRecursively() return an empty list. We, previously in JBoss 4.2.3, used :
List<Class<? extends Enum<?>>> result = new ArrayList<Class<? extends Enum<?>>>();
ClassLoader cld = Thread.currentThread().getContextClassLoader();
if (null != cld) {
String pkgPath = packageName.replace('.', '/');
URL resource = cld.getResource(pkgPath);
if (null != resource) {
if (resource.getProtocol().equals("jar")) {
int index = resource.getPath().indexOf('!');
// on extrait le chemin du jar
String fullJarPathWithName = resource.getPath().substring(5, index);
List<Class<? extends Enum<?>>> classes = getEnumClassesFromJar(fullJarPathWithName, packageName, pkgPath);
if (null != classes) {
result.addAll(classes);
}
}
}
}
But with VFS the path doesn't contain ! anymore.
We also tried with a visitor pattern but without success. I've read the article at dzone but some classes doesn't exist anymore (SuffixVisitor in jboss-vfs.3.0.0.CR5.jar).
Thanks in advance.
Guillaume.