1/**
2 * Copyright (C) 2011 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
18package com.google.inject.internal;
19
20import static com.google.common.base.Preconditions.checkNotNull;
21
22import com.google.inject.internal.ProvisionListenerStackCallback.ProvisionCallback;
23import com.google.inject.spi.Dependency;
24
25import javax.inject.Provider;
26
27/**
28 * Base class for InternalFactories that are used by Providers, to handle
29 * circular dependencies.
30 *
31 * @author sameb@google.com (Sam Berlin)
32 */
33abstract class ProviderInternalFactory<T> implements InternalFactory<T> {
34
35  protected final Object source;
36
37  ProviderInternalFactory(Object source) {
38    this.source = checkNotNull(source, "source");
39  }
40
41  protected T circularGet(final Provider<? extends T> provider, final Errors errors,
42      InternalContext context, final Dependency<?> dependency,
43      ProvisionListenerStackCallback<T> provisionCallback)
44      throws ErrorsException {
45    final ConstructionContext<T> constructionContext = context.getConstructionContext(this);
46
47    // We have a circular reference between constructors. Return a proxy.
48    if (constructionContext.isConstructing()) {
49        Class<?> expectedType = dependency.getKey().getTypeLiteral().getRawType();
50        // TODO: if we can't proxy this object, can we proxy the other object?
51        @SuppressWarnings("unchecked")
52        T proxyType = (T) constructionContext.createProxy(
53            errors, context.getInjectorOptions(), expectedType);
54        return proxyType;
55    }
56
57    // Optimization: Don't go through the callback stack if no one's listening.
58    constructionContext.startConstruction();
59    try {
60      if (!provisionCallback.hasListeners()) {
61        return provision(provider, errors, dependency, constructionContext);
62      } else {
63        return provisionCallback.provision(errors, context, new ProvisionCallback<T>() {
64          public T call() throws ErrorsException {
65            return provision(provider, errors, dependency, constructionContext);
66          }
67        });
68      }
69    } finally {
70      constructionContext.removeCurrentReference();
71      constructionContext.finishConstruction();
72    }
73  }
74
75  /**
76   * Provisions a new instance. Subclasses should override this to catch
77   * exceptions & rethrow as ErrorsExceptions.
78   */
79  protected T provision(Provider<? extends T> provider, Errors errors, Dependency<?> dependency,
80      ConstructionContext<T> constructionContext) throws ErrorsException {
81    T t = errors.checkForNull(provider.get(), source, dependency);
82    constructionContext.setProxyDelegates(t);
83    return t;
84  }
85}
86