Search in sources :

Example 1 with TooManyInstancesException

use of cucumber.runtime.TooManyInstancesException in project cucumber-jvm by cucumber.

the class ObjectFactoryLoader method loadObjectFactory.

/**
     * Loads an instance of {@link ObjectFactory}. The class name can be explicit, or it can be null.
     * When it's null, the implementation is searched for in the <pre>cucumber.runtime</pre> packahe.
     *
     * @param classFinder where to load classes from
     * @param objectFactoryClassName specific class name of {@link ObjectFactory} implementation. May be null.
     * @return an instance of {@link ObjectFactory}
     */
public static ObjectFactory loadObjectFactory(ClassFinder classFinder, String objectFactoryClassName) {
    ObjectFactory objectFactory;
    try {
        Reflections reflections = new Reflections(classFinder);
        if (objectFactoryClassName != null) {
            Class<ObjectFactory> objectFactoryClass = (Class<ObjectFactory>) classFinder.loadClass(objectFactoryClassName);
            objectFactory = reflections.newInstance(new Class[0], new Object[0], objectFactoryClass);
        } else {
            objectFactory = reflections.instantiateExactlyOneSubclass(ObjectFactory.class, "cucumber.runtime", new Class[0], new Object[0]);
        }
    } catch (TooManyInstancesException e) {
        System.out.println(e.getMessage());
        System.out.println(getMultipleObjectFactoryLogMessage());
        objectFactory = new DefaultJavaObjectFactory();
    } catch (NoInstancesException e) {
        objectFactory = new DefaultJavaObjectFactory();
    } catch (ClassNotFoundException e) {
        throw new CucumberException("Couldn't instantiate custom ObjectFactory", e);
    }
    return objectFactory;
}
Also used : TooManyInstancesException(cucumber.runtime.TooManyInstancesException) ObjectFactory(cucumber.api.java.ObjectFactory) CucumberException(cucumber.runtime.CucumberException) NoInstancesException(cucumber.runtime.NoInstancesException) Reflections(cucumber.runtime.Reflections)

Aggregations

ObjectFactory (cucumber.api.java.ObjectFactory)1 CucumberException (cucumber.runtime.CucumberException)1 NoInstancesException (cucumber.runtime.NoInstancesException)1 Reflections (cucumber.runtime.Reflections)1 TooManyInstancesException (cucumber.runtime.TooManyInstancesException)1