Search in sources :

Example 16 with InstanceBinding

use of com.google.inject.spi.InstanceBinding in project roboguice by roboguice.

the class OptionalBinderTest method testBindingKeysFixedOnReturnFromGetElements.

/**
   * Ensure bindings do not rehash their keys once returned from {@link Elements#getElements}.
   */
public void testBindingKeysFixedOnReturnFromGetElements() {
    final List<String> list = Lists.newArrayList();
    Module m = new AbstractModule() {

        @Override
        protected void configure() {
            OptionalBinder<List<String>> b = OptionalBinder.newOptionalBinder(binder(), listOfStrings);
            b.setDefault().toInstance(list);
            list.add("A");
            list.add("B");
        }
    };
    InstanceBinding<?> binding = Iterables.getOnlyElement(Iterables.filter(Elements.getElements(m), InstanceBinding.class));
    Key<?> keyBefore = binding.getKey();
    assertEquals(listOfStrings, keyBefore.getTypeLiteral());
    list.add("C");
    Key<?> keyAfter = binding.getKey();
    assertSame(keyBefore, keyAfter);
}
Also used : InstanceBinding(com.google.inject.spi.InstanceBinding) List(java.util.List) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Example 17 with InstanceBinding

use of com.google.inject.spi.InstanceBinding in project roboguice by roboguice.

the class MultibinderTest method testBindingKeysFixedOnReturnFromGetElements.

/**
   * Ensure bindings do not rehash their keys once returned from {@link Elements#getElements}.
   */
public void testBindingKeysFixedOnReturnFromGetElements() {
    final List<String> list = Lists.newArrayList();
    Module ab = new AbstractModule() {

        @Override
        protected void configure() {
            Multibinder<List<String>> multibinder = Multibinder.newSetBinder(binder(), listOfStrings);
            multibinder.addBinding().toInstance(list);
            list.add("A");
            list.add("B");
        }
    };
    InstanceBinding<?> binding = Iterables.getOnlyElement(Iterables.filter(Elements.getElements(ab), InstanceBinding.class));
    Key<?> keyBefore = binding.getKey();
    assertEquals(listOfStrings, keyBefore.getTypeLiteral());
    list.add("C");
    Key<?> keyAfter = binding.getKey();
    assertSame(keyBefore, keyAfter);
}
Also used : InstanceBinding(com.google.inject.spi.InstanceBinding) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Example 18 with InstanceBinding

use of com.google.inject.spi.InstanceBinding in project cdap by caskdata.

the class SparkProgramRuntimeProvider method createInstance.

/**
   * Create a new instance of the given {@link Type} from the given {@link Injector}. This method
   * is doing Guice injection manually through the @Inject constructor to avoid ClassLoader leakage
   * due to the just-in-time binding map inside the Guice Injector that holds a strong reference to the type,
   * hence the ClassLoader of that type
   *
   * @param injector The Guice Injector for acquiring CDAP system instances
   * @param type the {@link Class} of the instance to create
   * @return a new instance of the given {@link Type}
   */
private <T> T createInstance(Injector injector, Type type, ClassLoader sparkClassLoader) throws Exception {
    Key<?> typeKey = Key.get(type);
    // If there is an explicit instance binding, return the binded instance directly
    Binding<?> binding = injector.getExistingBinding(typeKey);
    if (binding != null && binding instanceof InstanceBinding) {
        return (T) ((InstanceBinding) binding).getInstance();
    }
    @SuppressWarnings("unchecked") Class<T> rawType = (Class<T>) typeKey.getTypeLiteral().getRawType();
    Constructor<T> constructor = findInjectableConstructor(rawType);
    constructor.setAccessible(true);
    // Acquire the instances for each parameter for the constructor
    Type[] paramTypes = constructor.getGenericParameterTypes();
    Object[] args = new Object[paramTypes.length];
    int i = 0;
    for (Type paramType : paramTypes) {
        Key<?> paramTypeKey = Key.get(paramType);
        // instance manually instead of getting through the Guice Injector to avoid ClassLoader leakage
        if (paramTypeKey.getTypeLiteral().getRawType().getClassLoader() == sparkClassLoader) {
            args[i++] = createInstance(injector, paramType, sparkClassLoader);
        } else {
            args[i++] = injector.getInstance(paramTypeKey);
        }
    }
    return constructor.newInstance(args);
}
Also used : InstanceBinding(com.google.inject.spi.InstanceBinding) ProgramType(co.cask.cdap.proto.ProgramType) Type(java.lang.reflect.Type)

Aggregations

InstanceBinding (com.google.inject.spi.InstanceBinding)18 AbstractModule (com.google.inject.AbstractModule)12 Binding (com.google.inject.Binding)6 Injector (com.google.inject.Injector)6 Module (com.google.inject.Module)6 HasDependencies (com.google.inject.spi.HasDependencies)6 Map (java.util.Map)6 ImmutableSet (com.google.common.collect.ImmutableSet)4 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)4 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 List (java.util.List)4 Set (java.util.Set)4 MapBinderBinding (com.google.inject.multibindings.MapBinderBinding)3 Optional (com.google.common.base.Optional)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ProviderMapEntry (com.google.inject.internal.RealMapBinder.ProviderMapEntry)2 ProgramType (co.cask.cdap.proto.ProgramType)1