use of com.google.inject.Key in project gerrit by GerritCodeReview.
the class CacheModule method cache.
/**
* Declare a named in-memory cache.
*
* @param <K> type of key used to lookup entries.
* @param <V> type of value stored by the cache.
* @return binding to describe the cache.
*/
protected <K, V> CacheBinding<K, V> cache(String name, TypeLiteral<K> keyType, TypeLiteral<V> valType) {
Type type = Types.newParameterizedType(Cache.class, keyType.getType(), valType.getType());
@SuppressWarnings("unchecked") Key<Cache<K, V>> key = (Key<Cache<K, V>>) Key.get(type, Names.named(name));
CacheProvider<K, V> m = new CacheProvider<>(this, name, keyType, valType);
bind(key).toProvider(m).asEagerSingleton();
bind(ANY_CACHE).annotatedWith(Exports.named(name)).to(key);
return m.maximumWeight(1024);
}
use of com.google.inject.Key in project roboguice by roboguice.
the class CheckedProviderTest method testDependencies_Bind.
public void testDependencies_Bind() {
bindInjector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(String.class).toInstance("Foo");
bind(Integer.class).toInstance(5);
bind(Double.class).toInstance(5d);
bind(Long.class).toInstance(5L);
ThrowingProviderBinder.create(binder()).bind(RemoteProvider.class, Foo.class).to(DependentRemoteProvider.class);
}
});
HasDependencies hasDependencies = (HasDependencies) bindInjector.getBinding(Key.get(remoteProviderOfFoo));
hasDependencies = (HasDependencies) bindInjector.getBinding(Iterables.getOnlyElement(hasDependencies.getDependencies()).getKey());
// Make sure that that is dependent on DependentRemoteProvider.
assertEquals(Dependency.get(Key.get(DependentRemoteProvider.class)), Iterables.getOnlyElement(hasDependencies.getDependencies()));
// And make sure DependentRemoteProvider has the proper dependencies.
hasDependencies = (HasDependencies) bindInjector.getBinding(DependentRemoteProvider.class);
Set<Key<?>> dependencyKeys = ImmutableSet.copyOf(Iterables.transform(hasDependencies.getDependencies(), DEPENDENCY_TO_KEY));
assertEquals(ImmutableSet.<Key<?>>of(Key.get(String.class), Key.get(Integer.class), Key.get(Long.class), Key.get(Double.class)), dependencyKeys);
}
use of com.google.inject.Key in project roboguice by roboguice.
the class CheckedProviderTest method testDependencies_Provides.
public void testDependencies_Provides() {
providesInjector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(String.class).toInstance("Foo");
bind(Integer.class).toInstance(5);
bind(Double.class).toInstance(5d);
bind(Long.class).toInstance(5L);
install(ThrowingProviderBinder.forModule(this));
}
@SuppressWarnings("unused")
@CheckedProvides(RemoteProvider.class)
Foo foo(String s, Integer i, Double d, Long l) {
return null;
}
});
HasDependencies hasDependencies = (HasDependencies) providesInjector.getBinding(Key.get(remoteProviderOfFoo));
// RemoteProvider<String> is dependent on the provider method..
hasDependencies = (HasDependencies) providesInjector.getBinding(Iterables.getOnlyElement(hasDependencies.getDependencies()).getKey());
// And the provider method has our real dependencies..
hasDependencies = (HasDependencies) providesInjector.getBinding(Iterables.getOnlyElement(hasDependencies.getDependencies()).getKey());
Set<Key<?>> dependencyKeys = ImmutableSet.copyOf(Iterables.transform(hasDependencies.getDependencies(), DEPENDENCY_TO_KEY));
assertEquals(ImmutableSet.<Key<?>>of(Key.get(String.class), Key.get(Integer.class), Key.get(Long.class), Key.get(Double.class)), dependencyKeys);
}
use of com.google.inject.Key in project xtext-core by eclipse.
the class WrappingInjectorProvider method createInjector.
protected Injector createInjector() {
Injector delegateInjector = delegate.getInjector();
final Map<Key<?>, Binding<?>> bindings = delegateInjector.getBindings();
Injector injector = Guice.createInjector(Modules.override(new Module() {
@Override
public void configure(Binder binder) {
for (Binding<?> binding : bindings.values()) {
Type typeLiteral = binding.getKey().getTypeLiteral().getType();
if (!Injector.class.equals(typeLiteral) && !Logger.class.equals(typeLiteral)) {
binding.applyTo(binder);
}
}
}
}).with(new Module() {
@Override
public void configure(Binder binder) {
binder.bind(IParser.class).toInstance(new TestDataProvider());
}
}));
return injector;
}
use of com.google.inject.Key in project camel by apache.
the class JndiBindings method bindInjectorAndBindings.
/**
* Binds the given injector and its binding providers to the given JNDI
* context using <a
* href="http://code.google.com/p/camel-extra/wiki/GuiceJndi">this mapping
* mechanism</a>.
* <p/>
* This will expose all of the bindings providers to JNDI along with any
* bindings which are annotated with {@link JndiBind} or {@link @Named} to
* the given JNDI context.
*
* @param context
* the context to export objects to
* @param injector
* the injector used to find the bindings
*/
public static void bindInjectorAndBindings(Context context, Injector injector, Properties jndiNames) throws NamingException {
// lets find all the exported bindings
Set<Entry<Key<?>, Binding<?>>> entries = injector.getBindings().entrySet();
for (Entry<Key<?>, Binding<?>> entry : entries) {
Key<?> key = entry.getKey();
Binding<?> binding = entry.getValue();
Annotation annotation = key.getAnnotation();
Type type = key.getTypeLiteral().getType();
JndiBind jndiBind = null;
if (type instanceof Class) {
Class<?> aClass = (Class<?>) type;
jndiBind = aClass.getAnnotation(JndiBind.class);
}
if (annotation instanceof JndiBind) {
jndiBind = (JndiBind) annotation;
}
String jndiName = null;
if (jndiBind != null) {
jndiName = jndiBind.value();
}
if (jndiName == null) {
if (annotation instanceof Named) {
Named named = (Named) annotation;
String name = named.value();
jndiName = type.toString() + "/" + name;
} else if (type instanceof Class<?>) {
Class<?> aClass = (Class<?>) type;
if (annotation == null) {
jndiName = aClass.getName();
} else {
jndiName = aClass.getName() + annotation;
}
}
}
if (jndiName != null) {
Object value = binding.getProvider();
if (value != null) {
context.bind(jndiName, value);
}
}
}
for (Entry<Object, Object> entry : jndiNames.entrySet()) {
String jndiName = entry.getKey().toString();
String expression = entry.getValue().toString();
Provider<?> provider = getProviderForExpression(injector, expression);
if (provider != null) {
context.bind(jndiName, provider);
}
}
}
Aggregations