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

com.github.mlk.guice.MagicalLegacyProvider Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.github.mlk.guice;

import com.google.common.collect.ImmutableMap;
import com.google.inject.*;
import com.google.inject.spi.Dependency;
import com.google.inject.spi.HasDependencies;

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

public class MagicalLegacyProvider implements Provider, HasDependencies {
    private Injector injector;
    private final Key[] keys;
    private final Constructor ctor;
    private static final Map, Class> WAPPER_TO_PRIMATIVE
            = new ImmutableMap.Builder, Class>()
            .put(Boolean.class, boolean.class)
            .put(Byte.class, byte.class)
            .put(Short.class, short.class)
            .put(Integer.class, int.class)
            .put(Long.class, long.class)
            .put(Float.class, float.class)
            .put(Double.class, double.class)
            .put(Character.class, char.class)
            .put(Void.class, void.class)
            .build();


    public MagicalLegacyProvider(Class clazz, Key... keys) {
        this.keys = keys;

        Class[] ctorTypes = new Class[keys.length];
        for (int index = 0; index < ctorTypes.length; index++) {
            ctorTypes[index] = keys[index].getTypeLiteral().getRawType();

        }
        Constructor ctor = findConstructor(clazz, ctorTypes);
        if (ctor != null) {
            this.ctor =  ctor;
        } else {
            throw new ProvisionException("Unable to find a constructor matching the given types - " + Arrays.toString(keys));
        }
    }

    private Constructor findConstructor(Class clazz, Class[] ctorTypes) {
        Constructor ctor = null;
        for (Constructor constructor : clazz.getConstructors()) {

            if (isCompatable(constructor, ctorTypes)) {
                ctor = constructor;
                break;
            }
        }

        return (Constructor) ctor;
    }

    private boolean isCompatable(Constructor constructor, Class[] ctorTypes) {
        if(constructor.getParameterCount() != ctorTypes.length) {
            return false;
        }
        boolean found = true;
        for(int i = 0; i> getDependencies() {
        Set> dependencies = new HashSet<>();
        for(Key key : keys) {
            dependencies.add(Dependency.get(key));
        }
        return dependencies;
    }

    @Override
    public T get() {
        Object[] ctorParams = new Object[keys.length];
        for(int index = 0; index < ctorParams.length; index++) {
            ctorParams[index] = injector.getInstance(keys[index]);
        }
	try {
		return ctor.newInstance(ctorParams);
	} catch(InstantiationException | IllegalAccessException | java.lang.reflect.InvocationTargetException ie) {
		throw new RuntimeException(ie);
	}
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy