use of com.google.inject.Binding in project guice by google.
the class ElementsTest method testBindKeysNoAnnotations.
public void testBindKeysNoAnnotations() {
FailingElementVisitor keyChecker = new FailingElementVisitor() {
@Override
public <T> Void visit(Binding<T> command) {
assertEquals(Key.get(String.class), command.getKey());
return null;
}
};
checkModule(new AbstractModule() {
@Override
protected void configure() {
bind(String.class).toInstance("A");
bind(new TypeLiteral<String>() {
}).toInstance("B");
bind(Key.get(String.class)).toInstance("C");
}
}, keyChecker, keyChecker, keyChecker);
}
use of com.google.inject.Binding in project guice by google.
the class ElementsTest method testBindKeysWithAnnotationInstance.
public void testBindKeysWithAnnotationInstance() {
FailingElementVisitor annotationChecker = new FailingElementVisitor() {
@Override
public <T> Void visit(Binding<T> command) {
assertEquals(Key.get(String.class, Names.named("a")), command.getKey());
return null;
}
};
checkModule(new AbstractModule() {
@Override
protected void configure() {
bind(String.class).annotatedWith(Names.named("a")).toInstance("B");
bind(new TypeLiteral<String>() {
}).annotatedWith(Names.named("a")).toInstance("C");
}
}, annotationChecker, annotationChecker);
}
use of com.google.inject.Binding in project guice by google.
the class ModuleRewriterTest method testRewriteBindings.
public void testRewriteBindings() {
// create a module the binds String.class and CharSequence.class
Module module = new AbstractModule() {
@Override
protected void configure() {
bind(String.class).toInstance("Pizza");
bind(CharSequence.class).toInstance("Wine");
}
};
// record the elements from that module
List<Element> elements = Elements.getElements(module);
// create a rewriter that rewrites the binding to 'Wine' with a binding to 'Beer'
List<Element> rewritten = Lists.newArrayList();
for (Element element : elements) {
element = element.acceptVisitor(new DefaultElementVisitor<Element>() {
@Override
public <T> Element visit(Binding<T> binding) {
T target = binding.acceptTargetVisitor(Elements.<T>getInstanceVisitor());
if ("Wine".equals(target)) {
return null;
} else {
return binding;
}
}
});
if (element != null) {
rewritten.add(element);
}
}
// create a module from the original list of elements and the rewriter
Module rewrittenModule = Elements.getModule(rewritten);
// the wine binding is dropped
Injector injector = Guice.createInjector(rewrittenModule);
try {
injector.getInstance(CharSequence.class);
fail();
} catch (ConfigurationException expected) {
}
}
use of com.google.inject.Binding 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.Binding in project ninja by ninjaframework.
the class LifecycleServiceImpl method start.
@Override
public void start() {
startTime = System.currentTimeMillis();
log.info("Starting Ninja application...");
state = State.STARTING;
// until they are instantiated that LifecycleSupport has an opportunity to register them.
for (final Binding binding : injector.getBindings().values()) {
binding.acceptScopingVisitor(new DefaultBindingScopingVisitor() {
@Override
public Object visitEagerSingleton() {
injector.getInstance(binding.getKey());
return null;
}
@Override
public Object visitScope(Scope scope) {
if (scope.equals(Scopes.SINGLETON)) {
Object target = injector.getInstance(binding.getKey());
if (binding instanceof ProviderInstanceBinding) {
Provider providerInstance = ((ProviderInstanceBinding) binding).getProviderInstance();
if (providerInstance instanceof ProviderMethod) {
// @Provides methods don't get picked up by TypeListeners, so we need to manually register them
if (lifecycleSupport.hasLifecycleMethod(target.getClass())) {
lifecycleSupport.registerLifecycle(target);
}
}
}
}
return null;
}
});
}
lifecycleRegister.start();
long time = System.currentTimeMillis() - startTime;
log.info("Ninja application started in {}ms", time);
state = lifecycleRegister.isStarted() ? State.STARTED : State.STOPPED;
}
Aggregations