Search in sources :

Example 1 with ScopedBindingBuilder

use of com.google.inject.binder.ScopedBindingBuilder 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 2 with ScopedBindingBuilder

use of com.google.inject.binder.ScopedBindingBuilder in project guice by google.

the class BinderTestSuite method suite.

public static Test suite() {
    TestSuite suite = new TestSuite();
    new Builder().name("bind A").module(new AbstractModule() {

        @Override
        protected void configure() {
            bind(A.class);
        }
    }).creationException("No implementation for %s was bound", A.class.getName()).addToSuite(suite);
    new Builder().name("bind PlainA named apple").module(new AbstractModule() {

        @Override
        protected void configure() {
            bind(PlainA.class).annotatedWith(named("apple"));
        }
    }).creationException("No implementation for %s annotated with %s was bound", PlainA.class.getName(), named("apple")).addToSuite(suite);
    new Builder().name("bind A to new PlainA(1)").module(new AbstractModule() {

        @Override
        protected void configure() {
            bind(A.class).toInstance(new PlainA(1));
        }
    }).creationTime(CreationTime.NONE).expectedValues(new PlainA(1), new PlainA(1), new PlainA(1)).addToSuite(suite);
    new Builder().name("no binding, AWithProvidedBy").key(Key.get(AWithProvidedBy.class), InjectsAWithProvidedBy.class).addToSuite(suite);
    new Builder().name("no binding, AWithImplementedBy").key(Key.get(AWithImplementedBy.class), InjectsAWithImplementedBy.class).addToSuite(suite);
    new Builder().name("no binding, ScopedA").key(Key.get(ScopedA.class), InjectsScopedA.class).expectedValues(new PlainA(201), new PlainA(201), new PlainA(202), new PlainA(202)).addToSuite(suite);
    new Builder().name("no binding, AWithProvidedBy named apple").key(Key.get(AWithProvidedBy.class, named("apple")), InjectsAWithProvidedByNamedApple.class).configurationException("No implementation for %s annotated with %s was bound", AWithProvidedBy.class.getName(), named("apple")).addToSuite(suite);
    new Builder().name("no binding, AWithImplementedBy named apple").key(Key.get(AWithImplementedBy.class, named("apple")), InjectsAWithImplementedByNamedApple.class).configurationException("No implementation for %s annotated with %s was bound", AWithImplementedBy.class.getName(), named("apple")).addToSuite(suite);
    new Builder().name("no binding, ScopedA named apple").key(Key.get(ScopedA.class, named("apple")), InjectsScopedANamedApple.class).configurationException("No implementation for %s annotated with %s was bound", ScopedA.class.getName(), named("apple")).addToSuite(suite);
    for (final Scoper scoper : Scoper.values()) {
        new Builder().name("bind PlainA").key(Key.get(PlainA.class), InjectsPlainA.class).module(new AbstractModule() {

            @Override
            protected void configure() {
                AnnotatedBindingBuilder<PlainA> abb = bind(PlainA.class);
                scoper.configure(abb);
            }
        }).scoper(scoper).addToSuite(suite);
        new Builder().name("bind A to PlainA").module(new AbstractModule() {

            @Override
            protected void configure() {
                ScopedBindingBuilder sbb = bind(A.class).to(PlainA.class);
                scoper.configure(sbb);
            }
        }).scoper(scoper).addToSuite(suite);
        new Builder().name("bind A to PlainAProvider.class").module(new AbstractModule() {

            @Override
            protected void configure() {
                ScopedBindingBuilder sbb = bind(A.class).toProvider(PlainAProvider.class);
                scoper.configure(sbb);
            }
        }).scoper(scoper).addToSuite(suite);
        new Builder().name("bind A to new PlainAProvider()").module(new AbstractModule() {

            @Override
            protected void configure() {
                ScopedBindingBuilder sbb = bind(A.class).toProvider(new PlainAProvider());
                scoper.configure(sbb);
            }
        }).scoper(scoper).addToSuite(suite);
        new Builder().name("bind AWithProvidedBy").key(Key.get(AWithProvidedBy.class), InjectsAWithProvidedBy.class).module(new AbstractModule() {

            @Override
            protected void configure() {
                ScopedBindingBuilder sbb = bind(AWithProvidedBy.class);
                scoper.configure(sbb);
            }
        }).scoper(scoper).addToSuite(suite);
        new Builder().name("bind AWithImplementedBy").key(Key.get(AWithImplementedBy.class), InjectsAWithImplementedBy.class).module(new AbstractModule() {

            @Override
            protected void configure() {
                ScopedBindingBuilder sbb = bind(AWithImplementedBy.class);
                scoper.configure(sbb);
            }
        }).scoper(scoper).addToSuite(suite);
        new Builder().name("bind ScopedA").key(Key.get(ScopedA.class), InjectsScopedA.class).module(new AbstractModule() {

            @Override
            protected void configure() {
                ScopedBindingBuilder sbb = bind(ScopedA.class);
                scoper.configure(sbb);
            }
        }).expectedValues(new PlainA(201), new PlainA(201), new PlainA(202), new PlainA(202)).scoper(scoper).addToSuite(suite);
        new Builder().name("bind AWithProvidedBy named apple").module(new AbstractModule() {

            @Override
            protected void configure() {
                scoper.configure(bind(AWithProvidedBy.class).annotatedWith(named("apple")));
            }
        }).creationException("No implementation for %s annotated with %s was bound", AWithProvidedBy.class.getName(), named("apple")).addToSuite(suite);
        new Builder().name("bind AWithImplementedBy named apple").module(new AbstractModule() {

            @Override
            protected void configure() {
                scoper.configure(bind(AWithImplementedBy.class).annotatedWith(named("apple")));
            }
        }).creationException("No implementation for %s annotated with %s was bound", AWithImplementedBy.class.getName(), named("apple")).addToSuite(suite);
        new Builder().name("bind ScopedA named apple").module(new AbstractModule() {

            @Override
            protected void configure() {
                scoper.configure(bind(ScopedA.class).annotatedWith(named("apple")));
            }
        }).creationException("No implementation for %s annotated with %s was bound", ScopedA.class.getName(), named("apple")).addToSuite(suite);
    }
    return suite;
}
Also used : ScopedBindingBuilder(com.google.inject.binder.ScopedBindingBuilder) ScopedBindingBuilder(com.google.inject.binder.ScopedBindingBuilder) AnnotatedBindingBuilder(com.google.inject.binder.AnnotatedBindingBuilder) AnnotatedBindingBuilder(com.google.inject.binder.AnnotatedBindingBuilder) TestSuite(junit.framework.TestSuite)

