package cucumber.runtime.java.cditest;

import cucumber.runtime.java.ObjectFactory;
import de.hilling.junit.cdi.lifecycle.LifecycleNotifier;
import de.hilling.junit.cdi.scope.EventType;
import org.apache.deltaspike.cdise.api.CdiContainer;
import org.apache.deltaspike.cdise.api.CdiContainerLoader;
import org.apache.deltaspike.cdise.api.ContextControl;
import org.apache.deltaspike.core.api.provider.BeanProvider;
import org.junit.runner.Description;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;


public class CdiTestObjectFactory implements ObjectFactory {
    private static final Logger LOG = Logger.getLogger(CdiTestObjectFactory.class.getCanonicalName());

    private ContextControl contextControl;
    private LifecycleNotifier notifier;

    {
        contextControl = BeanProvider.getContextualReference(ContextControl.class);
        notifier = BeanProvider.getContextualReference(
                LifecycleNotifier.class, false);
    }

    static {
        CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
        cdiContainer.boot();
    }

    private Map<Class, Object> definitions = new HashMap<>();

    @Override
    public void start() {
        LOG.info("starting");
        contextControl.startContexts();
        notifier.notify(EventType.STARTING, Description.createSuiteDescription("cucumber"));
    }

    @Override
    public void stop() {
        contextControl.stopContexts();
        notifier.notify(EventType.FINISHING, Description.createSuiteDescription("cucumber"));
        LOG.info("stopped");
    }

    @Override
    public void addClass(Class<?> clazz) {
        LOG.info("adding " + clazz);
        definitions.put(clazz, BeanProvider.getContextualReference(clazz, false));
    }

    @Override
    public <T> T getInstance(Class<T> clazz) {
        return (T) definitions.get(clazz);
    }
}

@@ -1632,32 +1632,150 @@
 ass%3C?%3E clazz) %7B%0A
+%0A    %7D%0A%0A    @Override%0A    public %3CT%3E T getInstance(Class%3CT%3E clazz) %7B%0A        if (definitions.get(clazz) == null)%7B%0A    
         LOG.info
@@ -1791,24 +1791,28 @@
 %22 + clazz);%0A
+    
         defi
@@ -1890,71 +1890,13 @@
     
-%7D%0A%0A
     
-@Override%0A    public %3CT%3E T getInstance(Class%3CT%3E clazz) %7B
+%7D
 %0A   

move addClass implementation to getInstance