Search in sources :

Example 21 with Provides

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

the class TextTemplateModule method provideGroovyTemplateRenderingEngine.

@Provides
@Singleton
TextTemplateRenderingEngine provideGroovyTemplateRenderingEngine(ServerConfig serverConfig, ExecController execController, ByteBufAllocator bufferAllocator, Config config) {
    String templatesPath = config.getTemplatesPath();
    FileSystemBinding templateDir = serverConfig.getBaseDir().binding(templatesPath);
    if (templateDir == null) {
        throw new IllegalStateException("templatesPath '" + templatesPath + "' is outside the file system binding");
    }
    return new TextTemplateRenderingEngine(bufferAllocator, templateDir, serverConfig.isDevelopment(), config.staticallyCompile);
}
Also used : FileSystemBinding(ratpack.file.FileSystemBinding) TextTemplateRenderingEngine(ratpack.groovy.template.internal.TextTemplateRenderingEngine) Singleton(com.google.inject.Singleton) Provides(com.google.inject.Provides)

Example 22 with Provides

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

the class DefaultRoboModule method providesAndroidId.

@Provides
@Named(Settings.Secure.ANDROID_ID)
public String providesAndroidId() {
    String androidId = null;
    final ContentResolver contentResolver = application.getContentResolver();
    try {
        androidId = Secure.getString(contentResolver, Secure.ANDROID_ID);
    } catch (RuntimeException e) {
        // ignore Stub! errors for Secure.getString() when mocking in test cases since there's no way to mock static methods
        Log.e(DefaultRoboModule.class.getName(), "Impossible to get the android device Id. This may fail 'normally' when testing.", e);
    }
    if (!"".equals(androidId)) {
        return androidId;
    } else {
        throw new RuntimeException("No Android Id.");
    }
}
Also used : ContentResolver(android.content.ContentResolver) Named(com.google.inject.name.Named) Provides(com.google.inject.Provides)

Example 23 with Provides

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

the class FactoryModuleBuilderTest method testExplicitForwardingAssistedBindingCreatesNewObjects.

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

        @Override
        protected void configure() {
            install(new FactoryModuleBuilder().implement(Car.class, Mustang.class).build(ColoredCarFactory.class));
        }

        @Provides
        Mustang provide() {
            return providedMustang;
        }
    });
    assertSame(providedMustang, injector.getInstance(Mustang.class));
    ColoredCarFactory factory = injector.getInstance(ColoredCarFactory.class);
    Mustang created = (Mustang) 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 24 with Provides

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

the class ProviderMethodsTest method testVoidProviderMethods.

public void testVoidProviderMethods() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
            }

            @Provides
            void provideFoo() {
            }
        });
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), "1) Provider methods must return a value. Do not return void.", getClass().getName(), ".provideFoo(ProviderMethodsTest.java:");
    }
}
Also used : CreationException(com.google.inject.CreationException) Provides(com.google.inject.Provides) AbstractModule(com.google.inject.AbstractModule)

Example 25 with Provides

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

the class ThymeleafModule method provideTemplateResolver.

@Provides
@Singleton
ITemplateResolver provideTemplateResolver(ServerConfig serverConfig, Config config) {
    IResourceResolver resourceResolver = new FileSystemBindingThymeleafResourceResolver(serverConfig.getBaseDir());
    TemplateResolver templateResolver = new TemplateResolver();
    templateResolver.setResourceResolver(resourceResolver);
    templateResolver.setTemplateMode(getTemplatesModeSetting(config));
    templateResolver.setPrefix(getTemplatesPrefixSetting(config));
    templateResolver.setSuffix(getTemplatesSuffixSetting(config));
    templateResolver.setCacheable(getCacheSizeSetting(config) > 0);
    // Never use TTL expiration
    templateResolver.setCacheTTLMs(null);
    return templateResolver;
}
Also used : IResourceResolver(org.thymeleaf.resourceresolver.IResourceResolver) FileSystemBindingThymeleafResourceResolver(ratpack.thymeleaf.internal.FileSystemBindingThymeleafResourceResolver) TemplateResolver(org.thymeleaf.templateresolver.TemplateResolver) ITemplateResolver(org.thymeleaf.templateresolver.ITemplateResolver) 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