Example 3 with ScopedBindingBuilder

use of com.google.inject.binder.ScopedBindingBuilder in project roboguice by roboguice.

the class BinderTestSuite method suite.

public static Test suite() {
    TestSuite suite = new TestSuite();
    new Builder().name("bind A").module(new AbstractModule() {

        protected void configure() {
            bind(A.class);
        }
    }).creationException("No implementation for %s was bound", A.class.getName()).addToSuite(suite);
    new Builder().name("bind PlainA named apple").module(new AbstractModule() {

        protected void configure() {
            bind(PlainA.class).annotatedWith(named("apple"));
        }
    }).creationException("No implementation for %s annotated with %s was bound", PlainA.class.getName(), named("apple")).addToSuite(suite);
    new Builder().name("bind A to new PlainA(1)").module(new AbstractModule() {

        protected void configure() {
            bind(A.class).toInstance(new PlainA(1));
        }
    }).creationTime(CreationTime.NONE).expectedValues(new PlainA(1), new PlainA(1), new PlainA(1)).addToSuite(suite);
    new Builder().name("no binding, AWithProvidedBy").key(Key.get(AWithProvidedBy.class), InjectsAWithProvidedBy.class).addToSuite(suite);
    new Builder().name("no binding, AWithImplementedBy").key(Key.get(AWithImplementedBy.class), InjectsAWithImplementedBy.class).addToSuite(suite);
    new Builder().name("no binding, ScopedA").key(Key.get(ScopedA.class), InjectsScopedA.class).expectedValues(new PlainA(201), new PlainA(201), new PlainA(202), new PlainA(202)).addToSuite(suite);
    new Builder().name("no binding, AWithProvidedBy named apple").key(Key.get(AWithProvidedBy.class, named("apple")), InjectsAWithProvidedByNamedApple.class).configurationException("No implementation for %s annotated with %s was bound", AWithProvidedBy.class.getName(), named("apple")).addToSuite(suite);
    new Builder().name("no binding, AWithImplementedBy named apple").key(Key.get(AWithImplementedBy.class, named("apple")), InjectsAWithImplementedByNamedApple.class).configurationException("No implementation for %s annotated with %s was bound", AWithImplementedBy.class.getName(), named("apple")).addToSuite(suite);
    new Builder().name("no binding, ScopedA named apple").key(Key.get(ScopedA.class, named("apple")), InjectsScopedANamedApple.class).configurationException("No implementation for %s annotated with %s was bound", ScopedA.class.getName(), named("apple")).addToSuite(suite);
    for (final Scoper scoper : Scoper.values()) {
        new Builder().name("bind PlainA").key(Key.get(PlainA.class), InjectsPlainA.class).module(new AbstractModule() {

            protected void configure() {
                AnnotatedBindingBuilder<PlainA> abb = bind(PlainA.class);
                scoper.configure(abb);
            }
        }).scoper(scoper).addToSuite(suite);
        new Builder().name("bind A to PlainA").module(new AbstractModule() {

            protected void configure() {
                ScopedBindingBuilder sbb = bind(A.class).to(PlainA.class);
                scoper.configure(sbb);
            }
        }).scoper(scoper).addToSuite(suite);
        new Builder().name("bind A to PlainAProvider.class").module(new AbstractModule() {

            protected void configure() {
                ScopedBindingBuilder sbb = bind(A.class).toProvider(PlainAProvider.class);
                scoper.configure(sbb);
            }
        }).scoper(scoper).addToSuite(suite);
        new Builder().name("bind A to new PlainAProvider()").module(new AbstractModule() {

            protected void configure() {
                ScopedBindingBuilder sbb = bind(A.class).toProvider(new PlainAProvider());
                scoper.configure(sbb);
            }
        }).scoper(scoper).addToSuite(suite);
        new Builder().name("bind AWithProvidedBy").key(Key.get(AWithProvidedBy.class), InjectsAWithProvidedBy.class).module(new AbstractModule() {

            protected void configure() {
                ScopedBindingBuilder sbb = bind(AWithProvidedBy.class);
                scoper.configure(sbb);
            }
        }).scoper(scoper).addToSuite(suite);
        new Builder().name("bind AWithImplementedBy").key(Key.get(AWithImplementedBy.class), InjectsAWithImplementedBy.class).module(new AbstractModule() {

            protected void configure() {
                ScopedBindingBuilder sbb = bind(AWithImplementedBy.class);
                scoper.configure(sbb);
            }
        }).scoper(scoper).addToSuite(suite);
        new Builder().name("bind ScopedA").key(Key.get(ScopedA.class), InjectsScopedA.class).module(new AbstractModule() {

            protected void configure() {
                ScopedBindingBuilder sbb = bind(ScopedA.class);
                scoper.configure(sbb);
            }
        }).expectedValues(new PlainA(201), new PlainA(201), new PlainA(202), new PlainA(202)).scoper(scoper).addToSuite(suite);
        new Builder().name("bind AWithProvidedBy named apple").module(new AbstractModule() {

            protected void configure() {
                scoper.configure(bind(AWithProvidedBy.class).annotatedWith(named("apple")));
            }
        }).creationException("No implementation for %s annotated with %s was bound", AWithProvidedBy.class.getName(), named("apple")).addToSuite(suite);
        new Builder().name("bind AWithImplementedBy named apple").module(new AbstractModule() {

            protected void configure() {
                scoper.configure(bind(AWithImplementedBy.class).annotatedWith(named("apple")));
            }
        }).creationException("No implementation for %s annotated with %s was bound", AWithImplementedBy.class.getName(), named("apple")).addToSuite(suite);
        new Builder().name("bind ScopedA named apple").module(new AbstractModule() {

            protected void configure() {
                scoper.configure(bind(ScopedA.class).annotatedWith(named("apple")));
            }
        }).creationException("No implementation for %s annotated with %s was bound", ScopedA.class.getName(), named("apple")).addToSuite(suite);
    }
    return suite;
}
Also used : ScopedBindingBuilder(com.google.inject.binder.ScopedBindingBuilder) AnnotatedBindingBuilder(com.google.inject.binder.AnnotatedBindingBuilder) ScopedBindingBuilder(com.google.inject.binder.ScopedBindingBuilder) AbstractModule(com.google.inject.AbstractModule) AnnotatedBindingBuilder(com.google.inject.binder.AnnotatedBindingBuilder) TestSuite(junit.framework.TestSuite)

Example 4 with ScopedBindingBuilder

use of com.google.inject.binder.ScopedBindingBuilder 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

ScopedBindingBuilder (com.google.inject.binder.ScopedBindingBuilder)4 PrivateBinder (com.google.inject.PrivateBinder)2 AnnotatedBindingBuilder (com.google.inject.binder.AnnotatedBindingBuilder)2 TestSuite (junit.framework.TestSuite)2 AbstractModule (com.google.inject.AbstractModule)1