blob: 68206f55185955eea3d100b230a502baec3c1cf0 [file] [log] [blame]
limpbizkit5fb9d922008-10-14 23:35:56 +00001/**
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
limpbizkit5ae41eb2009-06-06 17:51:27 +000017package com.google.inject.internal;
limpbizkit5fb9d922008-10-14 23:35:56 +000018
limpbizkit5ae41eb2009-06-06 17:51:27 +000019import com.google.inject.TypeLiteral;
limpbizkit53664a72009-02-21 00:25:27 +000020import static com.google.inject.internal.Preconditions.checkNotNull;
limpbizkit5fb9d922008-10-14 23:35:56 +000021import com.google.inject.spi.InjectionPoint;
22import java.util.Map;
limpbizkit5fb9d922008-10-14 23:35:56 +000023import java.util.Set;
24import java.util.concurrent.CountDownLatch;
25
26/**
limpbizkit1db23932008-10-15 00:06:43 +000027 * 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.
limpbizkit5fb9d922008-10-14 23:35:56 +000030 *
31 * @author jessewilson@google.com (Jesse Wilson)
32 */
limpbizkit5ae41eb2009-06-06 17:51:27 +000033final class Initializer {
limpbizkit5fb9d922008-10-14 23:35:56 +000034 /** 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 */
limpbizkit1db23932008-10-15 00:06:43 +000041 private final Map<Object, InjectableReference<?>> pendingInjection = Maps.newIdentityHashMap();
limpbizkit5fb9d922008-10-14 23:35:56 +000042
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 */
limpbizkit5ae41eb2009-06-06 17:51:27 +000050 <T> Initializable<T> requestInjection(InjectorImpl injector, T instance, Object source,
limpbizkit5fb9d922008-10-14 23:35:56 +000051 Set<InjectionPoint> injectionPoints) {
52 checkNotNull(source);
limpbizkit1db23932008-10-15 00:06:43 +000053
limpbizkita843a952009-04-08 22:24:55 +000054 // short circuit if the object has no injections
55 if (instance == null
56 || (injectionPoints.isEmpty() && !injector.membersInjectorStore.hasTypeListeners())) {
limpbizkit1db23932008-10-15 00:06:43 +000057 return Initializables.of(instance);
58 }
59
limpbizkitfcbdf992008-11-26 02:37:35 +000060 InjectableReference<T> initializable = new InjectableReference<T>(injector, instance, source);
limpbizkit1db23932008-10-15 00:06:43 +000061 pendingInjection.put(instance, initializable);
62 return initializable;
limpbizkit5fb9d922008-10-14 23:35:56 +000063 }
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) {
limpbizkit1db23932008-10-15 00:06:43 +000070 for (InjectableReference<?> reference : pendingInjection.values()) {
limpbizkit5fb9d922008-10-14 23:35:56 +000071 try {
limpbizkit1db23932008-10-15 00:06:43 +000072 reference.validate(errors);
limpbizkit5fb9d922008-10-14 23:35:56 +000073 } 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
limpbizkit1db23932008-10-15 00:06:43 +000081 * 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.
limpbizkit5fb9d922008-10-14 23:35:56 +000083 */
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
limpbizkit1db23932008-10-15 00:06:43 +000087 for (InjectableReference<?> reference : Lists.newArrayList(pendingInjection.values())) {
limpbizkit5fb9d922008-10-14 23:35:56 +000088 try {
limpbizkit1db23932008-10-15 00:06:43 +000089 reference.get(errors);
limpbizkit5fb9d922008-10-14 23:35:56 +000090 } catch (ErrorsException e) {
91 errors.merge(e.getErrors());
92 }
93 }
94
limpbizkit1db23932008-10-15 00:06:43 +000095 if (!pendingInjection.isEmpty()) {
96 throw new AssertionError("Failed to satisfy " + pendingInjection);
limpbizkit5fb9d922008-10-14 23:35:56 +000097 }
98
99 ready.countDown();
100 }
101
102 private class InjectableReference<T> implements Initializable<T> {
limpbizkitfcbdf992008-11-26 02:37:35 +0000103 private final InjectorImpl injector;
limpbizkit5fb9d922008-10-14 23:35:56 +0000104 private final T instance;
limpbizkit1db23932008-10-15 00:06:43 +0000105 private final Object source;
limpbizkit7cef5b02009-03-29 21:16:51 +0000106 private MembersInjectorImpl<T> membersInjector;
limpbizkit5fb9d922008-10-14 23:35:56 +0000107
limpbizkitfcbdf992008-11-26 02:37:35 +0000108 public InjectableReference(InjectorImpl injector, T instance, Object source) {
109 this.injector = injector;
limpbizkit5fb9d922008-10-14 23:35:56 +0000110 this.instance = checkNotNull(instance, "instance");
limpbizkit1db23932008-10-15 00:06:43 +0000111 this.source = checkNotNull(source, "source");
112 }
113
114 public void validate(Errors errors) throws ErrorsException {
limpbizkit7cef5b02009-03-29 21:16:51 +0000115 @SuppressWarnings("unchecked") // the type of 'T' is a TypeLiteral<T>
limpbizkit5ae41eb2009-06-06 17:51:27 +0000116 TypeLiteral<T> type = TypeLiteral.get((Class<T>) instance.getClass());
limpbizkit7cef5b02009-03-29 21:16:51 +0000117 membersInjector = injector.membersInjectorStore.get(type, errors.withSource(source));
limpbizkit5fb9d922008-10-14 23:35:56 +0000118 }
119
120 /**
limpbizkit1db23932008-10-15 00:06:43 +0000121 * 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.
limpbizkit5fb9d922008-10-14 23:35:56 +0000123 */
124 public T get(Errors errors) throws ErrorsException {
limpbizkit1db23932008-10-15 00:06:43 +0000125 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
limpbizkit8f370d32008-10-16 16:15:48 +0000140 // toInject needs injection, do it right away. we only do this once, even if it fails
limpbizkit1db23932008-10-15 00:06:43 +0000141 if (pendingInjection.remove(instance) != null) {
limpbizkita843a952009-04-08 22:24:55 +0000142 membersInjector.injectAndNotify(instance, errors.withSource(source));
limpbizkit1db23932008-10-15 00:06:43 +0000143 }
144
limpbizkit5fb9d922008-10-14 23:35:56 +0000145 return instance;
146 }
147
148 @Override public String toString() {
149 return instance.toString();
150 }
151 }
limpbizkit5fb9d922008-10-14 23:35:56 +0000152}