Search in sources :

Example 1 with Key

use of com.google.inject.Key in project druid by druid-io.

the class JsonConfigProvider method bindInstance.

@SuppressWarnings("unchecked")
public static <T> void bindInstance(Binder binder, Key<T> bindKey, T instance) {
    binder.bind(bindKey).toInstance(instance);
    final ParameterizedType supType = Types.newParameterizedType(Supplier.class, bindKey.getTypeLiteral().getType());
    final Key supplierKey;
    if (bindKey.getAnnotationType() != null) {
        supplierKey = Key.get(supType, bindKey.getAnnotationType());
    } else if (bindKey.getAnnotation() != null) {
        supplierKey = Key.get(supType, bindKey.getAnnotation());
    } else {
        supplierKey = Key.get(supType);
    }
    binder.bind(supplierKey).toInstance(Suppliers.<T>ofInstance(instance));
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Key(com.google.inject.Key)

Example 2 with Key

use of com.google.inject.Key in project neo4j-mobile-android by neo4j-contrib.

the class DBInspectorApplication method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    Module defaultModule = RoboGuice.newDefaultRoboModule(this);
    Module dbInspectorModule = new DBInspectorModule();
    Module combinedModule = Modules.combine(defaultModule, dbInspectorModule);
    Injector injector = RoboGuice.setBaseApplicationInjector(this, RoboGuice.DEFAULT_STAGE, combinedModule);
    Map<Key<?>, Binding<?>> bindings = injector.getAllBindings();
    for (Key<?> key : bindings.keySet()) {
        Binding<?> value = bindings.get(key);
        Ln.d("binding key '" + key + "', value '" + value + "'");
    }
    Ln.i("Application initialized.");
}
Also used : Binding(com.google.inject.Binding) Injector(com.google.inject.Injector) Module(com.google.inject.Module) Key(com.google.inject.Key)

Example 3 with Key

use of com.google.inject.Key in project druid by druid-io.

the class ResourceFilterTestHelper method getRequestPaths.

// Feeds in an array of [ PathName, MethodName, ResourceFilter , Injector]
public static Collection<Object[]> getRequestPaths(final Class clazz, final Iterable<Class<?>> mockableInjections, final Iterable<Key<?>> mockableKeys, final Iterable<?> injectedObjs) {
    final Injector injector = Guice.createInjector(new Module() {

        @Override
        public void configure(Binder binder) {
            for (Class clazz : mockableInjections) {
                binder.bind(clazz).toInstance(EasyMock.createNiceMock(clazz));
            }
            for (Object obj : injectedObjs) {
                binder.bind((Class) obj.getClass()).toInstance(obj);
            }
            for (Key<?> key : mockableKeys) {
                binder.bind((Key<Object>) key).toInstance(EasyMock.createNiceMock(key.getTypeLiteral().getRawType()));
            }
            binder.bind(AuthConfig.class).toInstance(new AuthConfig(true));
        }
    });
    //Ignore the first "/"
    final String basepath = ((Path) clazz.getAnnotation(Path.class)).value().substring(1);
    final List<Class<? extends ResourceFilter>> baseResourceFilters = clazz.getAnnotation(ResourceFilters.class) == null ? Collections.<Class<? extends ResourceFilter>>emptyList() : ImmutableList.copyOf(((ResourceFilters) clazz.getAnnotation(ResourceFilters.class)).value());
    return ImmutableList.copyOf(Iterables.concat(// Step 3 - Merge all the Objects arrays for each endpoints
    Iterables.transform(//  - Resource Filter instance for the endpoint
    Iterables.filter(// ResourceFilters applied to them
    ImmutableList.copyOf(clazz.getDeclaredMethods()), new Predicate<Method>() {

        @Override
        public boolean apply(Method input) {
            return input.getAnnotation(GET.class) != null || input.getAnnotation(POST.class) != null || input.getAnnotation(DELETE.class) != null && (input.getAnnotation(ResourceFilters.class) != null || !baseResourceFilters.isEmpty());
        }
    }), new Function<Method, Collection<Object[]>>() {

        @Override
        public Collection<Object[]> apply(final Method method) {
            final List<Class<? extends ResourceFilter>> resourceFilters = method.getAnnotation(ResourceFilters.class) == null ? baseResourceFilters : ImmutableList.copyOf(method.getAnnotation(ResourceFilters.class).value());
            return Collections2.transform(resourceFilters, new Function<Class<? extends ResourceFilter>, Object[]>() {

                @Override
                public Object[] apply(Class<? extends ResourceFilter> input) {
                    if (method.getAnnotation(Path.class) != null) {
                        return new Object[] { String.format("%s%s", basepath, method.getAnnotation(Path.class).value()), input.getAnnotation(GET.class) == null ? (method.getAnnotation(DELETE.class) == null ? "POST" : "DELETE") : "GET", injector.getInstance(input), injector };
                    } else {
                        return new Object[] { basepath, input.getAnnotation(GET.class) == null ? (method.getAnnotation(DELETE.class) == null ? "POST" : "DELETE") : "GET", injector.getInstance(input), injector };
                    }
                }
            });
        }
    })));
}
Also used : Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) AuthConfig(io.druid.server.security.AuthConfig) Method(java.lang.reflect.Method) Predicate(com.google.common.base.Predicate) Binder(com.google.inject.Binder) ResourceFilters(com.sun.jersey.spi.container.ResourceFilters) Function(com.google.common.base.Function) ResourceFilter(com.sun.jersey.spi.container.ResourceFilter) DELETE(javax.ws.rs.DELETE) Injector(com.google.inject.Injector) GET(javax.ws.rs.GET) Collection(java.util.Collection) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Module(com.google.inject.Module) Key(com.google.inject.Key)

Example 4 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 5 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)

Aggregations

Key (com.google.inject.Key)150 Injector (com.google.inject.Injector)68 AbstractModule (com.google.inject.AbstractModule)65 Binding (com.google.inject.Binding)41 Module (com.google.inject.Module)28 Map (java.util.Map)23 Test (org.junit.Test)21 Provider (com.google.inject.Provider)18 Element (com.google.inject.spi.Element)17 TypeLiteral (com.google.inject.TypeLiteral)16 Annotation (java.lang.annotation.Annotation)15 HasDependencies (com.google.inject.spi.HasDependencies)14 Binder (com.google.inject.Binder)13 InstanceBinding (com.google.inject.spi.InstanceBinding)13 List (java.util.List)13 ImmutableList (com.google.common.collect.ImmutableList)12 InjectionPoint (com.google.inject.spi.InjectionPoint)11 HashMap (java.util.HashMap)11 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)10 ImmutableMap (com.google.common.collect.ImmutableMap)9