limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +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 | */ |
| 16 | |
| 17 | package com.google.inject; |
| 18 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 20 | import com.google.common.collect.Iterables; |
| 21 | import com.google.common.collect.Lists; |
| 22 | import com.google.inject.Reflection.Factory; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 23 | import static com.google.inject.Scopes.SINGLETON; |
| 24 | import com.google.inject.commands.Command; |
| 25 | import com.google.inject.commands.CommandRecorder; |
| 26 | import com.google.inject.commands.FutureInjector; |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 27 | import com.google.inject.internal.Errors; |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 28 | import com.google.inject.internal.ErrorsException; |
limpbizkit | 2b93168 | 2008-04-21 00:59:34 +0000 | [diff] [blame] | 29 | import com.google.inject.internal.Stopwatch; |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 30 | import com.google.inject.spi.InjectionPoint; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 31 | import com.google.inject.spi.SourceProviders; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 32 | import java.lang.reflect.Member; |
limpbizkit | f44e9cc | 2008-03-26 06:33:29 +0000 | [diff] [blame] | 33 | import java.util.List; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 34 | import java.util.logging.Logger; |
| 35 | |
| 36 | /** |
| 37 | * Builds a dependency injection {@link Injector}. |
| 38 | * |
| 39 | * @author crazybob@google.com (Bob Lee) |
| 40 | * @author jessewilson@google.com (Jesse Wilson) |
| 41 | */ |
| 42 | class InjectorBuilder { |
| 43 | |
| 44 | private final Stopwatch stopwatch = new Stopwatch(); |
| 45 | |
| 46 | private Injector parent; |
| 47 | private Stage stage; |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 48 | private Factory reflectionFactory = new RuntimeReflectionFactory(); |
| 49 | private final List<Module> modules = Lists.newLinkedList(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 50 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 51 | private InjectorImpl injector; |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 52 | private Errors errors = new Errors(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 53 | |
| 54 | private final FutureInjector futureInjector = new FutureInjector(); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 55 | private final List<Command> commands = Lists.newArrayList(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 56 | |
| 57 | private BindCommandProcessor bindCommandProcesor; |
| 58 | private RequestStaticInjectionCommandProcessor requestStaticInjectionCommandProcessor; |
| 59 | |
| 60 | /** |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 61 | * @param stage we're running in. If the stage is {@link Stage#PRODUCTION}, we will eagerly load |
| 62 | * singletons. |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 63 | */ |
| 64 | InjectorBuilder stage(Stage stage) { |
| 65 | this.stage = stage; |
| 66 | return this; |
| 67 | } |
| 68 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 69 | InjectorBuilder usingReflectionFactory(Factory reflectionFactory) { |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 70 | this.reflectionFactory = reflectionFactory; |
| 71 | return this; |
| 72 | } |
| 73 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 74 | InjectorBuilder parentInjector(Injector parent) { |
| 75 | this.parent = parent; |
| 76 | return this; |
| 77 | } |
| 78 | |
| 79 | InjectorBuilder addModules(Iterable<? extends Module> modules) { |
| 80 | for (Module module : modules) { |
| 81 | this.modules.add(module); |
| 82 | } |
| 83 | return this; |
| 84 | } |
| 85 | |
| 86 | Injector build() { |
| 87 | if (injector != null) { |
| 88 | throw new AssertionError("Already built, builders are not reusable."); |
| 89 | } |
| 90 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 91 | injector = new InjectorImpl(parent); |
limpbizkit | 7f8eda0 | 2008-03-26 01:11:16 +0000 | [diff] [blame] | 92 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 93 | modules.add(0, new BuiltInModule(injector, stage)); |
| 94 | |
| 95 | CommandRecorder commandRecorder = new CommandRecorder(futureInjector); |
| 96 | commandRecorder.setCurrentStage(stage); |
| 97 | commands.addAll(commandRecorder.recordCommands(modules)); |
| 98 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 99 | buildCoreInjector(); |
| 100 | |
| 101 | validate(); |
| 102 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 103 | errors.throwCreationExceptionIfErrorsExist(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 104 | |
| 105 | // If we're in the tool stage, stop here. Don't eagerly inject or load |
| 106 | // anything. |
| 107 | if (stage == Stage.TOOL) { |
| 108 | // TODO: Wrap this and prevent usage of anything besides getBindings(). |
| 109 | return injector; |
| 110 | } |
| 111 | |
| 112 | fulfillInjectionRequests(); |
| 113 | |
| 114 | if (!commands.isEmpty()) { |
| 115 | throw new AssertionError("Failed to execute " + commands); |
| 116 | } |
| 117 | |
| 118 | return injector; |
| 119 | } |
| 120 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 121 | /** Builds the injector. */ |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 122 | private void buildCoreInjector() { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 123 | new ErrorsCommandProcessor(errors) |
limpbizkit | ad94bcb | 2008-04-28 00:22:43 +0000 | [diff] [blame] | 124 | .processCommands(commands); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 125 | |
| 126 | BindInterceptorCommandProcessor bindInterceptorCommandProcessor |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 127 | = new BindInterceptorCommandProcessor(errors); |
limpbizkit | ad94bcb | 2008-04-28 00:22:43 +0000 | [diff] [blame] | 128 | bindInterceptorCommandProcessor.processCommands(commands); |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 129 | ConstructionProxyFactory proxyFactory = bindInterceptorCommandProcessor.createProxyFactory(); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 130 | injector.reflection = reflectionFactory.create(proxyFactory); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 131 | stopwatch.resetAndLog("Interceptors creation"); |
| 132 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 133 | new ScopesCommandProcessor(errors, injector.scopes).processCommands(commands); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 134 | stopwatch.resetAndLog("Scopes creation"); |
| 135 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 136 | new ConvertToTypesCommandProcessor(errors, injector.converters).processCommands(commands); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 137 | stopwatch.resetAndLog("Converters creation"); |
| 138 | |
limpbizkit | 150d677 | 2008-04-25 17:34:25 +0000 | [diff] [blame] | 139 | bindLogger(); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 140 | bindCommandProcesor = new BindCommandProcessor(errors, |
limpbizkit | ecbb080 | 2008-06-02 07:02:11 +0000 | [diff] [blame] | 141 | injector, injector.scopes, injector.explicitBindings, |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 142 | injector.outstandingInjections); |
limpbizkit | ad94bcb | 2008-04-28 00:22:43 +0000 | [diff] [blame] | 143 | bindCommandProcesor.processCommands(commands); |
limpbizkit | f44e9cc | 2008-03-26 06:33:29 +0000 | [diff] [blame] | 144 | bindCommandProcesor.createUntargettedBindings(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 145 | stopwatch.resetAndLog("Binding creation"); |
| 146 | |
| 147 | injector.index(); |
| 148 | stopwatch.resetAndLog("Binding indexing"); |
| 149 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 150 | requestStaticInjectionCommandProcessor |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 151 | = new RequestStaticInjectionCommandProcessor(errors); |
limpbizkit | ad94bcb | 2008-04-28 00:22:43 +0000 | [diff] [blame] | 152 | requestStaticInjectionCommandProcessor |
| 153 | .processCommands(commands); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 154 | stopwatch.resetAndLog("Static injection"); |
| 155 | } |
| 156 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 157 | /** Validate everything that we can validate now that the injector is ready for use. */ |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 158 | private void validate() { |
| 159 | bindCommandProcesor.runCreationListeners(injector); |
| 160 | stopwatch.resetAndLog("Validation"); |
| 161 | |
| 162 | requestStaticInjectionCommandProcessor.validate(injector); |
| 163 | stopwatch.resetAndLog("Static validation"); |
| 164 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 165 | injector.validateOustandingInjections(errors); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 166 | stopwatch.resetAndLog("Instance member validation"); |
| 167 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 168 | new GetProviderProcessor(errors, injector).processCommands(commands); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 169 | stopwatch.resetAndLog("Provider verification"); |
| 170 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 171 | errors.throwCreationExceptionIfErrorsExist(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 172 | } |
| 173 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 174 | /** Inject everything that can be injected. This uses runtime error handling. */ |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 175 | private void fulfillInjectionRequests() { |
| 176 | futureInjector.initialize(injector); |
| 177 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 178 | requestStaticInjectionCommandProcessor.injectMembers(injector); |
| 179 | stopwatch.resetAndLog("Static member injection"); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 180 | injector.fulfillOutstandingInjections(errors); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 181 | stopwatch.resetAndLog("Instance injection"); |
| 182 | |
limpbizkit | ecbb080 | 2008-06-02 07:02:11 +0000 | [diff] [blame] | 183 | loadEagerSingletons(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 184 | stopwatch.resetAndLog("Preloading"); |
| 185 | } |
| 186 | |
limpbizkit | ecbb080 | 2008-06-02 07:02:11 +0000 | [diff] [blame] | 187 | public void loadEagerSingletons() { |
| 188 | // load eager singletons, or all singletons if we're in Stage.PRODUCTION. |
limpbizkit | e81cf1b | 2008-06-02 16:41:22 +0000 | [diff] [blame] | 189 | for (final BindingImpl<?> binding |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 190 | : Iterables.concat(injector.explicitBindings.values(), injector.jitBindings.values())) { |
limpbizkit | e81cf1b | 2008-06-02 16:41:22 +0000 | [diff] [blame] | 191 | if ((stage == Stage.PRODUCTION && binding.getScope() == Scopes.SINGLETON) |
| 192 | || binding.getLoadStrategy() == LoadStrategy.EAGER) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 193 | try { |
| 194 | injector.callInContext(new ContextualCallable<Void>() { |
| 195 | public Void call(InternalContext context) { |
| 196 | InjectionPoint<?> injectionPoint = InjectionPoint.newInstance(binding.key); |
| 197 | context.setInjectionPoint(injectionPoint); |
| 198 | errors.pushInjectionPoint(injectionPoint); |
| 199 | try { |
| 200 | binding.internalFactory.get(errors, context, injectionPoint); |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 201 | } catch (ErrorsException e) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 202 | errors.merge(e.getErrors()); |
| 203 | } finally { |
| 204 | context.setInjectionPoint(null); |
| 205 | errors.popInjectionPoint(injectionPoint); |
| 206 | } |
| 207 | |
limpbizkit | ecbb080 | 2008-06-02 07:02:11 +0000 | [diff] [blame] | 208 | return null; |
limpbizkit | ecbb080 | 2008-06-02 07:02:11 +0000 | [diff] [blame] | 209 | } |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 210 | }); |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 211 | } catch (ErrorsException e) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 212 | throw new AssertionError(); |
| 213 | } |
limpbizkit | ecbb080 | 2008-06-02 07:02:11 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame^] | 218 | private static class BuiltInModule implements Module { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 219 | final Injector injector; |
| 220 | final Stage stage; |
| 221 | |
| 222 | private BuiltInModule(Injector injector, Stage stage) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 223 | this.injector = checkNotNull(injector, "injector"); |
| 224 | this.stage = checkNotNull(stage, "stage"); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 225 | } |
| 226 | |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame^] | 227 | public void configure(Binder binder) { |
| 228 | binder = binder.withSource(SourceProviders.UNKNOWN_SOURCE); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 229 | |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame^] | 230 | binder.bind(Stage.class).toInstance(stage); |
| 231 | binder.bindScope(Singleton.class, SINGLETON); |
| 232 | // Create default bindings. |
| 233 | // We use toProvider() instead of toInstance() to avoid infinite recursion |
| 234 | // in toString(). |
| 235 | binder.bind(Injector.class).toProvider(new InjectorProvider(injector)); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | class InjectorProvider implements Provider<Injector> { |
| 239 | final Injector injector; |
| 240 | |
| 241 | InjectorProvider(Injector injector) { |
| 242 | this.injector = injector; |
| 243 | } |
| 244 | |
| 245 | public Injector get() { |
| 246 | return injector; |
| 247 | } |
| 248 | |
| 249 | public String toString() { |
| 250 | return "Provider<Injector>"; |
| 251 | } |
| 252 | } |
limpbizkit | 2b93168 | 2008-04-21 00:59:34 +0000 | [diff] [blame] | 253 | } |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 254 | |
limpbizkit | 2b93168 | 2008-04-21 00:59:34 +0000 | [diff] [blame] | 255 | /** |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 256 | * The Logger is a special case because it knows the injection point of the injected member. It's |
| 257 | * the only binding that does this. |
limpbizkit | 2b93168 | 2008-04-21 00:59:34 +0000 | [diff] [blame] | 258 | */ |
| 259 | private void bindLogger() { |
| 260 | Key<Logger> key = Key.get(Logger.class); |
| 261 | LoggerFactory loggerFactory = new LoggerFactory(); |
| 262 | injector.explicitBindings.put(key, |
| 263 | new ProviderInstanceBindingImpl<Logger>(injector, key, |
| 264 | SourceProviders.UNKNOWN_SOURCE, loggerFactory, Scopes.NO_SCOPE, |
limpbizkit | 0575714 | 2008-06-02 06:58:47 +0000 | [diff] [blame] | 265 | loggerFactory, LoadStrategy.LAZY)); |
limpbizkit | 2b93168 | 2008-04-21 00:59:34 +0000 | [diff] [blame] | 266 | } |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 267 | |
limpbizkit | 2b93168 | 2008-04-21 00:59:34 +0000 | [diff] [blame] | 268 | static class LoggerFactory implements InternalFactory<Logger>, Provider<Logger> { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 269 | public Logger get(Errors errors, InternalContext context, InjectionPoint<?> injectionPoint) { |
limpbizkit | 2b93168 | 2008-04-21 00:59:34 +0000 | [diff] [blame] | 270 | Member member = injectionPoint.getMember(); |
| 271 | return member == null |
| 272 | ? Logger.getAnonymousLogger() |
| 273 | : Logger.getLogger(member.getDeclaringClass().getName()); |
| 274 | } |
| 275 | |
| 276 | public Logger get() { |
| 277 | return Logger.getAnonymousLogger(); |
| 278 | } |
| 279 | |
| 280 | public String toString() { |
| 281 | return "Provider<Logger>"; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 285 | } |