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

The following examples show how to use com.google.inject.Injector#getBindings() . 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: DefaultApplicationContextTest.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Test
public void test() {
    DefaultApplicationContext applicationContext = newApplicationContext();
    try {
        Injector injector = applicationContext.getInjector();
        Map<Key<?>, Binding<?>> bindings = injector.getBindings();
        for (Map.Entry<Key<?>, Binding<?>> e : bindings.entrySet()) {
            Key<?> key = e.getKey();
            Binding<?> binding = e.getValue();

            if (isPinpointBinding(key)) {
                boolean isSingletonScoped = Scopes.isSingleton(binding);
                Assert.assertTrue("Binding " + key + " is not Singleton scoped", isSingletonScoped);
            }
        }
        AgentInfoSender instance1 = injector.getInstance(AgentInfoSender.class);
        AgentInfoSender instance2 = injector.getInstance(AgentInfoSender.class);
        Assert.assertSame(instance1, instance2);
    } finally {
        applicationContext.close();
    }
}
 
Example 2
Source File: PreemptorModuleTest.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
@Test
public void testPreemptorDisabled() throws Exception {
  CliOptions options = new CliOptions();
  options.preemptor.enablePreemptor = false;
  options.preemptor.slotFinderModules = ImmutableList.of(FakeSlotFinder.class);

  Injector injector = createInjector(new PreemptorModule(options));

  control.replay();

  injector.getBindings();
  assertEquals(
      Optional.empty(),
      injector.getInstance(Preemptor.class).attemptPreemptionFor(
          IAssignedTask.build(new AssignedTask()),
          AttributeAggregate.empty(),
          storageUtil.mutableStoreProvider));
}
 
Example 3
Source File: AsyncModuleTest.java    From attic-aurora with Apache License 2.0 6 votes vote down vote up
@Test
public void testBindings() throws Exception {
  Injector injector = createInjector(new AsyncModule(new AsyncModule.Options()));

  control.replay();

  Set<Service> services = injector.getInstance(
      Key.get(new TypeLiteral<Set<Service>>() { }, AppStartup.class));
  for (Service service : services) {
    service.startAsync().awaitRunning();
  }

  injector.getBindings();

  assertEquals(
      ImmutableMap.of(
          RegisterGauges.TIMEOUT_QUEUE_GAUGE, 0,
          RegisterGauges.ASYNC_TASKS_GAUGE, 0L),
      statsProvider.getAllValues()
  );
}
 
Example 4
Source File: WebDispatch.java    From greenbeans with Apache License 2.0 5 votes vote down vote up
private Map<WebPath, Handler> createPathToHandler(Injector injector) {
	ImmutableMap.Builder<WebPath, Handler> builder = ImmutableMap.builder();

	Map<Key<?>, Binding<?>> bindings = injector.getBindings();
	for (Entry<Key<?>, Binding<?>> entry : bindings.entrySet()) {
		Key<?> key = entry.getKey();
		if (isWebService(key)) {
			introspectWebService(builder, entry.getValue());
		}
	}

	return builder.build();
}
 
Example 5
Source File: CodeBuilderFactory.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Create an injector that override the given injectors with the modules.
 *
 * @param originalInjector the original injector.
 * @param modules the overriding modules.
 * @return the new injector.
 */
public static Injector createOverridingInjector(Injector originalInjector, com.google.inject.Module module) {
	final Map<Key<?>, Binding<?>> bindings = originalInjector.getBindings();
	return Guice.createInjector(Modules2.mixin((binder) -> {
		for(Binding<?> binding: bindings.values()) {
			final Type typeLiteral = binding.getKey().getTypeLiteral().getType();
			if (typeLiteral != null) {
				final String typeName = typeLiteral.getTypeName();
				if (isValid(typeName)) {
					binding.applyTo(binder);
				}
			}
		}
	}, module));
}