| /** |
| * Copyright (C) 2006 Google Inc. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.google.inject; |
| |
| import com.google.inject.binder.BindingBuilder; |
| import com.google.inject.binder.ConstantBindingBuilder; |
| import com.google.inject.binder.LinkedBindingBuilder; |
| import com.google.inject.matcher.Matcher; |
| import com.google.inject.util.Objects; |
| import java.lang.annotation.Annotation; |
| import java.lang.reflect.Method; |
| import org.aopalliance.intercept.MethodInterceptor; |
| |
| /** |
| * A support class for {@link Module Modules} which reduces repetition and |
| * results in a more readable configuration. Simply extends this class, |
| * implement {@link #configure()}, and call the inherited methods which mirror |
| * those found in {@link BinderImpl}. For example: |
| * |
| * <pre> |
| * import static com.google.inject.Names.named; |
| * |
| * public class MyModule extends AbstractModule { |
| * protected void configure() { |
| * bind(Foo.class).to(FooImpl.class).in(Scopes.CONTAINER); |
| * bind(BarImpl.class); |
| * link(Bar.class).to(BarImpl.class); |
| * bindConstant(named("port")).to(8080); |
| * } |
| * } |
| * </pre> |
| * |
| * @author crazybob@google.com (Bob Lee) |
| */ |
| public abstract class AbstractModule implements Module { |
| |
| Binder builder; |
| |
| public final synchronized void configure(BinderImpl builder) { |
| try { |
| if (this.builder != null) { |
| throw new IllegalStateException("Re-entry is not allowed."); |
| } |
| this.builder = Objects.nonNull(builder, "builder"); |
| |
| configure(); |
| |
| } |
| finally { |
| this.builder = null; |
| } |
| } |
| |
| /** |
| * Configures a {@link BinderImpl} via the exposed methods. |
| */ |
| protected abstract void configure(); |
| |
| /** |
| * Gets the builder. |
| */ |
| protected Binder builder() { |
| return builder; |
| } |
| |
| /** |
| * @see BinderImpl#bindScope(Class, Scope) |
| */ |
| protected void scope(Class<? extends Annotation> scopeAnnotation, |
| Scope scope) { |
| builder.bindScope(scopeAnnotation, scope); |
| } |
| |
| /** |
| * @see BinderImpl#bind(Key) |
| */ |
| protected <T> BindingBuilder<T> bind(Key<T> key) { |
| return builder.bind(key); |
| } |
| |
| /** |
| * @see BinderImpl#bind(TypeLiteral) |
| */ |
| protected <T> BindingBuilder<T> bind( |
| TypeLiteral<T> typeLiteral) { |
| return builder.bind(typeLiteral); |
| } |
| |
| /** |
| * @see BinderImpl#bind(Class) |
| */ |
| protected <T> BindingBuilder<T> bind(Class<T> clazz) { |
| return builder.bind(clazz); |
| } |
| |
| /** |
| * @see BinderImpl#link(Key) |
| */ |
| protected <T> LinkedBindingBuilder<T> link(Key<T> key) { |
| return builder.link(key); |
| } |
| |
| /** |
| * @see BinderImpl#bindConstant(Class) |
| */ |
| protected ConstantBindingBuilder bindConstant( |
| Class<? extends Annotation> annotationType) { |
| return builder.bindConstant(annotationType); |
| } |
| |
| /** |
| * @see BinderImpl#bindConstant(java.lang.annotation.Annotation) |
| */ |
| protected ConstantBindingBuilder bindConstant( |
| Annotation annotation) { |
| return builder.bindConstant(annotation); |
| } |
| |
| /** |
| * @see BinderImpl#install(Module) |
| */ |
| protected void install(Module module) { |
| builder.install(module); |
| } |
| |
| /** |
| * @see BinderImpl#requestStaticInjection(Class[]) |
| */ |
| protected void requestStaticInjection(Class<?>... types) { |
| builder.requestStaticInjection(types); |
| } |
| |
| /** |
| * @see BinderImpl#bindInterceptor(com.google.inject.matcher.Matcher, |
| * com.google.inject.matcher.Matcher, |
| * org.aopalliance.intercept.MethodInterceptor[]) |
| */ |
| protected void intercept(Matcher<? super Class<?>> classMatcher, |
| Matcher<? super Method> methodMatcher, |
| MethodInterceptor... interceptors) { |
| builder.bindInterceptor(classMatcher, methodMatcher, interceptors); |
| } |
| } |