Java Code Examples for com.google.inject.Injector#getBinding()

The following examples show how to use com.google.inject.Injector#getBinding() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PageObjectProviderHelper.java    From bobcat with Apache License 2.0 5 votes vote down vote up
private static Class<?> retrieveBindingOfPageObjectInterface(Class<?> type, Injector injector) {
  Binding<?> binding = injector.getBinding(type);
  if (binding instanceof LinkedBindingImpl) {
    type = ((LinkedBindingImpl) binding).getLinkedKey().getTypeLiteral().getRawType();
  }
  return type;
}
 
Example 2
Source File: ModuleTest.java    From mangooio with Apache License 2.0 5 votes vote down vote up
@Test
public void testBindings() {
       //given
	Injector guice = Application.getInjector();
	
       //when
	Binding<Stage> stage = guice.getBinding(Stage.class);
	Binding<Injector> injector = guice.getBinding(Injector.class);
	Binding<Logger> logger = guice.getBinding(Logger.class);
	Binding<Config> config = guice.getBinding(Config.class);
	Binding<JobFactory> jobFactory = guice.getBinding(JobFactory.class);
	Binding<Cache> cache = guice.getBinding(Cache.class);
	Binding<TemplateEngine> templateEngine = guice.getBinding(TemplateEngine.class);		
	Binding<OncePerRequestFilter> mangooRequestFilter = guice.getBinding(OncePerRequestFilter.class);
	Binding<MangooBootstrap> mangooBootstrap = guice.getBinding(MangooBootstrap.class);
	
	//then
	assertThat(stage.getKey().getTypeLiteral().getType().getTypeName(), equalTo("com.google.inject.Stage"));
	assertThat(injector.getKey().getTypeLiteral().getType().getTypeName(), equalTo("com.google.inject.Injector"));
	assertThat(logger.getKey().getTypeLiteral().getType().getTypeName(), equalTo("java.util.logging.Logger"));
	assertThat(config.getKey().getTypeLiteral().getType().getTypeName(), equalTo("io.mangoo.core.Config"));
	assertThat(jobFactory.getKey().getTypeLiteral().getType().getTypeName(), equalTo("org.quartz.spi.JobFactory"));
	assertThat(cache.getKey().getTypeLiteral().getType().getTypeName(), equalTo("io.mangoo.cache.Cache"));
	assertThat(templateEngine.getKey().getTypeLiteral().getType().getTypeName(), equalTo("io.mangoo.templating.TemplateEngine"));
	assertThat(mangooRequestFilter.getKey().getTypeLiteral().getType().getTypeName(), equalTo("io.mangoo.interfaces.filters.OncePerRequestFilter"));
	assertThat(mangooBootstrap.getKey().getTypeLiteral().getType().getTypeName(), equalTo("io.mangoo.interfaces.MangooBootstrap"));
}
 
Example 3
Source File: KaryonAbstractInjectorGrapher.java    From karyon with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the bindings for the root keys and their transitive dependencies.
 */
private Iterable<Binding<?>> getBindings(Injector injector, Set<Key<?>> root) {
    Set<Key<?>> keys = Sets.newHashSet(root);
    Set<Key<?>> visitedKeys = Sets.newHashSet();
    List<Binding<?>> bindings = Lists.newArrayList();
    TransitiveDependencyVisitor keyVisitor = new TransitiveDependencyVisitor();

    while (!keys.isEmpty()) {
        Iterator<Key<?>> iterator = keys.iterator();
        Key<?> key = iterator.next();
        iterator.remove();

        if (!visitedKeys.contains(key)) {
            try {
                Binding<?> binding = injector.getBinding(key);
                bindings.add(binding);
                visitedKeys.add(key);
                keys.addAll(binding.acceptTargetVisitor(keyVisitor));
            }
            catch (ConfigurationException e) {
                System.out.println("Missing binding for : " + key);
                visitedKeys.add(key);
            }
        }
    }
    return bindings;
}