Search in sources :

Example 1 with ExposedBinding

use of com.google.inject.spi.ExposedBinding in project guice by google.

the class PrivateModuleTest method testSpiAccess.

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

        @Override
        public void configure() {
            bind(String.class).annotatedWith(named("a")).toInstance("private");
            bind(String.class).annotatedWith(named("b")).toInstance("exposed");
            expose(String.class).annotatedWith(named("b"));
        }
    });
    ExposedBinding<?> binding = (ExposedBinding<?>) injector.getBinding(Key.get(String.class, Names.named("b")));
    assertEquals(ImmutableSet.<Dependency<?>>of(Dependency.get(Key.get(Injector.class))), binding.getDependencies());
    PrivateElements privateElements = binding.getPrivateElements();
    assertEquals(ImmutableSet.<Key<?>>of(Key.get(String.class, named("b"))), privateElements.getExposedKeys());
    assertContains(privateElements.getExposedSource(Key.get(String.class, named("b"))).toString(), PrivateModuleTest.class.getName(), getDeclaringSourcePart(getClass()));
    Injector privateInjector = privateElements.getInjector();
    assertEquals("private", privateInjector.getInstance(Key.get(String.class, Names.named("a"))));
}
Also used : PrivateElements(com.google.inject.spi.PrivateElements) ExposedBinding(com.google.inject.spi.ExposedBinding)

Example 2 with ExposedBinding

use of com.google.inject.spi.ExposedBinding in project roboguice by roboguice.

the class PrivateModuleTest method testSpiAccess.

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

        @Override
        public void configure() {
            bind(String.class).annotatedWith(named("a")).toInstance("private");
            bind(String.class).annotatedWith(named("b")).toInstance("exposed");
            expose(String.class).annotatedWith(named("b"));
        }
    });
    ExposedBinding<?> binding = (ExposedBinding<?>) injector.getBinding(Key.get(String.class, Names.named("b")));
    assertEquals(ImmutableSet.<Dependency<?>>of(Dependency.get(Key.get(Injector.class))), binding.getDependencies());
    PrivateElements privateElements = binding.getPrivateElements();
    assertEquals(ImmutableSet.<Key<?>>of(Key.get(String.class, named("b"))), privateElements.getExposedKeys());
    assertContains(privateElements.getExposedSource(Key.get(String.class, named("b"))).toString(), PrivateModuleTest.class.getName(), getDeclaringSourcePart(getClass()));
    Injector privateInjector = privateElements.getInjector();
    assertEquals("private", privateInjector.getInstance(Key.get(String.class, Names.named("a"))));
}
Also used : PrivateElements(com.google.inject.spi.PrivateElements) ExposedBinding(com.google.inject.spi.ExposedBinding)

Aggregations

ExposedBinding (com.google.inject.spi.ExposedBinding)2 PrivateElements (com.google.inject.spi.PrivateElements)2