use of com.google.inject.PrivateModule in project cdap by caskdata.
the class DistributedProgramRunnableModule method createStreamFactoryModule.
private Module createStreamFactoryModule() {
return new PrivateModule() {
@Override
protected void configure() {
install(new FactoryModuleBuilder().implement(StreamWriter.class, DefaultStreamWriter.class).build(StreamWriterFactory.class));
expose(StreamWriterFactory.class);
}
};
}
use of com.google.inject.PrivateModule in project roboguice by roboguice.
the class ServletScopesTest method testIsRequestScopedPositive.
public void testIsRequestScopedPositive() {
final Key<String> a = Key.get(String.class, named("A"));
final Key<String> b = Key.get(String.class, named("B"));
final Key<String> c = Key.get(String.class, named("C"));
final Key<String> d = Key.get(String.class, named("D"));
final Key<Object> e = Key.get(Object.class, named("E"));
final Key<String> f = Key.get(String.class, named("F"));
final Key<String> g = Key.get(String.class, named("G"));
Module requestScopedBindings = new AbstractModule() {
@Override
protected void configure() {
bind(a).to(b);
bind(b).to(c);
bind(c).toProvider(Providers.of("c")).in(ServletScopes.REQUEST);
bind(d).toProvider(Providers.of("d")).in(RequestScoped.class);
bind(e).to(AnnotatedRequestScopedClass.class);
install(new PrivateModule() {
@Override
protected void configure() {
bind(f).toProvider(Providers.of("f")).in(RequestScoped.class);
expose(f);
}
});
}
@Provides
@Named("G")
@RequestScoped
String provideG() {
return "g";
}
};
// we know the module contains only bindings
@SuppressWarnings("unchecked") List<Element> moduleBindings = Elements.getElements(requestScopedBindings);
ImmutableMap<Key<?>, Binding<?>> map = indexBindings(moduleBindings);
// linked bindings are not followed by modules
assertFalse(ServletScopes.isRequestScoped(map.get(a)));
assertFalse(ServletScopes.isRequestScoped(map.get(b)));
assertTrue(ServletScopes.isRequestScoped(map.get(c)));
assertTrue(ServletScopes.isRequestScoped(map.get(d)));
// annotated classes are not followed by modules
assertFalse(ServletScopes.isRequestScoped(map.get(e)));
assertTrue(ServletScopes.isRequestScoped(map.get(f)));
assertTrue(ServletScopes.isRequestScoped(map.get(g)));
Injector injector = Guice.createInjector(requestScopedBindings, new ServletModule());
assertTrue(ServletScopes.isRequestScoped(injector.getBinding(a)));
assertTrue(ServletScopes.isRequestScoped(injector.getBinding(b)));
assertTrue(ServletScopes.isRequestScoped(injector.getBinding(c)));
assertTrue(ServletScopes.isRequestScoped(injector.getBinding(d)));
assertTrue(ServletScopes.isRequestScoped(injector.getBinding(e)));
assertTrue(ServletScopes.isRequestScoped(injector.getBinding(f)));
assertTrue(ServletScopes.isRequestScoped(injector.getBinding(g)));
}
use of com.google.inject.PrivateModule in project roboguice by roboguice.
the class OverrideModuleTest method testOverridePrivateModuleOverPrivateModule.
public void testOverridePrivateModuleOverPrivateModule() {
Module exposes5and6 = new AbstractModule() {
@Override
protected void configure() {
install(new PrivateModule() {
@Override
protected void configure() {
bind(Integer.class).toInstance(5);
expose(Integer.class);
bind(Character.class).toInstance('E');
}
});
install(new PrivateModule() {
@Override
protected void configure() {
bind(Long.class).toInstance(6L);
expose(Long.class);
bind(Character.class).toInstance('F');
}
});
}
};
AbstractModule exposes15 = new AbstractModule() {
@Override
protected void configure() {
install(new PrivateModule() {
@Override
protected void configure() {
bind(Integer.class).toInstance(15);
expose(Integer.class);
bind(Character.class).toInstance('G');
}
});
install(new PrivateModule() {
@Override
protected void configure() {
bind(Character.class).toInstance('H');
}
});
}
};
// override forwards
Injector injector = Guice.createInjector(Modules.override(exposes5and6).with(exposes15));
assertEquals(15, injector.getInstance(Integer.class).intValue());
assertEquals(6L, injector.getInstance(Long.class).longValue());
// and in reverse order
Injector reverse = Guice.createInjector(Modules.override(exposes15).with(exposes5and6));
assertEquals(5, reverse.getInstance(Integer.class).intValue());
assertEquals(6L, reverse.getInstance(Long.class).longValue());
}
use of com.google.inject.PrivateModule in project roboguice by roboguice.
the class OverrideModuleTest method testOverrideDeepExpose.
public void testOverrideDeepExpose() {
final AtomicReference<Provider<Character>> charAProvider = new AtomicReference<Provider<Character>>();
Module exposes5 = new PrivateModule() {
@Override
protected void configure() {
install(new PrivateModule() {
@Override
protected void configure() {
bind(Integer.class).toInstance(5);
expose(Integer.class);
charAProvider.set(getProvider(Character.class));
bind(Character.class).toInstance('A');
}
});
expose(Integer.class);
}
};
Injector injector = Guice.createInjector(Modules.override(exposes5).with(EMPTY_MODULE));
assertEquals(5, injector.getInstance(Integer.class).intValue());
assertEquals('A', charAProvider.getAndSet(null).get().charValue());
injector = Guice.createInjector(Modules.override(EMPTY_MODULE).with(exposes5));
assertEquals(5, injector.getInstance(Integer.class).intValue());
assertEquals('A', charAProvider.getAndSet(null).get().charValue());
final AtomicReference<Provider<Character>> charBProvider = new AtomicReference<Provider<Character>>();
Module binds15 = new AbstractModule() {
@Override
protected void configure() {
bind(Integer.class).toInstance(15);
install(new PrivateModule() {
@Override
protected void configure() {
charBProvider.set(getProvider(Character.class));
bind(Character.class).toInstance('B');
}
});
}
};
injector = Guice.createInjector(Modules.override(binds15).with(exposes5));
assertEquals(5, injector.getInstance(Integer.class).intValue());
assertEquals('A', charAProvider.getAndSet(null).get().charValue());
assertEquals('B', charBProvider.getAndSet(null).get().charValue());
injector = Guice.createInjector(Modules.override(exposes5).with(binds15));
assertEquals(15, injector.getInstance(Integer.class).intValue());
assertEquals('A', charAProvider.getAndSet(null).get().charValue());
assertEquals('B', charBProvider.getAndSet(null).get().charValue());
}
use of com.google.inject.PrivateModule in project guice by google.
the class ServletScopesTest method testIsRequestScopedPositive.
public void testIsRequestScopedPositive() {
final Key<String> a = Key.get(String.class, named("A"));
final Key<String> b = Key.get(String.class, named("B"));
final Key<String> c = Key.get(String.class, named("C"));
final Key<String> d = Key.get(String.class, named("D"));
final Key<Object> e = Key.get(Object.class, named("E"));
final Key<String> f = Key.get(String.class, named("F"));
final Key<String> g = Key.get(String.class, named("G"));
Module requestScopedBindings = new AbstractModule() {
@Override
protected void configure() {
bind(a).to(b);
bind(b).to(c);
bind(c).toProvider(Providers.of("c")).in(ServletScopes.REQUEST);
bind(d).toProvider(Providers.of("d")).in(RequestScoped.class);
bind(e).to(AnnotatedRequestScopedClass.class);
install(new PrivateModule() {
@Override
protected void configure() {
bind(f).toProvider(Providers.of("f")).in(RequestScoped.class);
expose(f);
}
});
}
@Provides
@Named("G")
@RequestScoped
String provideG() {
return "g";
}
};
// we know the module contains only bindings
@SuppressWarnings("unchecked") List<Element> moduleBindings = Elements.getElements(requestScopedBindings);
ImmutableMap<Key<?>, Binding<?>> map = indexBindings(moduleBindings);
// linked bindings are not followed by modules
assertFalse(ServletScopes.isRequestScoped(map.get(a)));
assertFalse(ServletScopes.isRequestScoped(map.get(b)));
assertTrue(ServletScopes.isRequestScoped(map.get(c)));
assertTrue(ServletScopes.isRequestScoped(map.get(d)));
// annotated classes are not followed by modules
assertFalse(ServletScopes.isRequestScoped(map.get(e)));
assertTrue(ServletScopes.isRequestScoped(map.get(f)));
assertTrue(ServletScopes.isRequestScoped(map.get(g)));
Injector injector = Guice.createInjector(requestScopedBindings, new ServletModule());
assertTrue(ServletScopes.isRequestScoped(injector.getBinding(a)));
assertTrue(ServletScopes.isRequestScoped(injector.getBinding(b)));
assertTrue(ServletScopes.isRequestScoped(injector.getBinding(c)));
assertTrue(ServletScopes.isRequestScoped(injector.getBinding(d)));
assertTrue(ServletScopes.isRequestScoped(injector.getBinding(e)));
assertTrue(ServletScopes.isRequestScoped(injector.getBinding(f)));
assertTrue(ServletScopes.isRequestScoped(injector.getBinding(g)));
}
Aggregations