Search in sources :

Example 51 with Key

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);
}
Also used : Type(java.lang.reflect.Type) Key(com.google.inject.Key) LoadingCache(com.google.common.cache.LoadingCache) Cache(com.google.common.cache.Cache)

Example 52 with Key

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);
}
Also used : HasDependencies(com.google.inject.spi.HasDependencies) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule)

Example 53 with Key

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);
}
Also used : HasDependencies(com.google.inject.spi.HasDependencies) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule)

Example 54 with Key

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;
}
Also used : Binding(com.google.inject.Binding) Binder(com.google.inject.Binder) Type(java.lang.reflect.Type) Injector(com.google.inject.Injector) Module(com.google.inject.Module) Logger(java.util.logging.Logger) Key(com.google.inject.Key) IParser(org.eclipse.xtext.parser.IParser)

Example 55 with Key

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);
        }
    }
}
Also used : Binding(com.google.inject.Binding) Named(com.google.inject.name.Named) Annotation(java.lang.annotation.Annotation) Entry(java.util.Map.Entry) Type(java.lang.reflect.Type) Key(com.google.inject.Key)

Aggregations

Key (com.google.inject.Key)106 AbstractModule (com.google.inject.AbstractModule)55 Injector (com.google.inject.Injector)52 Binding (com.google.inject.Binding)36 Module (com.google.inject.Module)20 Provider (com.google.inject.Provider)18 Element (com.google.inject.spi.Element)16 Map (java.util.Map)16 HasDependencies (com.google.inject.spi.HasDependencies)14 InstanceBinding (com.google.inject.spi.InstanceBinding)13 List (java.util.List)13 TypeLiteral (com.google.inject.TypeLiteral)12 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)10 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)9 PrivateModule (com.google.inject.PrivateModule)8 DefaultBindingTargetVisitor (com.google.inject.spi.DefaultBindingTargetVisitor)8 Dependency (com.google.inject.spi.Dependency)8 ProviderKeyBinding (com.google.inject.spi.ProviderKeyBinding)8 ImmutableList (com.google.common.collect.ImmutableList)7 InjectionPoint (com.google.inject.spi.InjectionPoint)7