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

com.netflix.fabricator.guice.mapping.CompositeInterfaceBinding Maven / Gradle / Ivy

package com.netflix.fabricator.guice.mapping;

import com.google.inject.Binding;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import com.google.inject.util.Types;
import com.netflix.fabricator.ConfigurationNode;
import com.netflix.fabricator.component.ComponentFactory;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * Created by hyuan on 1/17/14.
 */
public class CompositeInterfaceBinding implements BindingReslove {
    @Override
    public boolean execute(String name, Object obj, ConfigurationNode node, Class argType, Injector injector, Method method) throws Exception {
        if (argType.isInterface()) {
            TypeLiteral>> mapType =
                    (TypeLiteral>>) TypeLiteral.get(
                            Types.mapOf(String.class,
                                    Types.newParameterizedType(
                                            ComponentFactory.class,
                                            argType)));
            
            Key>> mapKey = Key.get(mapType);

            Binding>> binding = injector.getExistingBinding(mapKey);
            
            if (binding != null) {
                if (node.getType() != null) {
                    Map> map = binding
                            .getProvider().get();
                    ComponentFactory factory = map
                            .get(node.getType());
                    if (factory != null) {
                        Object subObject = factory
                                .create(node);
                        method.invoke(obj, subObject);
                        return true;
                    }
                }
            }
        }
        return false;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy