limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2008 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 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 19 | import static com.google.inject.Scopes.SINGLETON; |
| 20 | import com.google.inject.internal.Errors; |
| 21 | import com.google.inject.internal.ErrorsException; |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 22 | import com.google.inject.internal.ImmutableSet; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 23 | import com.google.inject.internal.InternalContext; |
| 24 | import com.google.inject.internal.InternalFactory; |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 25 | import com.google.inject.internal.Lists; |
| 26 | import static com.google.inject.internal.Preconditions.checkNotNull; |
| 27 | import static com.google.inject.internal.Preconditions.checkState; |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 28 | import com.google.inject.internal.PrivateElementsImpl; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 29 | import com.google.inject.internal.ProviderInstanceBindingImpl; |
| 30 | import com.google.inject.internal.Scoping; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 31 | import com.google.inject.internal.SourceProvider; |
| 32 | import com.google.inject.internal.Stopwatch; |
| 33 | import com.google.inject.spi.Dependency; |
| 34 | import com.google.inject.spi.Element; |
| 35 | import com.google.inject.spi.Elements; |
| 36 | import com.google.inject.spi.InjectionPoint; |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 37 | import com.google.inject.spi.PrivateElements; |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 38 | import com.google.inject.spi.TypeListenerBinding; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 39 | import java.util.List; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 40 | import java.util.logging.Logger; |
| 41 | |
| 42 | /** |
| 43 | * A partially-initialized injector. See {@link InjectorBuilder}, which uses this to build a tree |
| 44 | * of injectors in batch. |
| 45 | * |
| 46 | * @author jessewilson@google.com (Jesse Wilson) |
| 47 | */ |
| 48 | class InjectorShell { |
| 49 | |
| 50 | private final List<Element> elements; |
| 51 | private final InjectorImpl injector; |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 52 | private final PrivateElements privateElements; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 53 | |
| 54 | private InjectorShell(Builder builder, List<Element> elements, InjectorImpl injector) { |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 55 | this.privateElements = builder.privateElements; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 56 | this.elements = elements; |
| 57 | this.injector = injector; |
| 58 | } |
| 59 | |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 60 | PrivateElements getPrivateElements() { |
| 61 | return privateElements; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | InjectorImpl getInjector() { |
| 65 | return injector; |
| 66 | } |
| 67 | |
| 68 | List<Element> getElements() { |
| 69 | return elements; |
| 70 | } |
| 71 | |
| 72 | static class Builder { |
| 73 | private final List<Element> elements = Lists.newArrayList(); |
| 74 | private final List<Module> modules = Lists.newArrayList(); |
| 75 | |
| 76 | /** lazily constructed */ |
| 77 | private State state; |
| 78 | |
| 79 | private InjectorImpl parent; |
| 80 | private Stage stage; |
| 81 | |
| 82 | /** null unless this exists in a {@link Binder#newPrivateBinder private environment} */ |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 83 | private PrivateElementsImpl privateElements; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 84 | |
| 85 | Builder parent(InjectorImpl parent) { |
| 86 | this.parent = parent; |
| 87 | this.state = new InheritingState(parent.state); |
| 88 | return this; |
| 89 | } |
| 90 | |
| 91 | Builder stage(Stage stage) { |
| 92 | this.stage = stage; |
| 93 | return this; |
| 94 | } |
| 95 | |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 96 | Builder privateElements(PrivateElements privateElements) { |
| 97 | this.privateElements = (PrivateElementsImpl) privateElements; |
| 98 | this.elements.addAll(privateElements.getElements()); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 99 | return this; |
| 100 | } |
| 101 | |
| 102 | void addModules(Iterable<? extends Module> modules) { |
| 103 | for (Module module : modules) { |
| 104 | this.modules.add(module); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** Synchronize on this before calling {@link #build}. */ |
| 109 | Object lock() { |
| 110 | return getState().lock(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Creates and returns the injector shells for the current modules. Multiple shells will be |
| 115 | * returned if any modules contain {@link Binder#newPrivateBinder private environments}. The |
| 116 | * primary injector will be first in the returned list. |
| 117 | */ |
| 118 | List<InjectorShell> build(Initializer initializer, BindingProcessor bindingProcessor, |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 119 | Stopwatch stopwatch, Errors errors) { |
| 120 | checkState(stage != null, "Stage not initialized"); |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 121 | checkState(privateElements == null || parent != null, "PrivateElements with no parent"); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 122 | checkState(state != null, "no state. Did you remember to lock() ?"); |
| 123 | |
| 124 | InjectorImpl injector = new InjectorImpl(parent, state, initializer); |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 125 | if (privateElements != null) { |
| 126 | privateElements.initInjector(injector); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // bind Stage and Singleton if this is a top-level injector |
| 130 | if (parent == null) { |
| 131 | modules.add(0, new RootModule(stage)); |
| 132 | new TypeConverterBindingProcessor(errors).prepareBuiltInConverters(injector); |
| 133 | } |
| 134 | |
| 135 | elements.addAll(Elements.getElements(stage, modules)); |
| 136 | stopwatch.resetAndLog("Module execution"); |
| 137 | |
| 138 | new MessageProcessor(errors).process(injector, elements); |
| 139 | |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 140 | /*if[AOP]*/ |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 141 | InterceptorBindingProcessor interceptors = new InterceptorBindingProcessor(errors); |
| 142 | interceptors.process(injector, elements); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 143 | stopwatch.resetAndLog("Interceptors creation"); |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 144 | /*end[AOP]*/ |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 145 | |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 146 | new TypeListenerBindingProcessor(errors).process(injector, elements); |
| 147 | List<TypeListenerBinding> listenerBindings = injector.state.getTypeListenerBindings(); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 148 | injector.membersInjectorStore = new MembersInjectorStore(injector, listenerBindings); |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 149 | stopwatch.resetAndLog("TypeListeners creation"); |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 150 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 151 | new ScopeBindingProcessor(errors).process(injector, elements); |
| 152 | stopwatch.resetAndLog("Scopes creation"); |
| 153 | |
| 154 | new TypeConverterBindingProcessor(errors).process(injector, elements); |
| 155 | stopwatch.resetAndLog("Converters creation"); |
| 156 | |
| 157 | bindInjector(injector); |
| 158 | bindLogger(injector); |
| 159 | bindingProcessor.process(injector, elements); |
| 160 | stopwatch.resetAndLog("Binding creation"); |
| 161 | |
| 162 | List<InjectorShell> injectorShells = Lists.newArrayList(); |
| 163 | injectorShells.add(new InjectorShell(this, elements, injector)); |
| 164 | |
| 165 | // recursively build child shells |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 166 | PrivateElementProcessor processor = new PrivateElementProcessor(errors, stage); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 167 | processor.process(injector, elements); |
| 168 | for (Builder builder : processor.getInjectorShellBuilders()) { |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 169 | injectorShells.addAll(builder.build(initializer, bindingProcessor, stopwatch, errors)); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 170 | } |
| 171 | stopwatch.resetAndLog("Private environment creation"); |
| 172 | |
| 173 | return injectorShells; |
| 174 | } |
| 175 | |
| 176 | private State getState() { |
| 177 | if (state == null) { |
| 178 | state = new InheritingState(State.NONE); |
| 179 | } |
| 180 | return state; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * The Injector is a special case because we allow both parent and child injectors to both have |
| 186 | * a binding for that key. |
| 187 | */ |
| 188 | private static void bindInjector(InjectorImpl injector) { |
| 189 | Key<Injector> key = Key.get(Injector.class); |
| 190 | InjectorFactory injectorFactory = new InjectorFactory(injector); |
| 191 | injector.state.putBinding(key, |
| 192 | new ProviderInstanceBindingImpl<Injector>(injector, key, SourceProvider.UNKNOWN_SOURCE, |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 193 | injectorFactory, Scoping.UNSCOPED, injectorFactory, |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 194 | ImmutableSet.<InjectionPoint>of())); |
| 195 | } |
| 196 | |
| 197 | private static class InjectorFactory implements InternalFactory<Injector>, Provider<Injector> { |
| 198 | private final Injector injector; |
| 199 | |
| 200 | private InjectorFactory(Injector injector) { |
| 201 | this.injector = injector; |
| 202 | } |
| 203 | |
| 204 | public Injector get(Errors errors, InternalContext context, Dependency<?> dependency) |
| 205 | throws ErrorsException { |
| 206 | return injector; |
| 207 | } |
| 208 | |
| 209 | public Injector get() { |
| 210 | return injector; |
| 211 | } |
| 212 | |
| 213 | public String toString() { |
| 214 | return "Provider<Injector>"; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * The Logger is a special case because it knows the injection point of the injected member. It's |
| 220 | * the only binding that does this. |
| 221 | */ |
| 222 | private static void bindLogger(InjectorImpl injector) { |
| 223 | Key<Logger> key = Key.get(Logger.class); |
| 224 | LoggerFactory loggerFactory = new LoggerFactory(); |
| 225 | injector.state.putBinding(key, |
| 226 | new ProviderInstanceBindingImpl<Logger>(injector, key, |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 227 | SourceProvider.UNKNOWN_SOURCE, loggerFactory, Scoping.UNSCOPED, |
| 228 | loggerFactory, ImmutableSet.<InjectionPoint>of())); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | private static class LoggerFactory implements InternalFactory<Logger>, Provider<Logger> { |
| 232 | public Logger get(Errors errors, InternalContext context, Dependency<?> dependency) { |
| 233 | InjectionPoint injectionPoint = dependency.getInjectionPoint(); |
| 234 | return injectionPoint == null |
| 235 | ? Logger.getAnonymousLogger() |
| 236 | : Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName()); |
| 237 | } |
| 238 | |
| 239 | public Logger get() { |
| 240 | return Logger.getAnonymousLogger(); |
| 241 | } |
| 242 | |
| 243 | public String toString() { |
| 244 | return "Provider<Logger>"; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | private static class RootModule implements Module { |
| 249 | final Stage stage; |
| 250 | |
| 251 | private RootModule(Stage stage) { |
| 252 | this.stage = checkNotNull(stage, "stage"); |
| 253 | } |
| 254 | |
| 255 | public void configure(Binder binder) { |
| 256 | binder = binder.withSource(SourceProvider.UNKNOWN_SOURCE); |
| 257 | binder.bind(Stage.class).toInstance(stage); |
| 258 | binder.bindScope(Singleton.class, SINGLETON); |
| 259 | } |
| 260 | } |
| 261 | } |