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 | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 21 | import com.google.common.collect.Iterables; |
| 22 | import com.google.common.collect.Lists; |
| 23 | import com.google.inject.Reflection.Factory; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 24 | import static com.google.inject.Scopes.SINGLETON; |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 25 | import com.google.inject.internal.Errors; |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 26 | import com.google.inject.internal.ErrorsException; |
limpbizkit | 6663d02 | 2008-06-19 07:57:55 +0000 | [diff] [blame] | 27 | import com.google.inject.internal.SourceProvider; |
limpbizkit | 2b93168 | 2008-04-21 00:59:34 +0000 | [diff] [blame] | 28 | import com.google.inject.internal.Stopwatch; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 29 | import com.google.inject.spi.Dependency; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 30 | import com.google.inject.spi.Element; |
| 31 | import com.google.inject.spi.Elements; |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 32 | import com.google.inject.spi.InjectionPoint; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 33 | import java.util.Collection; |
limpbizkit | f44e9cc | 2008-03-26 06:33:29 +0000 | [diff] [blame] | 34 | import java.util.List; |
limpbizkit | 8eef998 | 2008-08-03 00:45:52 +0000 | [diff] [blame] | 35 | import java.util.Map; |
limpbizkit | 5127365 | 2008-09-29 18:21:20 +0000 | [diff] [blame] | 36 | import java.util.Set; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 37 | import java.util.logging.Logger; |
| 38 | |
| 39 | /** |
| 40 | * Builds a dependency injection {@link Injector}. |
| 41 | * |
| 42 | * @author crazybob@google.com (Bob Lee) |
| 43 | * @author jessewilson@google.com (Jesse Wilson) |
| 44 | */ |
| 45 | class InjectorBuilder { |
| 46 | |
| 47 | private final Stopwatch stopwatch = new Stopwatch(); |
| 48 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 49 | private InjectorImpl parent = null; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 50 | private Stage stage; |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 51 | private Factory reflectionFactory = new RuntimeReflectionFactory(); |
| 52 | private final List<Module> modules = Lists.newLinkedList(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 53 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 54 | private InjectorImpl injector; |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 55 | private Errors errors = new Errors(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 56 | |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 57 | private final List<Element> elements = Lists.newArrayList(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 58 | |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 59 | private BindingProcessor bindCommandProcesor; |
| 60 | private InjectionRequestProcessor injectionCommandProcessor; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 61 | |
| 62 | /** |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 63 | * @param stage we're running in. If the stage is {@link Stage#PRODUCTION}, we will eagerly load |
| 64 | * singletons. |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 65 | */ |
| 66 | InjectorBuilder stage(Stage stage) { |
| 67 | this.stage = stage; |
| 68 | return this; |
| 69 | } |
| 70 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 71 | InjectorBuilder usingReflectionFactory(Factory reflectionFactory) { |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 72 | this.reflectionFactory = reflectionFactory; |
| 73 | return this; |
| 74 | } |
| 75 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 76 | InjectorBuilder parentInjector(InjectorImpl parent) { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 77 | this.parent = parent; |
| 78 | return this; |
| 79 | } |
| 80 | |
| 81 | InjectorBuilder addModules(Iterable<? extends Module> modules) { |
| 82 | for (Module module : modules) { |
| 83 | this.modules.add(module); |
| 84 | } |
| 85 | return this; |
| 86 | } |
| 87 | |
| 88 | Injector build() { |
| 89 | if (injector != null) { |
| 90 | throw new AssertionError("Already built, builders are not reusable."); |
| 91 | } |
| 92 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 93 | injector = new InjectorImpl(parent); |
limpbizkit | 7f8eda0 | 2008-03-26 01:11:16 +0000 | [diff] [blame] | 94 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 95 | // bind Stage and Singleton if this is a top-level injector |
| 96 | if (parent == null) { |
| 97 | modules.add(0, new RootModule(stage)); |
| 98 | } |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 99 | |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 100 | elements.addAll(Elements.getElements(stage, modules)); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 101 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 102 | buildCoreInjector(); |
| 103 | |
| 104 | validate(); |
| 105 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 106 | errors.throwCreationExceptionIfErrorsExist(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 107 | |
| 108 | // If we're in the tool stage, stop here. Don't eagerly inject or load |
| 109 | // anything. |
| 110 | if (stage == Stage.TOOL) { |
limpbizkit | 8eef998 | 2008-08-03 00:45:52 +0000 | [diff] [blame] | 111 | return new ToolStageInjector(injector); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | fulfillInjectionRequests(); |
| 115 | |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 116 | if (!elements.isEmpty()) { |
| 117 | throw new AssertionError("Failed to execute " + elements); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return injector; |
| 121 | } |
| 122 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 123 | /** Builds the injector. */ |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 124 | private void buildCoreInjector() { |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 125 | new MessageProcessor(errors) |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 126 | .processCommands(elements); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 127 | |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 128 | InterceptorBindingProcessor interceptorCommandProcessor |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 129 | = new InterceptorBindingProcessor(errors, injector.state); |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 130 | interceptorCommandProcessor.processCommands(elements); |
| 131 | ConstructionProxyFactory proxyFactory = interceptorCommandProcessor.createProxyFactory(); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 132 | injector.reflection = reflectionFactory.create(proxyFactory); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 133 | stopwatch.resetAndLog("Interceptors creation"); |
| 134 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 135 | new ScopeBindingProcessor(errors, injector.state).processCommands(elements); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 136 | stopwatch.resetAndLog("Scopes creation"); |
| 137 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 138 | new TypeConverterBindingProcessor(errors, injector.state).processCommands(elements); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 139 | stopwatch.resetAndLog("Converters creation"); |
| 140 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 141 | bindInjector(); |
limpbizkit | 150d677 | 2008-04-25 17:34:25 +0000 | [diff] [blame] | 142 | bindLogger(); |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 143 | bindCommandProcesor = new BindingProcessor(errors, |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 144 | injector, injector.state, injector.initializer); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 145 | bindCommandProcesor.processCommands(elements); |
limpbizkit | f44e9cc | 2008-03-26 06:33:29 +0000 | [diff] [blame] | 146 | bindCommandProcesor.createUntargettedBindings(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 147 | stopwatch.resetAndLog("Binding creation"); |
| 148 | |
| 149 | injector.index(); |
| 150 | stopwatch.resetAndLog("Binding indexing"); |
| 151 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 152 | injectionCommandProcessor = new InjectionRequestProcessor(errors, injector.initializer); |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 153 | injectionCommandProcessor.processCommands(elements); |
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 | |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 162 | injectionCommandProcessor.validate(injector); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 163 | stopwatch.resetAndLog("Static validation"); |
| 164 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 165 | injector.initializer.validateOustandingInjections(errors); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 166 | stopwatch.resetAndLog("Instance member validation"); |
| 167 | |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 168 | new ProviderLookupProcessor(errors, injector).processCommands(elements); |
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 | 9532e62 | 2008-06-18 08:20:54 +0000 | [diff] [blame] | 174 | /** Inject everything that can be injected. */ |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 175 | private void fulfillInjectionRequests() { |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 176 | injectionCommandProcessor.injectMembers(injector); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 177 | stopwatch.resetAndLog("Static member injection"); |
limpbizkit | 9532e62 | 2008-06-18 08:20:54 +0000 | [diff] [blame] | 178 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 179 | injector.initializer.injectAll(errors); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 180 | stopwatch.resetAndLog("Instance injection"); |
limpbizkit | 9532e62 | 2008-06-18 08:20:54 +0000 | [diff] [blame] | 181 | errors.throwCreationExceptionIfErrorsExist(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 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"); |
limpbizkit | 9532e62 | 2008-06-18 08:20:54 +0000 | [diff] [blame] | 185 | errors.throwCreationExceptionIfErrorsExist(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 186 | } |
| 187 | |
limpbizkit | ecbb080 | 2008-06-02 07:02:11 +0000 | [diff] [blame] | 188 | public void loadEagerSingletons() { |
| 189 | // load eager singletons, or all singletons if we're in Stage.PRODUCTION. |
limpbizkit | 5127365 | 2008-09-29 18:21:20 +0000 | [diff] [blame] | 190 | // Bindings discovered while we're binding these singletons are not be eager. |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 191 | @SuppressWarnings("unchecked") // casting Collection<Binding> to Collection<BindingImpl> is safe |
| 192 | Set<BindingImpl<?>> candidateBindings = ImmutableSet.copyOf(Iterables.concat( |
| 193 | (Collection) injector.state.getExplicitBindingsThisLevel().values(), |
| 194 | injector.jitBindings.values())); |
limpbizkit | 5127365 | 2008-09-29 18:21:20 +0000 | [diff] [blame] | 195 | for (final BindingImpl<?> binding : candidateBindings) { |
limpbizkit | 5789ef4 | 2008-08-04 06:44:30 +0000 | [diff] [blame] | 196 | if ((stage == Stage.PRODUCTION && binding.getScope() == SINGLETON) |
limpbizkit | e81cf1b | 2008-06-02 16:41:22 +0000 | [diff] [blame] | 197 | || binding.getLoadStrategy() == LoadStrategy.EAGER) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 198 | try { |
| 199 | injector.callInContext(new ContextualCallable<Void>() { |
| 200 | public Void call(InternalContext context) { |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 201 | Dependency<?> dependency = Dependency.get(binding.key); |
| 202 | context.setDependency(dependency); |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 203 | errors.pushSource(dependency); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 204 | try { |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 205 | binding.internalFactory.get(errors, context, dependency); |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 206 | } catch (ErrorsException e) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 207 | errors.merge(e.getErrors()); |
| 208 | } finally { |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 209 | context.setDependency(null); |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 210 | errors.popSource(dependency); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 211 | } |
| 212 | |
limpbizkit | ecbb080 | 2008-06-02 07:02:11 +0000 | [diff] [blame] | 213 | return null; |
limpbizkit | ecbb080 | 2008-06-02 07:02:11 +0000 | [diff] [blame] | 214 | } |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 215 | }); |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 216 | } catch (ErrorsException e) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 217 | throw new AssertionError(); |
| 218 | } |
limpbizkit | ecbb080 | 2008-06-02 07:02:11 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 223 | private static class RootModule implements Module { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 224 | final Stage stage; |
| 225 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 226 | private RootModule(Stage stage) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 227 | this.stage = checkNotNull(stage, "stage"); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 228 | } |
| 229 | |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame] | 230 | public void configure(Binder binder) { |
limpbizkit | 9532e62 | 2008-06-18 08:20:54 +0000 | [diff] [blame] | 231 | binder = binder.withSource(SourceProvider.UNKNOWN_SOURCE); |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame] | 232 | binder.bind(Stage.class).toInstance(stage); |
| 233 | binder.bindScope(Singleton.class, SINGLETON); |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * The Injector is a special case because we allow both parent and child injectors to both have |
| 239 | * a binding for that key. |
| 240 | */ |
| 241 | private void bindInjector() { |
| 242 | Key<Injector> key = Key.get(Injector.class); |
| 243 | InjectorFactory injectorFactory = new InjectorFactory(injector); |
| 244 | injector.state.putBinding(key, |
| 245 | new ProviderInstanceBindingImpl<Injector>(injector, key, SourceProvider.UNKNOWN_SOURCE, |
| 246 | injectorFactory, Scopes.NO_SCOPE, injectorFactory, LoadStrategy.LAZY, |
| 247 | ImmutableSet.<InjectionPoint>of())); |
| 248 | } |
| 249 | |
| 250 | static class InjectorFactory implements InternalFactory<Injector>, Provider<Injector> { |
| 251 | private final Injector injector; |
| 252 | |
| 253 | private InjectorFactory(Injector injector) { |
| 254 | this.injector = injector; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 255 | } |
| 256 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 257 | public Injector get(Errors errors, InternalContext context, Dependency<?> dependency) |
| 258 | throws ErrorsException { |
| 259 | return injector; |
| 260 | } |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 261 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 262 | public Injector get() { |
| 263 | return injector; |
| 264 | } |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 265 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 266 | public String toString() { |
| 267 | return "Provider<Injector>"; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 268 | } |
limpbizkit | 2b93168 | 2008-04-21 00:59:34 +0000 | [diff] [blame] | 269 | } |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 270 | |
limpbizkit | 2b93168 | 2008-04-21 00:59:34 +0000 | [diff] [blame] | 271 | /** |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 272 | * The Logger is a special case because it knows the injection point of the injected member. It's |
| 273 | * the only binding that does this. |
limpbizkit | 2b93168 | 2008-04-21 00:59:34 +0000 | [diff] [blame] | 274 | */ |
| 275 | private void bindLogger() { |
| 276 | Key<Logger> key = Key.get(Logger.class); |
| 277 | LoggerFactory loggerFactory = new LoggerFactory(); |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 278 | injector.state.putBinding(key, |
limpbizkit | 2b93168 | 2008-04-21 00:59:34 +0000 | [diff] [blame] | 279 | new ProviderInstanceBindingImpl<Logger>(injector, key, |
limpbizkit | 9532e62 | 2008-06-18 08:20:54 +0000 | [diff] [blame] | 280 | SourceProvider.UNKNOWN_SOURCE, loggerFactory, Scopes.NO_SCOPE, |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 281 | loggerFactory, LoadStrategy.LAZY, ImmutableSet.<InjectionPoint>of())); |
limpbizkit | 2b93168 | 2008-04-21 00:59:34 +0000 | [diff] [blame] | 282 | } |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 283 | |
limpbizkit | 2b93168 | 2008-04-21 00:59:34 +0000 | [diff] [blame] | 284 | static class LoggerFactory implements InternalFactory<Logger>, Provider<Logger> { |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 285 | public Logger get(Errors errors, InternalContext context, Dependency<?> dependency) { |
| 286 | InjectionPoint injectionPoint = dependency.getInjectionPoint(); |
| 287 | return injectionPoint == null |
limpbizkit | 2b93168 | 2008-04-21 00:59:34 +0000 | [diff] [blame] | 288 | ? Logger.getAnonymousLogger() |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 289 | : Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName()); |
limpbizkit | 2b93168 | 2008-04-21 00:59:34 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | public Logger get() { |
| 293 | return Logger.getAnonymousLogger(); |
| 294 | } |
| 295 | |
| 296 | public String toString() { |
| 297 | return "Provider<Logger>"; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
limpbizkit | 8eef998 | 2008-08-03 00:45:52 +0000 | [diff] [blame] | 301 | /** {@link Injector} exposed to users in {@link Stage#TOOL}. */ |
| 302 | static class ToolStageInjector implements Injector { |
| 303 | private final Injector delegateInjector; |
| 304 | |
| 305 | ToolStageInjector(Injector delegateInjector) { |
| 306 | this.delegateInjector = delegateInjector; |
| 307 | } |
| 308 | public void injectMembers(Object o) { |
| 309 | throw new UnsupportedOperationException( |
| 310 | "Injector.injectMembers(Object) is not supported in Stage.TOOL"); |
| 311 | } |
| 312 | public Map<Key<?>, Binding<?>> getBindings() { |
| 313 | return this.delegateInjector.getBindings(); |
| 314 | } |
| 315 | public <T> Binding<T> getBinding(Key<T> key) { |
| 316 | return this.delegateInjector.getBinding(key); |
| 317 | } |
| 318 | public <T> Binding<T> getBinding(Class<T> type) { |
| 319 | return this.delegateInjector.getBinding(type); |
| 320 | } |
| 321 | public <T> List<Binding<T>> findBindingsByType(TypeLiteral<T> type) { |
| 322 | return this.delegateInjector.findBindingsByType(type); |
| 323 | } |
limpbizkit | edd8d64 | 2008-10-18 19:50:49 +0000 | [diff] [blame^] | 324 | public Injector getParent() { |
| 325 | return delegateInjector.getParent(); |
| 326 | } |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 327 | public Injector createChildInjector(Iterable<? extends Module> modules) { |
| 328 | return delegateInjector.createChildInjector(modules); |
| 329 | } |
| 330 | public Injector createChildInjector(Module... modules) { |
| 331 | return delegateInjector.createChildInjector(modules); |
| 332 | } |
limpbizkit | 8eef998 | 2008-08-03 00:45:52 +0000 | [diff] [blame] | 333 | public <T> Provider<T> getProvider(Key<T> key) { |
| 334 | throw new UnsupportedOperationException( |
| 335 | "Injector.getProvider(Key<T>) is not supported in Stage.TOOL"); |
| 336 | } |
| 337 | public <T> Provider<T> getProvider(Class<T> type) { |
| 338 | throw new UnsupportedOperationException( |
| 339 | "Injector.getProvider(Class<T>) is not supported in Stage.TOOL"); |
| 340 | } |
| 341 | public <T> T getInstance(Key<T> key) { |
| 342 | throw new UnsupportedOperationException( |
| 343 | "Injector.getInstance(Key<T>) is not supported in Stage.TOOL"); |
| 344 | } |
| 345 | public <T> T getInstance(Class<T> type) { |
| 346 | throw new UnsupportedOperationException( |
| 347 | "Injector.getInstance(Class<T>) is not supported in Stage.TOOL"); |
| 348 | } |
| 349 | } |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 350 | } |