All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.inject.AbstractModule Maven / Gradle / Ivy

package com.google.inject;

import com.google.inject.binder.AnnotatedBindingBuilder;
import com.google.inject.binder.AnnotatedConstantBindingBuilder;
import com.google.inject.binder.LinkedBindingBuilder;
import com.google.inject.matcher.Matcher;
import com.google.inject.spi.Message;
import com.google.inject.spi.ProvisionListener;
import com.google.inject.spi.TypeConverter;
import com.google.inject.spi.TypeListener;

import java.lang.annotation.Annotation;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

/**
 * A support class for {@link Module}s which reduces repetition and results in
 * a more readable configuration. Simply extend this class, implement {@link
 * #configure()}, and call the inherited methods which mirror those found in
 * {@link Binder}. For example:
 *
 * 
 * public class MyModule extends AbstractModule {
 *   protected void configure() {
 *     bind(Service.class).to(ServiceImpl.class).in(Singleton.class);
 *     bind(CreditCardPaymentService.class);
 *     bind(PaymentService.class).to(CreditCardPaymentService.class);
 *     bindConstant().annotatedWith(Names.named("port")).to(8080);
 *   }
 * }
 * 
* */ public abstract class AbstractModule implements Module { Binder binder; @Override public final synchronized void configure(Binder builder) { checkState(this.binder == null, "Re-entry is not allowed."); this.binder = checkNotNull(builder, "builder"); try { configure(); } finally { this.binder = null; } } /** * Configures a {@link Binder} via the exposed methods. */ protected abstract void configure(); /** * Gets direct access to the underlying {@code Binder}. */ protected Binder binder() { checkState(binder != null, "The binder can only be used inside configure()"); return binder; } /** * @see Binder#bindScope(Class, Scope) */ protected void bindScope(Class scopeAnnotation, Scope scope) { binder().bindScope(scopeAnnotation, scope); } /** * @see Binder#bind(Key) */ protected LinkedBindingBuilder bind(Key key) { return binder().bind(key); } /** * @see Binder#bind(TypeLiteral) */ protected AnnotatedBindingBuilder bind(TypeLiteral typeLiteral) { return binder().bind(typeLiteral); } /** * @see Binder#bind(Class) */ protected AnnotatedBindingBuilder bind(Class clazz) { return binder().bind(clazz); } /** * @see Binder#bindConstant() */ protected AnnotatedConstantBindingBuilder bindConstant() { return binder().bindConstant(); } /** * @see Binder#install(Module) */ protected void install(Module module) { binder().install(module); } /** * @see Binder#addError(String, Object[]) */ protected void addError(String message, Object... arguments) { binder().addError(message, arguments); } /** * @see Binder#addError(Throwable) */ protected void addError(Throwable t) { binder().addError(t); } /** * @see Binder#addError(Message) */ protected void addError(Message message) { binder().addError(message); } /** * @see Binder#requestInjection(Object) */ protected void requestInjection(Object instance) { binder().requestInjection(instance); } /** * @see Binder#requestStaticInjection(Class[]) */ protected void requestStaticInjection(Class... types) { binder().requestStaticInjection(types); } /** * Adds a dependency from this module to {@code key}. When the injector is * created, Guice will report an error if {@code key} cannot be injected. * Note that this requirement may be satisfied by implicit binding, such as * a public no-arguments constructor. */ protected void requireBinding(Key key) { binder().getProvider(key); } /** * Adds a dependency from this module to {@code type}. When the injector is * created, Guice will report an error if {@code type} cannot be injected. * Note that this requirement may be satisfied by implicit binding, such as * a public no-arguments constructor. */ protected void requireBinding(Class type) { binder().getProvider(type); } /** * @see Binder#getProvider(Key) */ protected Provider getProvider(Key key) { return binder().getProvider(key); } /** * @see Binder#getProvider(Class) */ protected Provider getProvider(Class type) { return binder().getProvider(type); } /** * @see Binder#convertToTypes */ protected void convertToTypes(Matcher> typeMatcher, TypeConverter converter) { binder().convertToTypes(typeMatcher, converter); } /** * @see Binder#currentStage() */ protected Stage currentStage() { return binder().currentStage(); } /** * @see Binder#getMembersInjector(Class) */ protected MembersInjector getMembersInjector(Class type) { return binder().getMembersInjector(type); } /** * @see Binder#getMembersInjector(TypeLiteral) */ protected MembersInjector getMembersInjector(TypeLiteral type) { return binder().getMembersInjector(type); } /** * @see Binder#bindListener(com.google.inject.matcher.Matcher, * com.google.inject.spi.TypeListener) */ protected void bindListener(Matcher> typeMatcher, TypeListener listener) { binder().bindListener(typeMatcher, listener); } /** * @see Binder#bindListener(Matcher, ProvisionListener...) */ protected void bindListener(Matcher> bindingMatcher, ProvisionListener... listener) { binder().bindListener(bindingMatcher, listener); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy