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