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;
}
});
}
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);
}
Aggregations