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

uk.sponte.automation.seleniumpom.dependencies.GuiceDependencyInjector Maven / Gradle / Ivy

There is a newer version: 3.0.14
Show newest version
package uk.sponte.automation.seleniumpom.dependencies;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.Stage;
import uk.sponte.automation.seleniumpom.PageFactory;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Logger;

/**
 * Created by evops on 05/02/2016.
 */
public class GuiceDependencyInjector extends AbstractModule implements DependencyInjector {
    private final static Logger LOG = Logger.getLogger(GuiceDependencyInjector.class.getName());
    private Injector injector;
    private HashMap factories = new HashMap();
    protected ArrayList modules = new ArrayList();

    private PageFactory pageFactory;

    public GuiceDependencyInjector(PageFactory pageFactory, DependencyFactory... factories) {
        this.pageFactory = pageFactory;
        modules.add(this);

        for (DependencyFactory factory : factories) {
            registerFactory(factory);
        }
    }

    private Injector getInjector() {
        if(this.injector != null) return injector;

        injector = Guice.createInjector(Stage.PRODUCTION, modules);
        return injector;
    }

    @Override
    protected void configure() {
        // Register custom factories
        for (Type type : this.factories.keySet()) {
            bind(((Class)type)).toProvider(this.factories.get(type));
        }
    }

    @Override
    public  T get(Class klass) throws InjectionError {
        return getInjector().getInstance(klass);
    }

    public void injectMembers(Object object) {
        getInjector().injectMembers(object);
    }

    @Provides
    @Singleton
    public PageFactory pageFactory() {
        return this.pageFactory;
    }

    /**
     * Registers dependency factories, can have multiple dependencies in one factory
     * @param factory
     * @param 
     */
    public  void registerFactory(DependencyFactory factory) {
        Type[] typeParameters = factory.getClass().getGenericInterfaces();

        for (Type typeParameter : typeParameters) {
            // If it's not a generic type move on
            if(!(typeParameter instanceof ParameterizedType)) continue;
            ParameterizedType parameterizedType = (ParameterizedType) typeParameter;

            // Register first generic type
            this.factories.put((Class) parameterizedType.getActualTypeArguments()[0], factory);
        }
    }
}