Package com.google.inject

Examples of com.google.inject.Binder


      }
    }

    public void install(Module module) {
      if (modules.add(module)) {
        Binder binder = this;
        // Update the module source for the new module
        if (!(module instanceof ProviderMethodsModule)) {
          moduleSource = getModuleSource(module);
        }
        if (module instanceof PrivateModule) {
          binder = binder.newPrivateBinder();
        }     
        try {
          module.configure(binder);
        } catch (RuntimeException e) {
          Collection<Message> messages = Errors.getMessagesFromThrowable(e);
          if (!messages.isEmpty()) {
            elements.addAll(messages);
          } else {
            addError(e);
          }
        }
        binder.install(ProviderMethodsModule.forModule(module));
        // We are done with this module, so undo module source change
        if (!(module instanceof ProviderMethodsModule)) {
          moduleSource = moduleSource.getParent();
        }
      }
View Full Code Here


    this.ginModule = ginModule;
    this.bindings = bindings;
  }

  public void configure() {
    Binder binder = binder().skipSources(PrivateGinModuleAdapter.class,
        BinderAdapter.class, PrivateBinderAdapter.class, PrivateGinModule.class);

    ginModule.configure(new PrivateBinderAdapter((PrivateBinder) binder,
        bindings == null ? null : bindings.createChildGinjectorBindings(ginModule.getClass())));

    // Install provider methods from the GinModule
    binder.install(ProviderMethodsModule.forObject(ginModule));
  }
View Full Code Here

      }
    }

    public void install(Module module) {
      if (modules.add(module)) {
        Binder binder = this;
        if (module instanceof PrivateModule) {
          binder = binder.newPrivateBinder();
        }

        try {
          module.configure(binder);
        } catch (RuntimeException e) {
          Collection<Message> messages = Errors.getMessagesFromThrowable(e);
          if (!messages.isEmpty()) {
            elements.addAll(messages);
          } else {
            addError(e);
          }
        }
        binder.install(ProviderMethodsModule.forModule(module));
      }
    }
