limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +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 | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 19 | import com.google.inject.internal.Errors; |
| 20 | import com.google.inject.internal.ErrorsException; |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 21 | import com.google.inject.internal.Lists; |
| 22 | import com.google.inject.internal.Maps; |
| 23 | import static com.google.inject.internal.Preconditions.checkNotNull; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 24 | import com.google.inject.spi.InjectionPoint; |
| 25 | import java.util.Map; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 26 | import java.util.Set; |
| 27 | import java.util.concurrent.CountDownLatch; |
| 28 | |
| 29 | /** |
limpbizkit | 1db2393 | 2008-10-15 00:06:43 +0000 | [diff] [blame] | 30 | * Manages and injects instances at injector-creation time. This is made more complicated by |
| 31 | * instances that request other instances while they're being injected. We overcome this by using |
| 32 | * {@link Initializable}, which attempts to perform injection before use. |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 33 | * |
| 34 | * @author jessewilson@google.com (Jesse Wilson) |
| 35 | */ |
| 36 | class Initializer { |
| 37 | /** the only thread that we'll use to inject members. */ |
| 38 | private final Thread creatingThread = Thread.currentThread(); |
| 39 | |
| 40 | /** zero means everything is injected. */ |
| 41 | private final CountDownLatch ready = new CountDownLatch(1); |
| 42 | |
| 43 | /** Maps instances that need injection to a source that registered them */ |
limpbizkit | 1db2393 | 2008-10-15 00:06:43 +0000 | [diff] [blame] | 44 | private final Map<Object, InjectableReference<?>> pendingInjection = Maps.newIdentityHashMap(); |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Registers an instance for member injection when that step is performed. |
| 48 | * |
| 49 | * @param instance an instance that optionally has members to be injected (each annotated with |
| 50 | * @Inject). |
| 51 | * @param source the source location that this injection was requested |
| 52 | */ |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 53 | public <T> Initializable<T> requestInjection(InjectorImpl injector, T instance, Object source, |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 54 | Set<InjectionPoint> injectionPoints) { |
| 55 | checkNotNull(source); |
limpbizkit | 1db2393 | 2008-10-15 00:06:43 +0000 | [diff] [blame] | 56 | |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 57 | // short circuit if the object has no injections |
| 58 | if (instance == null |
| 59 | || (injectionPoints.isEmpty() && !injector.membersInjectorStore.hasTypeListeners())) { |
limpbizkit | 1db2393 | 2008-10-15 00:06:43 +0000 | [diff] [blame] | 60 | return Initializables.of(instance); |
| 61 | } |
| 62 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 63 | InjectableReference<T> initializable = new InjectableReference<T>(injector, instance, source); |
limpbizkit | 1db2393 | 2008-10-15 00:06:43 +0000 | [diff] [blame] | 64 | pendingInjection.put(instance, initializable); |
| 65 | return initializable; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Prepares member injectors for all injected instances. This prompts Guice to do static analysis |
| 70 | * on the injected instances. |
| 71 | */ |
| 72 | void validateOustandingInjections(Errors errors) { |
limpbizkit | 1db2393 | 2008-10-15 00:06:43 +0000 | [diff] [blame] | 73 | for (InjectableReference<?> reference : pendingInjection.values()) { |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 74 | try { |
limpbizkit | 1db2393 | 2008-10-15 00:06:43 +0000 | [diff] [blame] | 75 | reference.validate(errors); |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 76 | } catch (ErrorsException e) { |
| 77 | errors.merge(e.getErrors()); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Performs creation-time injections on all objects that require it. Whenever fulfilling an |
limpbizkit | 1db2393 | 2008-10-15 00:06:43 +0000 | [diff] [blame] | 84 | * injection depends on another object that requires injection, we inject it first. If the two |
| 85 | * instances are codependent (directly or transitively), ordering of injection is arbitrary. |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 86 | */ |
| 87 | void injectAll(final Errors errors) { |
| 88 | // loop over a defensive copy since ensureInjected() mutates the set. Unfortunately, that copy |
| 89 | // is made complicated by a bug in IBM's JDK, wherein entrySet().toArray(Object[]) doesn't work |
limpbizkit | 1db2393 | 2008-10-15 00:06:43 +0000 | [diff] [blame] | 90 | for (InjectableReference<?> reference : Lists.newArrayList(pendingInjection.values())) { |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 91 | try { |
limpbizkit | 1db2393 | 2008-10-15 00:06:43 +0000 | [diff] [blame] | 92 | reference.get(errors); |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 93 | } catch (ErrorsException e) { |
| 94 | errors.merge(e.getErrors()); |
| 95 | } |
| 96 | } |
| 97 | |
limpbizkit | 1db2393 | 2008-10-15 00:06:43 +0000 | [diff] [blame] | 98 | if (!pendingInjection.isEmpty()) { |
| 99 | throw new AssertionError("Failed to satisfy " + pendingInjection); |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | ready.countDown(); |
| 103 | } |
| 104 | |
| 105 | private class InjectableReference<T> implements Initializable<T> { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 106 | private final InjectorImpl injector; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 107 | private final T instance; |
limpbizkit | 1db2393 | 2008-10-15 00:06:43 +0000 | [diff] [blame] | 108 | private final Object source; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 109 | private MembersInjectorImpl<T> membersInjector; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 110 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 111 | public InjectableReference(InjectorImpl injector, T instance, Object source) { |
| 112 | this.injector = injector; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 113 | this.instance = checkNotNull(instance, "instance"); |
limpbizkit | 1db2393 | 2008-10-15 00:06:43 +0000 | [diff] [blame] | 114 | this.source = checkNotNull(source, "source"); |
| 115 | } |
| 116 | |
| 117 | public void validate(Errors errors) throws ErrorsException { |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 118 | @SuppressWarnings("unchecked") // the type of 'T' is a TypeLiteral<T> |
| 119 | TypeLiteral<T> type = TypeLiteral.get((Class<T>) instance.getClass()); |
| 120 | membersInjector = injector.membersInjectorStore.get(type, errors.withSource(source)); |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
limpbizkit | 1db2393 | 2008-10-15 00:06:43 +0000 | [diff] [blame] | 124 | * Reentrant. If {@code instance} was registered for injection at injector-creation time, this |
| 125 | * method will ensure that all its members have been injected before returning. |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 126 | */ |
| 127 | public T get(Errors errors) throws ErrorsException { |
limpbizkit | 1db2393 | 2008-10-15 00:06:43 +0000 | [diff] [blame] | 128 | if (ready.getCount() == 0) { |
| 129 | return instance; |
| 130 | } |
| 131 | |
| 132 | // just wait for everything to be injected by another thread |
| 133 | if (Thread.currentThread() != creatingThread) { |
| 134 | try { |
| 135 | ready.await(); |
| 136 | return instance; |
| 137 | } catch (InterruptedException e) { |
| 138 | // Give up, since we don't know if our injection is ready |
| 139 | throw new RuntimeException(e); |
| 140 | } |
| 141 | } |
| 142 | |
limpbizkit | 8f370d3 | 2008-10-16 16:15:48 +0000 | [diff] [blame] | 143 | // toInject needs injection, do it right away. we only do this once, even if it fails |
limpbizkit | 1db2393 | 2008-10-15 00:06:43 +0000 | [diff] [blame] | 144 | if (pendingInjection.remove(instance) != null) { |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 145 | membersInjector.injectAndNotify(instance, errors.withSource(source)); |
limpbizkit | 1db2393 | 2008-10-15 00:06:43 +0000 | [diff] [blame] | 146 | } |
| 147 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 148 | return instance; |
| 149 | } |
| 150 | |
| 151 | @Override public String toString() { |
| 152 | return instance.toString(); |
| 153 | } |
| 154 | } |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 155 | } |