All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cucumber.runtime.io.ResourceLoaderClassFinder Maven / Gradle / Ivy

There is a newer version: 1.2.6
Show newest version
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 loadClass(String className) throws ClassNotFoundException {
        return (Class) classLoader.loadClass(className);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy