cucumber.runtime.io.ResourceLoaderClassFinder Maven / Gradle / Ivy
package cucumber.runtime.io;
import cucumber.runtime.ClassFinder;
import java.io.File;
import java.util.Collection;
import java.util.HashSet;
public class ResourceLoaderClassFinder implements ClassFinder {
private final ResourceLoader resourceLoader;
private final ClassLoader classLoader;
public ResourceLoaderClassFinder(ResourceLoader resourceLoader, ClassLoader classLoader) {
this.resourceLoader = resourceLoader;
this.classLoader = classLoader;
}
@Override
public Collection> getDescendants(Class parentType, String packageName) {
Collection> result = new HashSet>();
String packagePath = "classpath:" + packageName.replace('.', '/').replace(File.separatorChar, '/');
for (Resource classResource : resourceLoader.resources(packagePath, ".class")) {
String className = classResource.getClassName(".class");
try {
Class> clazz = loadClass(className);
if (clazz != null && !parentType.equals(clazz) && parentType.isAssignableFrom(clazz)) {
result.add(clazz.asSubclass(parentType));
}
} catch (ClassNotFoundException ignore) {
} catch (NoClassDefFoundError ignore) {
}
}
return result;
}
public Class extends T> loadClass(String className) throws ClassNotFoundException {
return (Class extends T>) classLoader.loadClass(className);
}
}