Search in sources :

Example 6 with PrivateModule

use of com.google.inject.PrivateModule in project guice by google.

the class ModuleAnnotatedMethodScannerTest method testPrivateModule_skipSourcesWithinPrivateModule.

public void testPrivateModule_skipSourcesWithinPrivateModule() {
    Injector injector = Guice.createInjector(NamedMunger.module(), new PrivateModule() {

        @Override
        protected void configure() {
            binder().skipSources(getClass()).install(new AbstractModule() {

                @Override
                protected void configure() {
                }

                @Exposed
                @TestProvides
                @Named("foo")
                String foo() {
                    return "foo";
                }
            });
        }
    });
    assertMungedBinding(injector, String.class, "foo", "foo");
}
Also used : Injector(com.google.inject.Injector) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule)

Example 7 with PrivateModule

use of com.google.inject.PrivateModule in project guice by google.

the class ModuleAnnotatedMethodScannerTest method testPrivateModuleInheritScanner_usingPrivateModule.

public void testPrivateModuleInheritScanner_usingPrivateModule() {
    Injector injector = Guice.createInjector(NamedMunger.module(), new PrivateModule() {

        @Override
        protected void configure() {
        }

        @Exposed
        @TestProvides
        @Named("foo")
        String foo() {
            return "foo";
        }
    });
    assertMungedBinding(injector, String.class, "foo", "foo");
}
Also used : Exposed(com.google.inject.Exposed) Named(com.google.inject.name.Named) Injector(com.google.inject.Injector) PrivateModule(com.google.inject.PrivateModule)

Example 8 with PrivateModule

use of com.google.inject.PrivateModule in project guice by google.

the class ModuleAnnotatedMethodScannerTest method testPrivateModuleScannersDontImpactSiblings_usingPrivateModule.

public void testPrivateModuleScannersDontImpactSiblings_usingPrivateModule() {
    Injector injector = Guice.createInjector(new PrivateModule() {

        @Override
        protected void configure() {
            install(NamedMunger.module());
        }

        @Exposed
        @TestProvides
        @Named("foo")
        String foo() {
            return "foo";
        }
    }, new PrivateModule() {

        @Override
        protected void configure() {
        }

        // ignored! (because the scanner doesn't run over this module)
        @Exposed
        @TestProvides
        @Named("foo")
        String foo() {
            return "foo";
        }
    });
    assertMungedBinding(injector, String.class, "foo", "foo");
}
Also used : Exposed(com.google.inject.Exposed) Named(com.google.inject.name.Named) Injector(com.google.inject.Injector) PrivateModule(com.google.inject.PrivateModule)

Example 9 with PrivateModule

use of com.google.inject.PrivateModule in project guice by google.

the class OverrideModuleTest method testOverrideModuleAndPrivateModule.

public void testOverrideModuleAndPrivateModule() {
    Module exposes5 = new PrivateModule() {

        @Override
        protected void configure() {
            bind(Integer.class).toInstance(5);
            expose(Integer.class);
        }
    };
    Module binds15 = new AbstractModule() {

        @Override
        protected void configure() {
            bind(Integer.class).toInstance(15);
        }
    };
    Injector injector = Guice.createInjector(Modules.override(exposes5).with(binds15));
    assertEquals(15, injector.getInstance(Integer.class).intValue());
    Injector reverse = Guice.createInjector(Modules.override(binds15).with(exposes5));
    assertEquals(5, reverse.getInstance(Integer.class).intValue());
}
Also used : Guice.createInjector(com.google.inject.Guice.createInjector) Injector(com.google.inject.Injector) Module(com.google.inject.Module) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule)

Example 10 with PrivateModule

use of com.google.inject.PrivateModule in project guice by google.

the class OverrideModuleTest method testOverridePrivateModuleOverPrivateModule.

public void testOverridePrivateModuleOverPrivateModule() {
    Module exposes5and6 = new AbstractModule() {

        @Override
        protected void configure() {
            install(new PrivateModule() {

                @Override
                protected void configure() {
                    bind(Integer.class).toInstance(5);
                    expose(Integer.class);
                    bind(Character.class).toInstance('E');
                }
            });
            install(new PrivateModule() {

                @Override
                protected void configure() {
                    bind(Long.class).toInstance(6L);
                    expose(Long.class);
                    bind(Character.class).toInstance('F');
                }
            });
        }
    };
    AbstractModule exposes15 = new AbstractModule() {

        @Override
        protected void configure() {
            install(new PrivateModule() {

                @Override
                protected void configure() {
                    bind(Integer.class).toInstance(15);
                    expose(Integer.class);
                    bind(Character.class).toInstance('G');
                }
            });
            install(new PrivateModule() {

                @Override
                protected void configure() {
                    bind(Character.class).toInstance('H');
                }
            });
        }
    };
    // override forwards
    Injector injector = Guice.createInjector(Modules.override(exposes5and6).with(exposes15));
    assertEquals(15, injector.getInstance(Integer.class).intValue());
    assertEquals(6L, injector.getInstance(Long.class).longValue());
    // and in reverse order
    Injector reverse = Guice.createInjector(Modules.override(exposes15).with(exposes5and6));
    assertEquals(5, reverse.getInstance(Integer.class).intValue());
    assertEquals(6L, reverse.getInstance(Long.class).longValue());
}
Also used : Guice.createInjector(com.google.inject.Guice.createInjector) Injector(com.google.inject.Injector) Module(com.google.inject.Module) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule)

Aggregations

PrivateModule (com.google.inject.PrivateModule)28 Injector (com.google.inject.Injector)17 AbstractModule (com.google.inject.AbstractModule)16 Module (com.google.inject.Module)10 FactoryModuleBuilder (com.google.inject.assistedinject.FactoryModuleBuilder)10 Guice.createInjector (com.google.inject.Guice.createInjector)6 DatasetDefinitionRegistry (co.cask.cdap.api.dataset.module.DatasetDefinitionRegistry)5 ConfigModule (co.cask.cdap.common.guice.ConfigModule)5 DefaultDatasetDefinitionRegistry (co.cask.cdap.data2.dataset2.DefaultDatasetDefinitionRegistry)5 DatasetFramework (co.cask.cdap.data2.dataset2.DatasetFramework)4 Binding (com.google.inject.Binding)4 Key (com.google.inject.Key)4 Singleton (com.google.inject.Singleton)4 Named (com.google.inject.name.Named)4 Element (com.google.inject.spi.Element)4 NonCustomLocationUnitTestModule (co.cask.cdap.common.guice.NonCustomLocationUnitTestModule)3 RemoteDatasetFramework (co.cask.cdap.data2.datafabric.dataset.RemoteDatasetFramework)3 DatasetDefinitionRegistryFactory (co.cask.cdap.data2.dataset2.DatasetDefinitionRegistryFactory)3 LineageStoreReader (co.cask.cdap.data2.metadata.lineage.LineageStoreReader)3 LineageStoreWriter (co.cask.cdap.data2.metadata.lineage.LineageStoreWriter)3