View Full Code Here

  /**
   * Execute this target against the linked binding builder.
   */
  protected <T> ScopedBindingBuilder bindKeyToTarget(
      final Binding<T> binding, Binder binder, final Key<T> key) {
    final Binder sourcedBinder = binder.withSource(binding.getSource());

    return binding.acceptTargetVisitor(new BindingTargetVisitor<T, ScopedBindingBuilder>() {
      public ScopedBindingBuilder visit(InstanceBinding<? extends T> binding) {
        sourcedBinder.bind(key).toInstance(binding.getInstance());
        return null;
      }

      public ScopedBindingBuilder visit(ProviderInstanceBinding<? extends T> binding) {
        return sourcedBinder.bind(key).toProvider(binding.getProviderInstance());
      }

      public ScopedBindingBuilder visit(ProviderKeyBinding<? extends T> binding) {
        return sourcedBinder.bind(key).toProvider(binding.getProviderKey());
      }

      public ScopedBindingBuilder visit(LinkedKeyBinding<? extends T> binding) {
        return sourcedBinder.bind(key).to(binding.getLinkedKey());
      }

      public ScopedBindingBuilder visit(UntargettedBinding<? extends T> binding) {
        return sourcedBinder.bind(key);
      }

      public ScopedBindingBuilder visit(ExposedBinding<? extends T> binding) {
        throw new IllegalArgumentException("Non-module element");
      }
View Full Code Here

  Module bindTo(final Key<T> returnValueKey) {
    return new PrivateModule() {
      @SuppressWarnings({ "unchecked", "RedundantCast" })
      // raw keys are necessary for the args array and return value
      @Override protected void configure() {
        Binder binder = binder();

        for (Key<?> configKey : declaredKeysToActualBindings.keySet()) {
          KeyOrInstanceUnionWithLabel<?> actualBinding =
              declaredKeysToActualBindings.get(configKey);

          if (actualBinding.key != null) {
            binder.bind((Key) configKey).to(actualBinding.key);
          } else {
            binder.bind((Key) configKey).toInstance(actualBinding.instance);
          }
        }

        // We don't use .toConstructor() to support users of Guice 2.0
        if (returnValueKey.equals(Key.get(implementationType))) {
          binder.bind(returnValueKey);
        } else {
          binder.bind(returnValueKey).to(implementationType);
        }

        expose(returnValueKey);
      }
    };
View Full Code Here

      }
    }

    public void install(Module module) {
      if (modules.add(module)) {
        Binder binder = this;
        if (module instanceof PrivateModule) {
          binder = binder.newPrivateBinder();
        }

        try {
          module.configure(binder);
        } catch (RuntimeException e) {
          Collection<Message> messages = Errors.getMessagesFromThrowable(e);
          if (!messages.isEmpty()) {
            elements.addAll(messages);
          } else {
            addError(e);
          }
        }
        binder.install(ProviderMethodsModule.forModule(module));
      }
    }
View Full Code Here

            @SuppressWarnings({"unchecked", "rawtypes"})
            @Override
            protected void configure()
            {
               Collection<ComponentAdapter<?>> adapters = delegate.getComponentAdapters();
               Binder binder = binder();
               for (ComponentAdapter<?> adapter : adapters)
               {
                  Object key = adapter.getComponentKey();
                  Class<?> type;
                  Annotation annotation = null;
                  Class<? extends Annotation> annotationType = null;
                  if (key instanceof Class<?> && !((Class<?>)key).isAnnotation())
                  {
                     type = (Class<?>)key;
                  }
                  else
                  {
                     if (key instanceof String)
                     {
                        annotation = Names.named((String)key);
                     }
                     else if (key instanceof Class<?>)
                     {
                        annotationType = (Class<? extends Annotation>)key;
                     }
                     type = adapter.getComponentImplementation();
                  }
                  if (annotation == null && annotationType == null)
                  {
                     binder.bind(type).toProvider(new ComponentAdapterProvider(type, adapter));
                  }
                  else
                  {
                     // As we don't know the type, we will bind it for each super classes and interfaces too
                     ComponentAdapterProvider provider = new ComponentAdapterProvider(type, adapter);
View Full Code Here

  private static final Logger log = new Logger(JettyServerModule.class);

  @Override
  protected void configureServlets()
  {
    Binder binder = binder();

    JsonConfigProvider.bind(binder, "druid.server.http", ServerConfig.class);

    binder.bind(GuiceContainer.class).to(DruidGuiceContainer.class);
    binder.bind(DruidGuiceContainer.class).in(Scopes.SINGLETON);
    serve("/*").with(DruidGuiceContainer.class);

    Jerseys.addResource(binder, StatusResource.class);
    binder.bind(StatusResource.class).in(LazySingleton.class);
  }
View Full Code Here

public class Antlr4RuntimeModuleTest {

  @SuppressWarnings("unchecked")
  @Test
  public void configure() {
    Binder binder = createNiceMock(Binder.class);
    AnnotatedBindingBuilder<LangFactory> bindLangFactory = createMock(AnnotatedBindingBuilder.class);
    AnnotatedBindingBuilder<ILinkingDiagnosticMessageProvider.Extended> bindLinkingDMP = createMock(AnnotatedBindingBuilder.class);
    AnnotatedBindingBuilder<ILaunchManager> bindLaunchManager = createMock(AnnotatedBindingBuilder.class);
    final ILaunchManager launchManager = createMock(ILaunchManager.class);

    expect(binder.bind(LangFactory.class)).andReturn(bindLangFactory);
    expect(binder.bind(ILinkingDiagnosticMessageProvider.Extended.class)).andReturn(bindLinkingDMP);
    expect(binder.bind(ILaunchManager.class)).andReturn(bindLaunchManager);

    bindLangFactory.toInstance(LangFactory.eINSTANCE);

    expect(bindLinkingDMP.to(Antlr4MissingReferenceMessageProvider.class)).andReturn(null);
View Full Code Here

    final Key<?> returnType = returnTypesByMethod.get(method);

    Module assistedModule = new AbstractModule() {
      @SuppressWarnings("unchecked") // raw keys are necessary for the args array and return value
      protected void configure() {
        Binder binder = binder().withSource(method);

        int p = 0;
        for (Key<?> paramKey : paramTypes.get(method)) {
          // Wrap in a Provider to cover null, and to prevent Guice from injecting the parameter
          binder.bind((Key) paramKey).toProvider(Providers.of(args[p++]));
        }

        if (producedType != null && !returnType.equals(producedType)) {
          binder.bind(returnType).to((Key) producedType);
        } else {
          binder.bind(returnType);
        }
      }
    };

    Injector forCreate = injector.createChildInjector(assistedModule);
View Full Code Here

TOP

Related Classes of com.google.inject.Binder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.