Search in sources :

Example 1 with Named

use of com.google.inject.name.Named in project roboguice by roboguice.

the class ServletTest method testScopeExceptions.

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

        @Override
        protected void configure() {
            install(new ServletModule());
        }

        @Provides
        @RequestScoped
        String provideString() {
            return "foo";
        }

        @Provides
        @SessionScoped
        Integer provideInteger() {
            return 1;
        }

        @Provides
        @RequestScoped
        @Named("foo")
        String provideNamedString() {
            return "foo";
        }
    });
    try {
        injector.getInstance(String.class);
        fail();
    } catch (ProvisionException oose) {
        assertContains(oose.getMessage(), "Cannot access scoped [java.lang.String].");
    }
    try {
        injector.getInstance(Integer.class);
        fail();
    } catch (ProvisionException oose) {
        assertContains(oose.getMessage(), "Cannot access scoped [java.lang.Integer].");
    }
    Key<?> key = Key.get(String.class, Names.named("foo"));
    try {
        injector.getInstance(key);
        fail();
    } catch (ProvisionException oose) {
        assertContains(oose.getMessage(), "Cannot access scoped [" + Errors.convert(key) + "]");
    }
}
Also used : Named(com.google.inject.name.Named) ProvisionException(com.google.inject.ProvisionException) Injector(com.google.inject.Injector) Provides(com.google.inject.Provides) AbstractModule(com.google.inject.AbstractModule)

Example 2 with Named

use of com.google.inject.name.Named 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 3 with Named

use of com.google.inject.name.Named in project guice by google.

the class ModuleAnnotatedMethodScannerTest method testWithSource.

public void testWithSource() throws Exception {
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            binder().withSource("source").install(new AbstractModule() {

                @Override
                protected void configure() {
                }

                @TestProvides
                @Named("foo")
                String foo() {
                    return "foo";
                }
            });
        }
    };
    Injector injector = Guice.createInjector(module, NamedMunger.module());
    assertMungedBinding(injector, String.class, "foo", "foo");
}
Also used : Named(com.google.inject.name.Named) Injector(com.google.inject.Injector) Module(com.google.inject.Module) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Example 4 with Named

use of com.google.inject.name.Named in project guice by google.

the class ProviderMethodsTest method testMultipleBindingAnnotations.

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 5 with Named

use of com.google.inject.name.Named in project guice by google.

the class ProviderMethodsTest method testProviderMethodDependenciesAreExposed.

public void testProviderMethodDependenciesAreExposed() throws Exception {
    Module module = 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;
        }
    };
    Injector injector = Guice.createInjector(module);
    ProviderInstanceBinding<?> binding = (ProviderInstanceBinding<?>) injector.getBinding(Key.get(String.class, Names.named("weight")));
    Method method = module.getClass().getDeclaredMethod("provideWeight", Integer.class, String.class);
    InjectionPoint point = new InjectionPoint(TypeLiteral.get(module.getClass()), method, false);
    assertEquals(ImmutableSet.<Dependency<?>>of(new Dependency<Integer>(point, Key.get(Integer.class), false, 0), new Dependency<String>(point, Key.get(String.class, Names.named("units")), false, 1)), binding.getDependencies());
}
Also used : Named(com.google.inject.name.Named) Injector(com.google.inject.Injector) ProviderMethod(com.google.inject.internal.ProviderMethod) Method(java.lang.reflect.Method) Module(com.google.inject.Module) ProviderMethodsModule(com.google.inject.internal.ProviderMethodsModule) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Aggregations

Named (com.google.inject.name.Named)26 AbstractModule (com.google.inject.AbstractModule)19 Injector (com.google.inject.Injector)14 Provides (com.google.inject.Provides)8 PrivateModule (com.google.inject.PrivateModule)7 Module (com.google.inject.Module)6 Annotation (java.lang.annotation.Annotation)5 Singleton (com.google.inject.Singleton)4 FactoryModuleBuilder (com.google.inject.assistedinject.FactoryModuleBuilder)4 DatasetDefinitionRegistry (co.cask.cdap.api.dataset.module.DatasetDefinitionRegistry)3 Binding (com.google.inject.Binding)3 ProvisionException (com.google.inject.ProvisionException)3 ProgramRunnerRuntimeModule (co.cask.cdap.app.guice.ProgramRunnerRuntimeModule)2 ConfigModule (co.cask.cdap.common.guice.ConfigModule)2 LocationRuntimeModule (co.cask.cdap.common.guice.LocationRuntimeModule)2 DatasetService (co.cask.cdap.data2.datafabric.dataset.service.DatasetService)2 DatasetAdminOpHTTPHandler (co.cask.cdap.data2.datafabric.dataset.service.executor.DatasetAdminOpHTTPHandler)2 DatasetOpExecutor (co.cask.cdap.data2.datafabric.dataset.service.executor.DatasetOpExecutor)2 DatasetOpExecutorService (co.cask.cdap.data2.datafabric.dataset.service.executor.DatasetOpExecutorService)2 LocalDatasetOpExecutor (co.cask.cdap.data2.datafabric.dataset.service.executor.LocalDatasetOpExecutor)2