blob: cc29e512657244b2e002b74e1776bc38041ef5fd [file] [log] [blame]
crazybobleeabc4dd02007-02-01 01:44:36 +00001/**
2 * Copyright (C) 2006 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
crazyboblee041e9332007-01-29 22:58:35 +000016
17package com.google.inject;
18
crazyboblee3a971f32007-03-02 16:53:12 +000019import com.google.inject.binder.AnnotatedBindingBuilder;
crazyboblee9e139522007-03-09 03:22:15 +000020import com.google.inject.binder.AnnotatedConstantBindingBuilder;
kevinb9n2394ca62007-04-20 14:33:40 +000021import com.google.inject.binder.LinkedBindingBuilder;
kevinb9ncad2c2b2007-05-15 17:28:03 +000022import com.google.inject.internal.Objects;
crazybobleedb395b22007-02-18 05:09:15 +000023import com.google.inject.matcher.Matcher;
crazyboblee9d573ed2007-02-20 19:23:10 +000024import com.google.inject.spi.SourceProviders;
crazyboblee7c9d7792007-09-09 03:41:05 +000025import com.google.inject.spi.TypeConverter;
crazyboblee1c4d3e32007-02-15 04:24:13 +000026import java.lang.annotation.Annotation;
crazybobleedb395b22007-02-18 05:09:15 +000027import java.lang.reflect.Method;
dan.halemd1727262008-04-01 23:02:08 +000028import org.aopalliance.intercept.MethodInterceptor;
crazyboblee041e9332007-01-29 22:58:35 +000029
30/**
crazyboblee0bfdbc62007-02-20 21:47:19 +000031 * A support class for {@link Module}s which reduces repetition and results in
32 * a more readable configuration. Simply extend this class, implement {@link
33 * #configure()}, and call the inherited methods which mirror those found in
34 * {@link Binder}. For example:
crazyboblee235d0682007-01-31 02:25:21 +000035 *
36 * <pre>
crazyboblee278ee4d2007-02-15 19:23:13 +000037 * import static com.google.inject.Names.named;
38 *
crazyboblee235d0682007-01-31 02:25:21 +000039 * public class MyModule extends AbstractModule {
40 * protected void configure() {
kevinb9na2915a92007-02-28 06:20:30 +000041 * bind(Foo.class).to(FooImpl.class).in(Scopes.SINGLETON);
crazyboblee235d0682007-01-31 02:25:21 +000042 * bind(BarImpl.class);
43 * link(Bar.class).to(BarImpl.class);
crazyboblee278ee4d2007-02-15 19:23:13 +000044 * bindConstant(named("port")).to(8080);
crazyboblee235d0682007-01-31 02:25:21 +000045 * }
46 * }
47 * </pre>
crazyboblee041e9332007-01-29 22:58:35 +000048 *
49 * @author crazybob@google.com (Bob Lee)
50 */
51public abstract class AbstractModule implements Module {
52
crazyboblee9d573ed2007-02-20 19:23:10 +000053 static {
54 SourceProviders.skip(AbstractModule.class);
55 }
56
crazyboblee81303cd2007-05-09 00:44:37 +000057 Binder binder;
crazyboblee041e9332007-01-29 22:58:35 +000058
crazyboblee9d573ed2007-02-20 19:23:10 +000059 public final synchronized void configure(Binder builder) {
crazyboblee041e9332007-01-29 22:58:35 +000060 try {
crazyboblee81303cd2007-05-09 00:44:37 +000061 if (this.binder != null) {
crazyboblee041e9332007-01-29 22:58:35 +000062 throw new IllegalStateException("Re-entry is not allowed.");
63 }
crazyboblee81303cd2007-05-09 00:44:37 +000064 this.binder = Objects.nonNull(builder, "builder");
crazyboblee041e9332007-01-29 22:58:35 +000065
66 configure();
67
kevinb9na99dca72007-02-11 04:48:57 +000068 }
69 finally {
crazyboblee81303cd2007-05-09 00:44:37 +000070 this.binder = null;
crazyboblee041e9332007-01-29 22:58:35 +000071 }
72 }
73
74 /**
crazyboblee9d573ed2007-02-20 19:23:10 +000075 * Configures a {@link Binder} via the exposed methods.
crazyboblee041e9332007-01-29 22:58:35 +000076 */
77 protected abstract void configure();
78
79 /**
crazyboblee0bfdbc62007-02-20 21:47:19 +000080 * Gets direct access to the underlying {@code Binder}.
crazybobleec2fcc432007-02-03 11:32:48 +000081 */
crazyboblee0bfdbc62007-02-20 21:47:19 +000082 protected Binder binder() {
crazyboblee81303cd2007-05-09 00:44:37 +000083 return binder;
crazybobleec2fcc432007-02-03 11:32:48 +000084 }
85
86 /**
crazyboblee9d573ed2007-02-20 19:23:10 +000087 * @see Binder#bindScope(Class, Scope)
crazyboblee041e9332007-01-29 22:58:35 +000088 */
crazyboblee0bfdbc62007-02-20 21:47:19 +000089 protected void bindScope(Class<? extends Annotation> scopeAnnotation,
crazybobleeb1f2e682007-02-15 05:14:32 +000090 Scope scope) {
crazyboblee81303cd2007-05-09 00:44:37 +000091 binder.bindScope(scopeAnnotation, scope);
crazyboblee041e9332007-01-29 22:58:35 +000092 }
93
94 /**
crazyboblee9d573ed2007-02-20 19:23:10 +000095 * @see Binder#bind(Key)
crazyboblee041e9332007-01-29 22:58:35 +000096 */
crazyboblee3a971f32007-03-02 16:53:12 +000097 protected <T> LinkedBindingBuilder<T> bind(Key<T> key) {
crazyboblee81303cd2007-05-09 00:44:37 +000098 return binder.bind(key);
crazyboblee041e9332007-01-29 22:58:35 +000099 }
100
101 /**
crazyboblee9d573ed2007-02-20 19:23:10 +0000102 * @see Binder#bind(TypeLiteral)
crazyboblee041e9332007-01-29 22:58:35 +0000103 */
crazyboblee3a971f32007-03-02 16:53:12 +0000104 protected <T> AnnotatedBindingBuilder<T> bind(TypeLiteral<T> typeLiteral) {
crazyboblee81303cd2007-05-09 00:44:37 +0000105 return binder.bind(typeLiteral);
crazyboblee041e9332007-01-29 22:58:35 +0000106 }
107
108 /**
crazyboblee9d573ed2007-02-20 19:23:10 +0000109 * @see Binder#bind(Class)
crazyboblee041e9332007-01-29 22:58:35 +0000110 */
crazyboblee3a971f32007-03-02 16:53:12 +0000111 protected <T> AnnotatedBindingBuilder<T> bind(Class<T> clazz) {
crazyboblee81303cd2007-05-09 00:44:37 +0000112 return binder.bind(clazz);
crazyboblee041e9332007-01-29 22:58:35 +0000113 }
114
115 /**
crazyboblee9e139522007-03-09 03:22:15 +0000116 * @see Binder#bindConstant()
crazyboblee041e9332007-01-29 22:58:35 +0000117 */
crazyboblee9e139522007-03-09 03:22:15 +0000118 protected AnnotatedConstantBindingBuilder bindConstant() {
crazyboblee81303cd2007-05-09 00:44:37 +0000119 return binder.bindConstant();
crazyboblee041e9332007-01-29 22:58:35 +0000120 }
121
122 /**
crazyboblee9d573ed2007-02-20 19:23:10 +0000123 * @see Binder#install(Module)
crazyboblee041e9332007-01-29 22:58:35 +0000124 */
crazybobleea3796012007-02-03 11:43:40 +0000125 protected void install(Module module) {
crazyboblee81303cd2007-05-09 00:44:37 +0000126 binder.install(module);
crazyboblee041e9332007-01-29 22:58:35 +0000127 }
crazyboblee1c4d3e32007-02-15 04:24:13 +0000128
129 /**
kevinb9n0af85cf2007-03-01 01:18:31 +0000130 * @see Binder#addError(String, Object[])
131 */
132 protected void addError(String message, Object... arguments) {
crazyboblee81303cd2007-05-09 00:44:37 +0000133 binder.addError(message, arguments);
kevinb9n0af85cf2007-03-01 01:18:31 +0000134 }
135
136 /**
137 * @see Binder#addError(Throwable)
138 */
139 protected void addError(Throwable t) {
crazyboblee81303cd2007-05-09 00:44:37 +0000140 binder.addError(t);
kevinb9n0af85cf2007-03-01 01:18:31 +0000141 }
142
143 /**
crazyboblee9d573ed2007-02-20 19:23:10 +0000144 * @see Binder#requestStaticInjection(Class[])
crazyboblee1c4d3e32007-02-15 04:24:13 +0000145 */
146 protected void requestStaticInjection(Class<?>... types) {
crazyboblee81303cd2007-05-09 00:44:37 +0000147 binder.requestStaticInjection(types);
crazyboblee1c4d3e32007-02-15 04:24:13 +0000148 }
crazybobleedb395b22007-02-18 05:09:15 +0000149
150 /**
crazyboblee9d573ed2007-02-20 19:23:10 +0000151 * @see Binder#bindInterceptor(com.google.inject.matcher.Matcher,
crazybobleedb395b22007-02-18 05:09:15 +0000152 * com.google.inject.matcher.Matcher,
153 * org.aopalliance.intercept.MethodInterceptor[])
154 */
crazyboblee0bfdbc62007-02-20 21:47:19 +0000155 protected void bindInterceptor(Matcher<? super Class<?>> classMatcher,
crazybobleedb395b22007-02-18 05:09:15 +0000156 Matcher<? super Method> methodMatcher,
157 MethodInterceptor... interceptors) {
crazyboblee81303cd2007-05-09 00:44:37 +0000158 binder.bindInterceptor(classMatcher, methodMatcher, interceptors);
159 }
160
161 /**
162 * @see Binder#getProvider(Key)
163 */
crazyboblee7c9d7792007-09-09 03:41:05 +0000164 protected <T> Provider<T> getProvider(Key<T> key) {
crazyboblee81303cd2007-05-09 00:44:37 +0000165 return binder.getProvider(key);
166 }
167
168 /**
169 * @see Binder#getProvider(Class)
170 */
crazyboblee7c9d7792007-09-09 03:41:05 +0000171 protected <T> Provider<T> getProvider(Class<T> type) {
crazyboblee81303cd2007-05-09 00:44:37 +0000172 return binder.getProvider(type);
crazybobleedb395b22007-02-18 05:09:15 +0000173 }
crazyboblee7c9d7792007-09-09 03:41:05 +0000174
175 /**
176 * @see Binder#convertToTypes
177 */
178 protected void convertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher,
179 TypeConverter converter) {
180 binder.convertToTypes(typeMatcher, converter);
181 }
182
183 /**
184 * @see Binder#currentStage()
185 */
186 protected Stage currentStage() {
187 return binder.currentStage();
188 }
crazyboblee041e9332007-01-29 22:58:35 +0000189}