Search in sources :

Example 6 with PrivateBinder

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

the class ElementsTest method testNewPrivateBinder.

public void testNewPrivateBinder() {
    final Key<Collection> collection = Key.get(Collection.class, SampleAnnotation.class);
    final Key<ArrayList> arrayList = Key.get(ArrayList.class);
    final ImmutableSet<Key<?>> collections = ImmutableSet.<Key<?>>of(arrayList, collection);
    final Key<?> a = Key.get(String.class, Names.named("a"));
    final Key<?> b = Key.get(String.class, Names.named("b"));
    final ImmutableSet<Key<?>> ab = ImmutableSet.of(a, b);
    checkModule(new AbstractModule() {

        protected void configure() {
            PrivateBinder one = binder().newPrivateBinder();
            one.expose(ArrayList.class);
            one.expose(Collection.class).annotatedWith(SampleAnnotation.class);
            one.bind(List.class).to(ArrayList.class);
            PrivateBinder two = binder().withSource("1 FooBar").newPrivateBinder().withSource("2 FooBar");
            two.expose(String.class).annotatedWith(Names.named("a"));
            two.expose(b);
            two.bind(List.class).to(ArrayList.class);
        }
    }, new FailingElementVisitor() {

        @Override
        public Void visit(PrivateElements one) {
            assertEquals(collections, one.getExposedKeys());
            checkElements(one.getElements(), new FailingElementVisitor() {

                @Override
                public <T> Void visit(Binding<T> binding) {
                    assertEquals(Key.get(List.class), binding.getKey());
                    return null;
                }
            });
            return null;
        }
    }, new ExternalFailureVisitor() {

        @Override
        public Void visit(PrivateElements two) {
            assertEquals(ab, two.getExposedKeys());
            assertEquals("1 FooBar", two.getSource().toString());
            checkElements(two.getElements(), new ExternalFailureVisitor() {

                @Override
                public <T> Void visit(Binding<T> binding) {
                    assertEquals("2 FooBar", binding.getSource().toString());
                    assertEquals(Key.get(List.class), binding.getKey());
                    return null;
                }
            });
            return null;
        }
    });
}
Also used : Binding(com.google.inject.Binding) ArrayList(java.util.ArrayList) AbstractModule(com.google.inject.AbstractModule) PrivateBinder(com.google.inject.PrivateBinder) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) Key(com.google.inject.Key)

Example 7 with PrivateBinder

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

the class CheckedProviderMethod method configure.

void configure(Binder binder) {
    binder = binder.withSource(method);
    SecondaryBinder<?, ?> sbinder = ThrowingProviderBinder.create(binder).bind(checkedProvider, key.getTypeLiteral());
    if (key.getAnnotation() != null) {
        sbinder = sbinder.annotatedWith(key.getAnnotation());
    } else if (key.getAnnotationType() != null) {
        sbinder = sbinder.annotatedWith(key.getAnnotationType());
    }
    ScopedBindingBuilder sbbuilder = sbinder.toProviderMethod(this);
    if (scopeAnnotation != null) {
        sbbuilder.in(scopeAnnotation);
    }
    if (exposed) {
        // the cast is safe 'cause the only binder we have implements PrivateBinder. If there's a
        // misplaced @Exposed, calling this will add an error to the binder's error queue
        ((PrivateBinder) binder).expose(sbinder.getKey());
    }
    CheckedProvideUtils.validateExceptions(binder, exceptionTypes, sbinder.getExceptionTypes(), checkedProvider);
}
Also used : ScopedBindingBuilder(com.google.inject.binder.ScopedBindingBuilder) PrivateBinder(com.google.inject.PrivateBinder)

Aggregations

PrivateBinder (com.google.inject.PrivateBinder)7 Key (com.google.inject.Key)4 ImmutableMap (com.google.common.collect.ImmutableMap)2 AbstractModule (com.google.inject.AbstractModule)2 Binding (com.google.inject.Binding)2 ScopedBindingBuilder (com.google.inject.binder.ScopedBindingBuilder)2 Element (com.google.inject.spi.Element)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 Map (java.util.Map)2