Search in sources :

Example 1 with PrivateBinder

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

the class PrivateElementsImpl method applyTo.

public void applyTo(Binder binder) {
    PrivateBinder privateBinder = binder.withSource(source).newPrivateBinder();
    for (Element element : getElements()) {
        element.applyTo(privateBinder);
    }
    // ensure exposedKeysToSources is populated
    getExposedKeys();
    for (Map.Entry<Key<?>, Object> entry : exposedKeysToSources.entrySet()) {
        privateBinder.withSource(entry.getValue()).expose(entry.getKey());
    }
}
Also used : PrivateBinder(com.google.inject.PrivateBinder) Element(com.google.inject.spi.Element) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Key(com.google.inject.Key)

Example 2 with PrivateBinder

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

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());
    }
    sbinder.scopeExceptions(scopeExceptions);
    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)

Example 3 with PrivateBinder

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

the class PrivateElementsImpl method applyTo.

@Override
public void applyTo(Binder binder) {
    PrivateBinder privateBinder = binder.withSource(source).newPrivateBinder();
    for (Element element : getElements()) {
        element.applyTo(privateBinder);
    }
    // ensure exposedKeysToSources is populated
    getExposedKeys();
    for (Map.Entry<Key<?>, Object> entry : exposedKeysToSources.entrySet()) {
        privateBinder.withSource(entry.getValue()).expose(entry.getKey());
    }
}
Also used : PrivateBinder(com.google.inject.PrivateBinder) Element(com.google.inject.spi.Element) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Key(com.google.inject.Key)

Example 4 with PrivateBinder

use of com.google.inject.PrivateBinder in project OpenAM by OpenRock.

the class DataLayerConnectionModule method configure.

@Override
protected void configure() {
    PrivateBinder binder = binder().withSource(connectionType);
    binder.bind(ConnectionType.class).toInstance(connectionType);
    configureConnections(binder);
    configureTaskExecutor(binder);
    configureDataStore(binder);
    expose(binder, ConnectionFactory.class);
    expose(binder, QueryFactory.class);
    if (exposesQueueConfiguration) {
        expose(binder, QueueConfiguration.class);
    }
    if (executorType != null) {
        expose(binder, TaskFactory.class);
        expose(binder, TaskExecutor.class);
    }
}
Also used : PrivateBinder(com.google.inject.PrivateBinder)

Example 5 with PrivateBinder

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

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() {

        @Override
        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)

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