crazyboblee | abc4dd0 | 2007-02-01 01:44:36 +0000 | [diff] [blame] | 1 | /** |
| 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 | */ |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 16 | |
| 17 | package com.google.inject; |
| 18 | |
crazyboblee | 3a971f3 | 2007-03-02 16:53:12 +0000 | [diff] [blame] | 19 | import com.google.inject.binder.AnnotatedBindingBuilder; |
crazyboblee | 9e13952 | 2007-03-09 03:22:15 +0000 | [diff] [blame] | 20 | import com.google.inject.binder.AnnotatedConstantBindingBuilder; |
kevinb9n | 2394ca6 | 2007-04-20 14:33:40 +0000 | [diff] [blame] | 21 | import com.google.inject.binder.LinkedBindingBuilder; |
kevinb9n | cad2c2b | 2007-05-15 17:28:03 +0000 | [diff] [blame] | 22 | import com.google.inject.internal.Objects; |
crazyboblee | db395b2 | 2007-02-18 05:09:15 +0000 | [diff] [blame] | 23 | import com.google.inject.matcher.Matcher; |
crazyboblee | 9d573ed | 2007-02-20 19:23:10 +0000 | [diff] [blame] | 24 | import com.google.inject.spi.SourceProviders; |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 25 | import com.google.inject.spi.TypeConverter; |
crazyboblee | 1c4d3e3 | 2007-02-15 04:24:13 +0000 | [diff] [blame] | 26 | import java.lang.annotation.Annotation; |
crazyboblee | db395b2 | 2007-02-18 05:09:15 +0000 | [diff] [blame] | 27 | import java.lang.reflect.Method; |
dan.halem | d172726 | 2008-04-01 23:02:08 +0000 | [diff] [blame] | 28 | import org.aopalliance.intercept.MethodInterceptor; |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 29 | |
| 30 | /** |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 31 | * 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: |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 35 | * |
| 36 | * <pre> |
crazyboblee | 278ee4d | 2007-02-15 19:23:13 +0000 | [diff] [blame] | 37 | * import static com.google.inject.Names.named; |
| 38 | * |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 39 | * public class MyModule extends AbstractModule { |
| 40 | * protected void configure() { |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 41 | * bind(Foo.class).to(FooImpl.class).in(Scopes.SINGLETON); |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 42 | * bind(BarImpl.class); |
| 43 | * link(Bar.class).to(BarImpl.class); |
crazyboblee | 278ee4d | 2007-02-15 19:23:13 +0000 | [diff] [blame] | 44 | * bindConstant(named("port")).to(8080); |
crazyboblee | 235d068 | 2007-01-31 02:25:21 +0000 | [diff] [blame] | 45 | * } |
| 46 | * } |
| 47 | * </pre> |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 48 | * |
| 49 | * @author crazybob@google.com (Bob Lee) |
| 50 | */ |
| 51 | public abstract class AbstractModule implements Module { |
| 52 | |
crazyboblee | 9d573ed | 2007-02-20 19:23:10 +0000 | [diff] [blame] | 53 | static { |
| 54 | SourceProviders.skip(AbstractModule.class); |
| 55 | } |
| 56 | |
crazyboblee | 81303cd | 2007-05-09 00:44:37 +0000 | [diff] [blame] | 57 | Binder binder; |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 58 | |
crazyboblee | 9d573ed | 2007-02-20 19:23:10 +0000 | [diff] [blame] | 59 | public final synchronized void configure(Binder builder) { |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 60 | try { |
crazyboblee | 81303cd | 2007-05-09 00:44:37 +0000 | [diff] [blame] | 61 | if (this.binder != null) { |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 62 | throw new IllegalStateException("Re-entry is not allowed."); |
| 63 | } |
crazyboblee | 81303cd | 2007-05-09 00:44:37 +0000 | [diff] [blame] | 64 | this.binder = Objects.nonNull(builder, "builder"); |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 65 | |
| 66 | configure(); |
| 67 | |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 68 | } |
| 69 | finally { |
crazyboblee | 81303cd | 2007-05-09 00:44:37 +0000 | [diff] [blame] | 70 | this.binder = null; |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
crazyboblee | 9d573ed | 2007-02-20 19:23:10 +0000 | [diff] [blame] | 75 | * Configures a {@link Binder} via the exposed methods. |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 76 | */ |
| 77 | protected abstract void configure(); |
| 78 | |
| 79 | /** |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 80 | * Gets direct access to the underlying {@code Binder}. |
crazyboblee | c2fcc43 | 2007-02-03 11:32:48 +0000 | [diff] [blame] | 81 | */ |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 82 | protected Binder binder() { |
crazyboblee | 81303cd | 2007-05-09 00:44:37 +0000 | [diff] [blame] | 83 | return binder; |
crazyboblee | c2fcc43 | 2007-02-03 11:32:48 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
crazyboblee | 9d573ed | 2007-02-20 19:23:10 +0000 | [diff] [blame] | 87 | * @see Binder#bindScope(Class, Scope) |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 88 | */ |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 89 | protected void bindScope(Class<? extends Annotation> scopeAnnotation, |
crazyboblee | b1f2e68 | 2007-02-15 05:14:32 +0000 | [diff] [blame] | 90 | Scope scope) { |
crazyboblee | 81303cd | 2007-05-09 00:44:37 +0000 | [diff] [blame] | 91 | binder.bindScope(scopeAnnotation, scope); |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
crazyboblee | 9d573ed | 2007-02-20 19:23:10 +0000 | [diff] [blame] | 95 | * @see Binder#bind(Key) |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 96 | */ |
crazyboblee | 3a971f3 | 2007-03-02 16:53:12 +0000 | [diff] [blame] | 97 | protected <T> LinkedBindingBuilder<T> bind(Key<T> key) { |
crazyboblee | 81303cd | 2007-05-09 00:44:37 +0000 | [diff] [blame] | 98 | return binder.bind(key); |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
crazyboblee | 9d573ed | 2007-02-20 19:23:10 +0000 | [diff] [blame] | 102 | * @see Binder#bind(TypeLiteral) |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 103 | */ |
crazyboblee | 3a971f3 | 2007-03-02 16:53:12 +0000 | [diff] [blame] | 104 | protected <T> AnnotatedBindingBuilder<T> bind(TypeLiteral<T> typeLiteral) { |
crazyboblee | 81303cd | 2007-05-09 00:44:37 +0000 | [diff] [blame] | 105 | return binder.bind(typeLiteral); |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
crazyboblee | 9d573ed | 2007-02-20 19:23:10 +0000 | [diff] [blame] | 109 | * @see Binder#bind(Class) |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 110 | */ |
crazyboblee | 3a971f3 | 2007-03-02 16:53:12 +0000 | [diff] [blame] | 111 | protected <T> AnnotatedBindingBuilder<T> bind(Class<T> clazz) { |
crazyboblee | 81303cd | 2007-05-09 00:44:37 +0000 | [diff] [blame] | 112 | return binder.bind(clazz); |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
crazyboblee | 9e13952 | 2007-03-09 03:22:15 +0000 | [diff] [blame] | 116 | * @see Binder#bindConstant() |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 117 | */ |
crazyboblee | 9e13952 | 2007-03-09 03:22:15 +0000 | [diff] [blame] | 118 | protected AnnotatedConstantBindingBuilder bindConstant() { |
crazyboblee | 81303cd | 2007-05-09 00:44:37 +0000 | [diff] [blame] | 119 | return binder.bindConstant(); |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
crazyboblee | 9d573ed | 2007-02-20 19:23:10 +0000 | [diff] [blame] | 123 | * @see Binder#install(Module) |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 124 | */ |
crazyboblee | a379601 | 2007-02-03 11:43:40 +0000 | [diff] [blame] | 125 | protected void install(Module module) { |
crazyboblee | 81303cd | 2007-05-09 00:44:37 +0000 | [diff] [blame] | 126 | binder.install(module); |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 127 | } |
crazyboblee | 1c4d3e3 | 2007-02-15 04:24:13 +0000 | [diff] [blame] | 128 | |
| 129 | /** |
kevinb9n | 0af85cf | 2007-03-01 01:18:31 +0000 | [diff] [blame] | 130 | * @see Binder#addError(String, Object[]) |
| 131 | */ |
| 132 | protected void addError(String message, Object... arguments) { |
crazyboblee | 81303cd | 2007-05-09 00:44:37 +0000 | [diff] [blame] | 133 | binder.addError(message, arguments); |
kevinb9n | 0af85cf | 2007-03-01 01:18:31 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @see Binder#addError(Throwable) |
| 138 | */ |
| 139 | protected void addError(Throwable t) { |
crazyboblee | 81303cd | 2007-05-09 00:44:37 +0000 | [diff] [blame] | 140 | binder.addError(t); |
kevinb9n | 0af85cf | 2007-03-01 01:18:31 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
crazyboblee | 9d573ed | 2007-02-20 19:23:10 +0000 | [diff] [blame] | 144 | * @see Binder#requestStaticInjection(Class[]) |
crazyboblee | 1c4d3e3 | 2007-02-15 04:24:13 +0000 | [diff] [blame] | 145 | */ |
| 146 | protected void requestStaticInjection(Class<?>... types) { |
crazyboblee | 81303cd | 2007-05-09 00:44:37 +0000 | [diff] [blame] | 147 | binder.requestStaticInjection(types); |
crazyboblee | 1c4d3e3 | 2007-02-15 04:24:13 +0000 | [diff] [blame] | 148 | } |
crazyboblee | db395b2 | 2007-02-18 05:09:15 +0000 | [diff] [blame] | 149 | |
| 150 | /** |
crazyboblee | 9d573ed | 2007-02-20 19:23:10 +0000 | [diff] [blame] | 151 | * @see Binder#bindInterceptor(com.google.inject.matcher.Matcher, |
crazyboblee | db395b2 | 2007-02-18 05:09:15 +0000 | [diff] [blame] | 152 | * com.google.inject.matcher.Matcher, |
| 153 | * org.aopalliance.intercept.MethodInterceptor[]) |
| 154 | */ |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 155 | protected void bindInterceptor(Matcher<? super Class<?>> classMatcher, |
crazyboblee | db395b2 | 2007-02-18 05:09:15 +0000 | [diff] [blame] | 156 | Matcher<? super Method> methodMatcher, |
| 157 | MethodInterceptor... interceptors) { |
crazyboblee | 81303cd | 2007-05-09 00:44:37 +0000 | [diff] [blame] | 158 | binder.bindInterceptor(classMatcher, methodMatcher, interceptors); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * @see Binder#getProvider(Key) |
| 163 | */ |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 164 | protected <T> Provider<T> getProvider(Key<T> key) { |
crazyboblee | 81303cd | 2007-05-09 00:44:37 +0000 | [diff] [blame] | 165 | return binder.getProvider(key); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @see Binder#getProvider(Class) |
| 170 | */ |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 171 | protected <T> Provider<T> getProvider(Class<T> type) { |
crazyboblee | 81303cd | 2007-05-09 00:44:37 +0000 | [diff] [blame] | 172 | return binder.getProvider(type); |
crazyboblee | db395b2 | 2007-02-18 05:09:15 +0000 | [diff] [blame] | 173 | } |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 174 | |
| 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 | } |
crazyboblee | 041e933 | 2007-01-29 22:58:35 +0000 | [diff] [blame] | 189 | } |