Search in sources :

Example 86 with Provides

use of com.google.inject.Provides in project roboguice by roboguice.

the class ProviderMethodsTest method testProviderMethodDependenciesAreExposed.

public void testProviderMethodDependenciesAreExposed() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(Integer.class).toInstance(50);
            bindConstant().annotatedWith(Names.named("units")).to("Kg");
        }

        @Provides
        @Named("weight")
        String provideWeight(Integer count, @Named("units") String units) {
            return count + units;
        }
    });
    ProviderInstanceBinding<?> binding = (ProviderInstanceBinding<?>) injector.getBinding(Key.get(String.class, Names.named("weight")));
    assertEquals(ImmutableSet.<Dependency<?>>of(Dependency.get(Key.get(Integer.class)), Dependency.get(Key.get(String.class, Names.named("units")))), binding.getDependencies());
}
Also used : Named(com.google.inject.name.Named) Injector(com.google.inject.Injector) Provides(com.google.inject.Provides) AbstractModule(com.google.inject.AbstractModule)

Example 87 with Provides

use of com.google.inject.Provides in project roboguice by roboguice.

the class ProviderMethodsTest method testMultipleBindingAnnotations.

// We'll have to make getProvider() support circular dependencies before this
// will work.
//
//  public void testCircularDependency() {
//    Injector injector = Guice.createInjector(new Module() {
//      public void configure(Binder binder) {
//        binder.install(ProviderMethods.from(ProviderMethodsTest.this));
//      }
//    });
//
//    Foo foo = injector.getInstance(Foo.class);
//    assertEquals(5, foo.getI());
//    assertEquals(10, foo.getBar().getI());
//    assertEquals(5, foo.getBar().getFoo().getI());
//  }
//
//  interface Foo {
//    Bar getBar();
//    int getI();
//  }
//
//  interface Bar {
//    Foo getFoo();
//    int getI();
//  }
//
//  @Provides Foo newFoo(final Bar bar) {
//    return new Foo() {
//
//      public Bar getBar() {
//        return bar;
//      }
//
//      public int getI() {
//        return 5;
//      }
//    };
//  }
//
//  @Provides Bar newBar(final Foo foo) {
//    return new Bar() {
//
//      public Foo getFoo() {
//        return foo;
//      }
//
//      public int getI() {
//        return 10;
//      }
//    };
//  }
public void testMultipleBindingAnnotations() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
            }

            @Provides
            @Named("A")
            @Blue
            public String provideString() {
                return "a";
            }
        });
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), "more than one annotation annotated with @BindingAnnotation:", "Named", "Blue", "at " + getClass().getName(), ".provideString(ProviderMethodsTest.java:");
    }
}
Also used : Named(com.google.inject.name.Named) CreationException(com.google.inject.CreationException) Provides(com.google.inject.Provides) AbstractModule(com.google.inject.AbstractModule)

Example 88 with Provides

use of com.google.inject.Provides in project roboguice by roboguice.

the class FactoryModuleBuilderTest method testImplicitForwardingAssistedBindingCreatesNewObjects.

public void testImplicitForwardingAssistedBindingCreatesNewObjects() {
    final Mustang providedMustang = new Mustang(Color.BLUE);
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            install(new FactoryModuleBuilder().build(MustangFactory.class));
        }

        @Provides
        Mustang provide() {
            return providedMustang;
        }
    });
    assertSame(providedMustang, injector.getInstance(Mustang.class));
    MustangFactory factory = injector.getInstance(MustangFactory.class);
    Mustang created = factory.create(Color.GREEN);
    assertNotSame(providedMustang, created);
    assertEquals(Color.BLUE, providedMustang.color);
    assertEquals(Color.GREEN, created.color);
}
Also used : Injector(com.google.inject.Injector) Provides(com.google.inject.Provides) AbstractModule(com.google.inject.AbstractModule)

Example 89 with Provides

use of com.google.inject.Provides in project roboguice by roboguice.

the class TransferRequestIntegrationTest method testTransferNonHttpRequest.

public void testTransferNonHttpRequest() throws Exception {
    final Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bindScope(RequestScoped.class, ServletScopes.REQUEST);
        }

        @Provides
        @RequestScoped
        Object provideObject() {
            return new Object();
        }
    });
    Callable<Callable<Boolean>> callable = new Callable<Callable<Boolean>>() {

        @Override
        public Callable<Boolean> call() {
            final Object original = injector.getInstance(Object.class);
            return ServletScopes.transferRequest(new Callable<Boolean>() {

                @Override
                public Boolean call() {
                    return original == injector.getInstance(Object.class);
                }
            });
        }
    };
    ImmutableMap<Key<?>, Object> seedMap = ImmutableMap.of();
    Callable<Boolean> transfer = ServletScopes.scopeRequest(callable, seedMap).call();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    assertTrue(executor.submit(transfer).get());
    executor.shutdownNow();
}
Also used : Provides(com.google.inject.Provides) Callable(java.util.concurrent.Callable) AbstractModule(com.google.inject.AbstractModule) Injector(com.google.inject.Injector) ExecutorService(java.util.concurrent.ExecutorService) Key(com.google.inject.Key)

Example 90 with Provides

use of com.google.inject.Provides in project airpal by airbnb.

the class AirpalModule method provideDBI.

@Singleton
@Provides
public DBI provideDBI(ObjectMapper objectMapper) throws ClassNotFoundException {
    final DBIFactory factory = new DBIFactory();
    final DBI dbi = factory.build(environment, config.getDataSourceFactory(), provideDbType().name());
    dbi.registerMapper(new TableRow.TableRowMapper(objectMapper));
    dbi.registerMapper(new QueryStoreMapper(objectMapper));
    dbi.registerArgumentFactory(new UUIDArgumentFactory());
    dbi.registerArgumentFactory(new URIArgumentFactory());
    return dbi;
}
Also used : URIArgumentFactory(com.airbnb.airpal.sql.jdbi.URIArgumentFactory) UUIDArgumentFactory(com.airbnb.airpal.sql.jdbi.UUIDArgumentFactory) TableRow(com.airbnb.airpal.sql.beans.TableRow) DBI(org.skife.jdbi.v2.DBI) QueryStoreMapper(com.airbnb.airpal.sql.jdbi.QueryStoreMapper) DBIFactory(io.dropwizard.jdbi.DBIFactory) Singleton(com.google.inject.Singleton) Provides(com.google.inject.Provides)

Aggregations

Provides (com.google.inject.Provides)111 AbstractModule (com.google.inject.AbstractModule)26 Singleton (com.google.inject.Singleton)23 Singleton (javax.inject.Singleton)23 Injector (com.google.inject.Injector)22 LazySingleton (io.druid.guice.LazySingleton)12 Named (javax.inject.Named)10 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 Named (com.google.inject.name.Named)8 File (java.io.File)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 Inject (javax.inject.Inject)6 Properties (java.util.Properties)5 Test (org.junit.Test)5 Key (com.google.inject.Key)4 ExecutorService (java.util.concurrent.ExecutorService)4 ConfigModule (co.cask.cdap.common.guice.ConfigModule)3 DB (com.codecademy.eventhub.base.DB)3 Binder (com.google.inject.Binder)3