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 package com.google.inject.internal;
18 
19 import com.google.inject.Key;
20 import com.google.inject.ProvidedBy;
21 import com.google.inject.internal.InjectorImpl.JitLimitation;
22 import com.google.inject.spi.Dependency;
23 import javax.inject.Provider;
24 
25 /**
26  * An {@link InternalFactory} for {@literal @}{@link ProvidedBy} bindings.
27  *
28  * @author sameb@google.com (Sam Berlin)
29  */
30 class ProvidedByInternalFactory<T> extends ProviderInternalFactory<T> implements DelayedInitialize {
31 
32   private final Class<?> rawType;
33   private final Class<? extends Provider<?>> providerType;
34   private final Key<? extends Provider<T>> providerKey;
35   private BindingImpl<? extends Provider<T>> providerBinding;
36   private ProvisionListenerStackCallback<T> provisionCallback;
37 
ProvidedByInternalFactory( Class<?> rawType, Class<? extends Provider<?>> providerType, Key<? extends Provider<T>> providerKey)38   ProvidedByInternalFactory(
39       Class<?> rawType,
40       Class<? extends Provider<?>> providerType,
41       Key<? extends Provider<T>> providerKey) {
42     super(providerKey);
43     this.rawType = rawType;
44     this.providerType = providerType;
45     this.providerKey = providerKey;
46   }
47 
setProvisionListenerCallback(ProvisionListenerStackCallback<T> listener)48   void setProvisionListenerCallback(ProvisionListenerStackCallback<T> listener) {
49     provisionCallback = listener;
50   }
51 
52   @Override
initialize(InjectorImpl injector, Errors errors)53   public void initialize(InjectorImpl injector, Errors errors) throws ErrorsException {
54     providerBinding =
55         injector.getBindingOrThrow(providerKey, errors, JitLimitation.NEW_OR_EXISTING_JIT);
56   }
57 
58   @Override
get(InternalContext context, Dependency<?> dependency, boolean linked)59   public T get(InternalContext context, Dependency<?> dependency, boolean linked)
60       throws InternalProvisionException {
61     BindingImpl<? extends Provider<T>> localProviderBinding = providerBinding;
62     if (localProviderBinding == null) {
63       throw new IllegalStateException("not initialized");
64     }
65     Key<? extends Provider<T>> localProviderKey = providerKey;
66     context.pushState(localProviderKey, localProviderBinding.getSource());
67 
68     try {
69       Provider<? extends T> provider =
70           localProviderBinding.getInternalFactory().get(context, dependency, true);
71       return circularGet(provider, context, dependency, provisionCallback);
72     } catch (InternalProvisionException ipe) {
73       throw ipe.addSource(localProviderKey);
74       } finally {
75         context.popState();
76 
77     }
78   }
79 
80   @Override
provision( javax.inject.Provider<? extends T> provider, Dependency<?> dependency, ConstructionContext<T> constructionContext)81   protected T provision(
82       javax.inject.Provider<? extends T> provider,
83       Dependency<?> dependency,
84       ConstructionContext<T> constructionContext)
85       throws InternalProvisionException {
86     try {
87       Object o = super.provision(provider, dependency, constructionContext);
88       if (o != null && !rawType.isInstance(o)) {
89         throw InternalProvisionException.subtypeNotProvided(providerType, rawType);
90       }
91       @SuppressWarnings("unchecked") // protected by isInstance() check above
92       T t = (T) o;
93       return t;
94     } catch (RuntimeException e) {
95       throw InternalProvisionException.errorInProvider(e).addSource(source);
96     }
97   }
98 }
99