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

cucumber.runtime.Reflections Maven / Gradle / Ivy

There is a newer version: 1.2.6
Show newest version
package cucumber.runtime;

import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;

public class Reflections {
    private final ClassFinder classFinder;

    public Reflections(ClassFinder classFinder) {
        this.classFinder = classFinder;
    }

    public  T instantiateExactlyOneSubclass(Class parentType, String packageName, Class[] constructorParams, Object[] constructorArgs) {
        Collection instances = instantiateSubclasses(parentType, packageName, constructorParams, constructorArgs);
        if (instances.size() == 1) {
            return instances.iterator().next();
        } else if (instances.size() == 0) {
            throw new NoInstancesException(parentType);
        } else {
            throw new TooManyInstancesException(instances);
        }
    }

    public  Collection instantiateSubclasses(Class parentType, String packageName, Class[] constructorParams, Object[] constructorArgs) {
        Collection result = new HashSet();
        for (Class clazz : classFinder.getDescendants(parentType, packageName)) {
            if (Utils.isInstantiable(clazz) && hasConstructor(clazz, constructorParams)) {
                result.add(newInstance(constructorParams, constructorArgs, clazz));
            }
        }
        return result;
    }

    public  T newInstance(Class[] constructorParams, Object[] constructorArgs, Class clazz) {
        Constructor constructor = null;
        try {
            constructor = clazz.getConstructor(constructorParams);
            try {
                return constructor.newInstance(constructorArgs);
            } catch (Exception e) {
                String message = String.format("Failed to instantiate %s with %s", constructor.toGenericString(), Arrays.asList(constructorArgs));
                throw new CucumberException(message, e);
            }
        } catch (NoSuchMethodException e) {
            throw new CucumberException(e);
        }
    }

    private boolean hasConstructor(Class clazz, Class[] paramTypes) {
        try {
            clazz.getConstructor(paramTypes);
            return true;
        } catch (NoSuchMethodException e) {
            return false;
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy