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;
}
Aggregations