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);
}
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);
}
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);
}
Aggregations