Java Code Examples for com.google.inject.Binder#requestInjection()

The following examples show how to use com.google.inject.Binder#requestInjection() . 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: DefaultFactoryStrategy.java    From business with Mozilla Public License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void resolve(Binder binder) {
    FactoryModuleBuilder guiceFactoryBuilder = new FactoryModuleBuilder();
    for (Map.Entry<Type, Class<?>> classes : typeVariables.entries()) {
        Type producedType = classes.getKey();
        Class<? extends T> producedImplementationType = (Class<? extends T>) classes.getValue();

        Key<T> key = BindingUtils.resolveKey(injecteeClass, producedImplementationType, producedType);
        Provider<T> provider = new GenericGuiceProvider<>(injectedClass, producedImplementationType);
        binder.requestInjection(provider);
        binder.bind(key).toProvider(provider);
        guiceFactoryBuilder.implement(key, injectedClass);
    }

    // Assisted factory should not be bound twice
    if (bindGuiceFactory) {
        TypeLiteral<?> guiceAssistedFactory = TypeLiteral.get(newParameterizedType(FACTORY_CLASS, injectedClass));
        binder.install(guiceFactoryBuilder.build(guiceAssistedFactory));
    }
}
 
Example 2
Source File: GenericBindingStrategy.java    From seed with Mozilla Public License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private void bindKey(Binder binder, FactoryModuleBuilder guiceFactoryBuilder, Type[] params, Key<?> defaultKey) {
    // If a default key is provided use a linked binding to bind it
    if (defaultKey != null) {
        binder.bind(defaultKey.getTypeLiteral()).to((Key) defaultKey);
    }
    // Get the key to bind
    Key<T> key = BindingUtils.resolveKey(injecteeClass, genericImplClass, params);

    // Prepare the Guice provider
    Provider<?> provider = new GenericGuiceProvider<T>(genericImplClass, params);
    binder.requestInjection(provider);
    binder.bind(key).toProvider((Provider) provider);

    // Prepare the factory for assisted injection
    guiceFactoryBuilder.implement(key, (Class) genericImplClass);
}
 
Example 3
Source File: DefaultRepositoryStrategy.java    From business with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void resolve(Binder binder) {
    for (Class<? extends T> impl : implementations) {
        if (Arrays.stream(impl.getMethods()).anyMatch(m -> isAbstract(m.getModifiers()))) {
            LOGGER.warn("Skipping default repository implementation {}: abstract methods are still present",
                    impl.getName());
        } else {
            Key<T> key = BusinessUtils.getQualifier(impl)
                    .map(qualifier -> Key.get(repositoryInterface, qualifier))
                    .orElseThrow(() -> new IllegalStateException("Missing qualifier on implementation" + impl));

            if (defaultKey != null) {
                binder.bind(repositoryInterface).to(defaultKey);
            }

            Provider<T> provider = new GenericGuiceProvider<>(impl, generics);
            binder.requestInjection(provider);
            binder.bind(key).toProvider(provider);

            FactoryModuleBuilder guiceFactoryBuilder = new FactoryModuleBuilder();
            guiceFactoryBuilder.implement(key, impl);
            binder.install(guiceFactoryBuilder.build(
                    TypeLiteral.get(Types.newParameterizedType(FACTORY_CLASS, impl))
            ));
        }
    }
}
 
Example 4
Source File: TimedInterceptor.java    From attic-aurora with Apache License 2.0 5 votes vote down vote up
/**
 * Installs an interceptor in a guice {@link com.google.inject.Injector}, enabling
 * {@literal @Timed} method interception in guice-provided instances.  Requires that a
 * {@link TimeSeriesRepository} is bound elsewhere.
 *
 * @param binder a guice binder to require bindings against
 */
public static void bind(Binder binder) {
  Preconditions.checkNotNull(binder);

  Bindings.requireBinding(binder, TimeSeriesRepository.class);

  TimedInterceptor interceptor = new TimedInterceptor();
  binder.requestInjection(interceptor);
  binder.bindInterceptor(Matchers.any(), Matchers.annotatedWith(Timed.class), interceptor);
}
 
Example 5
Source File: AopModule.java    From attic-aurora with Apache License 2.0 5 votes vote down vote up
public static void bindThriftDecorator(
    Binder binder,
    Matcher<? super Class<?>> classMatcher,
    MethodInterceptor interceptor) {

  binder.bindInterceptor(
      classMatcher,
      Matchers.returns(Matchers.subclassesOf(Response.class)),
      interceptor);
  binder.requestInjection(interceptor);
}