Java Code Examples for com.google.inject.Binding#acceptTargetVisitor()

The following examples show how to use com.google.inject.Binding#acceptTargetVisitor() . 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: EndpointsModuleTest.java    From endpoints-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testConfigureEndpoints_withInterceptor() {
  Injector injector = Guice.createInjector(module, new InterceptorModule());

  Visitor visitor = new Visitor();
  for (Binding<?> binding : injector.getAllBindings().values()) {
    binding.acceptTargetVisitor(visitor);
  }

  assertEquals("Servlet not bound.", 1, visitor.linkedServlets.size());
  LinkedServletBinding servletBinding = visitor.linkedServlets.get(0);
  assertEquals("URL pattern does not match", URL_PATTERN, servletBinding.getPattern());
  assertEquals("Wrong initialization parameter provided", "false",
      servletBinding.getInitParams().get("restricted"));
  assertNotNull("SystemService named provider not found.", visitor.systemServiceProvider);

  ServiceMap serviceMap = (ServiceMap) visitor.systemServiceProvider.getProvider().get();
  Collection<Object> services = serviceMap.getServices();
  assertEquals("Incorrect number of services provided", 1, services.size());
  assertEquals("Service not enhanced correctly.", SERVICES.toArray()[0],
      ((Class<?>) services.toArray()[0].getClass()).getSuperclass());
}
 
Example 2
Source File: EndpointsModuleTest.java    From endpoints-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testConfigureEndpoints_withoutInterceptor() {
  Injector injector = Guice.createInjector(module, new DummyModule());

  Visitor visitor = new Visitor();
  for (Binding<?> binding : injector.getAllBindings().values()) {
    binding.acceptTargetVisitor(visitor);
  }

  assertEquals("Servlet not bound.", 1, visitor.linkedServlets.size());
  LinkedServletBinding servletBinding = visitor.linkedServlets.get(0);
  assertEquals("URL pattern does not match", URL_PATTERN, servletBinding.getPattern());
  assertEquals("Wrong initialization parameter provided", "false",
      servletBinding.getInitParams().get("restricted"));
  assertNotNull("SystemService named provider not found.", visitor.systemServiceProvider);

  ServiceMap serviceMap = (ServiceMap) visitor.systemServiceProvider.getProvider().get();
  Collection<Object> services = serviceMap.getServices();
  assertEquals("Incorrect number of services provided", 1, services.size());
  assertEquals("Service not provided correctly.", SERVICES.toArray()[0],
      services.toArray()[0].getClass());
}
 
Example 3
Source File: AdapterInjector.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Infers the type of the given adapter, evaluating either the related
 * bindings or the runtime type of the adapter.
 *
 * @param adapterKey
 *            The key of the map binding, which is an {@link AdapterKey}.
 * @param binding
 *            The binding related to the {@link AdapterKey}.
 * @param adapter
 *            The adapter instance.
 * @param issues
 *            A list of issues that might be filled with error and warning
 *            messages.
 *
 * @return A {@link TypeToken} representing the type of the given adapter
 *         instance.
 */
private TypeToken<?> inferAdapterType(AdapterKey<?> adapterKey,
		Binding<?> binding, Object adapter, List<String> issues) {
	// try to infer the actual type of the adapter from the binding
	TypeToken<?> bindingInferredType = binding
			.acceptTargetVisitor(ADAPTER_TYPE_INFERRER);

	// perform some sanity checks
	validateAdapterBinding(adapterKey, binding, adapter,
			bindingInferredType, issues);

	// The key type always takes precedence. Otherwise, if we could
	// infer a type from the binding, we use that before falling back to
	// inferring the type from the adapter instance itself.
	TypeToken<?> bindingKeyType = adapterKey.getKey();
	return bindingKeyType != null ? bindingKeyType
			: (bindingInferredType != null ? bindingInferredType
					: TypeToken.of(adapter.getClass()));
}
 
Example 4
Source File: DependencyCollector.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public <T> Object visit(Binding<T> binding) {
    requireKey(binding.getKey());
    explicitBindings.put(binding.getKey(), binding);
    binding.acceptTargetVisitor(new BindingVisitor<>());
    return super.visit(binding);
}
 
Example 5
Source File: EndpointsModuleTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
private void testServletClass(
    EndpointsModule module, Class<?> expectedClass) {
  Injector injector = Guice.createInjector(module, new DummyModule());

  Visitor visitor = new Visitor();
  for (Binding<?> binding : injector.getAllBindings().values()) {
    binding.acceptTargetVisitor(visitor);
  }

  assertEquals("Servlet not bound.", 1, visitor.linkedServlets.size());
  LinkedServletBinding servletBinding = visitor.linkedServlets.get(0);
  assertEquals("Wrong servlet class", expectedClass,
      servletBinding.getLinkedKey().getTypeLiteral().getRawType());
}
 
Example 6
Source File: EndpointsModuleTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfigureEndpoints_defaultInitParameters() {
  module = new EndpointsModule() {
    @Override
    protected void configureServlets() {
      super.configureServlets();
      configureEndpoints(URL_PATTERN, SERVICES, true);
    }
  };
  Injector injector = Guice.createInjector(module, new DummyModule());

  Visitor visitor = new Visitor();
  for (Binding<?> binding : injector.getAllBindings().values()) {
    binding.acceptTargetVisitor(visitor);
  }

  assertEquals("Servlet not bound.", 1, visitor.linkedServlets.size());
  LinkedServletBinding servletBinding = visitor.linkedServlets.get(0);
  assertEquals("URL pattern does not match", URL_PATTERN, servletBinding.getPattern());
  assertEquals("Wrong initialization parameter provided", "true",
      servletBinding.getInitParams().get("restricted"));
  assertNotNull("SystemService named provider not found.", visitor.systemServiceProvider);

  ServiceMap serviceMap = (ServiceMap) visitor.systemServiceProvider.getProvider().get();
  Collection<Object> services = serviceMap.getServices();
  assertEquals("Incorrect number of services provided", 1, services.size());
  assertEquals("Service not provided correctly.", SERVICES.toArray()[0],
      services.toArray()[0].getClass());
}
 
Example 7
Source File: AdapterInjector.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public TypeToken<?> visit(LinkedKeyBinding<? extends Object> binding) {
	Binding<?> linkedKeyBinding = injector
			.getBinding(binding.getLinkedKey());
	return linkedKeyBinding.acceptTargetVisitor(this);
}
 
Example 8
Source File: GuiceElementVisitor.java    From dropwizard-guicey with MIT License 4 votes vote down vote up
@Override
public <T> BindingDeclaration visit(final Binding<T> binding) {
    return binding.acceptTargetVisitor(BINDING_VISITOR);
}