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

cucumber.runtime.java.picocontainer.PicoFactory Maven / Gradle / Ivy

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

import cucumber.runtime.java.ObjectFactory;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.PicoBuilder;

import java.lang.reflect.Constructor;
import java.util.HashSet;
import java.util.Set;

public class PicoFactory implements ObjectFactory {
    private MutablePicoContainer pico;
    private final Set> classes = new HashSet>();

    public void start() {
        pico = new PicoBuilder().withCaching().build();
        for (Class clazz : classes) {
            pico.addComponent(clazz);
        }
        pico.start();
    }

    public void stop() {
        pico.stop();
        pico.dispose();
    }

    public void addClass(Class clazz) {
        if (classes.add(clazz)) {
            addConstructorDependencies(clazz);
        }
    }

    public  T getInstance(Class type) {
        return pico.getComponent(type);
    }

    private void addConstructorDependencies(Class clazz) {
        for (Constructor constructor : clazz.getConstructors()) {
            for (Class paramClazz : constructor.getParameterTypes()) {
                addClass(paramClazz);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy