Search in sources :

Example 1 with OptionalBinder

use of com.google.inject.multibindings.OptionalBinder in project guice by google.

the class OptionalBinderTest method testDependencies_actual.

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

        @Override
        protected void configure() {
            OptionalBinder<String> optionalbinder = OptionalBinder.newOptionalBinder(binder(), String.class);
            optionalbinder.setBinding().to(Key.get(String.class, Names.named("b")));
            bindConstant().annotatedWith(Names.named("b")).to("B");
        }
    });
    Binding<String> binding = injector.getBinding(Key.get(String.class));
    HasDependencies withDependencies = (HasDependencies) binding;
    Set<String> elements = Sets.newHashSet();
    elements.addAll(recurseForDependencies(injector, withDependencies));
    assertEquals(ImmutableSet.of("B"), elements);
}
Also used : OptionalBinder(com.google.inject.multibindings.OptionalBinder) Injector(com.google.inject.Injector) HasDependencies(com.google.inject.spi.HasDependencies) AbstractModule(com.google.inject.AbstractModule)

Example 2 with OptionalBinder

use of com.google.inject.multibindings.OptionalBinder in project guice by google.

the class OptionalBinderTest method testDependencies_both.

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

        @Override
        protected void configure() {
            OptionalBinder<String> optionalbinder = OptionalBinder.newOptionalBinder(binder(), String.class);
            optionalbinder.setDefault().toInstance("A");
            optionalbinder.setBinding().to(Key.get(String.class, Names.named("b")));
            bindConstant().annotatedWith(Names.named("b")).to("B");
        }
    });
    Binding<String> binding = injector.getBinding(Key.get(String.class));
    HasDependencies withDependencies = (HasDependencies) binding;
    Set<String> elements = Sets.newHashSet();
    elements.addAll(recurseForDependencies(injector, withDependencies));
    assertEquals(ImmutableSet.of("B"), elements);
}
Also used : OptionalBinder(com.google.inject.multibindings.OptionalBinder) Injector(com.google.inject.Injector) HasDependencies(com.google.inject.spi.HasDependencies) AbstractModule(com.google.inject.AbstractModule)

Example 3 with OptionalBinder

use of com.google.inject.multibindings.OptionalBinder in project guice by google.

the class OptionalBinderTest method testDependencies_default.

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

        @Override
        protected void configure() {
            OptionalBinder<String> optionalbinder = OptionalBinder.newOptionalBinder(binder(), String.class);
            optionalbinder.setDefault().toInstance("A");
        }
    });
    Binding<String> binding = injector.getBinding(Key.get(String.class));
    HasDependencies withDependencies = (HasDependencies) binding;
    Set<String> elements = Sets.newHashSet();
    elements.addAll(recurseForDependencies(injector, withDependencies));
    assertEquals(ImmutableSet.of("A"), elements);
}
Also used : OptionalBinder(com.google.inject.multibindings.OptionalBinder) Injector(com.google.inject.Injector) HasDependencies(com.google.inject.spi.HasDependencies) AbstractModule(com.google.inject.AbstractModule)

Example 4 with OptionalBinder

use of com.google.inject.multibindings.OptionalBinder in project guice by google.

the class OptionalBinderTest method testMatchingMarkerAnnotations.

@Marker
public void testMatchingMarkerAnnotations() throws Exception {
    Method m = OptionalBinderTest.class.getDeclaredMethod("testMatchingMarkerAnnotations");
    assertNotNull(m);
    final Annotation marker = m.getAnnotation(Marker.class);
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        public void configure() {
            OptionalBinder<Integer> mb1 = OptionalBinder.newOptionalBinder(binder(), Key.get(Integer.class, Marker.class));
            OptionalBinder<Integer> mb2 = OptionalBinder.newOptionalBinder(binder(), Key.get(Integer.class, marker));
            mb1.setDefault().toInstance(1);
            mb2.setBinding().toInstance(2);
            // This assures us that the two binders are equivalent, so we expect the instance added to
            // each to have been added to one set.
            assertEquals(mb1, mb2);
        }
    });
    Integer i1 = injector.getInstance(Key.get(Integer.class, Marker.class));
    Integer i2 = injector.getInstance(Key.get(Integer.class, marker));
    // These must be identical, because the marker annotations collapsed to the same thing.
    assertSame(i1, i2);
    assertEquals(2, i2.intValue());
}
Also used : OptionalBinder(com.google.inject.multibindings.OptionalBinder) Injector(com.google.inject.Injector) Method(java.lang.reflect.Method) BindingAnnotation(com.google.inject.BindingAnnotation) Annotation(java.lang.annotation.Annotation) AbstractModule(com.google.inject.AbstractModule)

Aggregations

AbstractModule (com.google.inject.AbstractModule)4 Injector (com.google.inject.Injector)4 OptionalBinder (com.google.inject.multibindings.OptionalBinder)4 HasDependencies (com.google.inject.spi.HasDependencies)3 BindingAnnotation (com.google.inject.BindingAnnotation)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1