crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2006 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; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 18 | |
sberlin | b7a02b0 | 2011-07-08 00:34:16 +0000 | [diff] [blame] | 19 | import com.google.common.base.Objects; |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.common.collect.ImmutableMap; |
| 22 | import com.google.common.collect.ImmutableSet; |
| 23 | import com.google.common.collect.Lists; |
| 24 | import com.google.common.collect.Maps; |
guice.mirrorbot@gmail.com | efa4e9f | 2011-09-13 17:41:32 +0000 | [diff] [blame] | 25 | import com.google.common.collect.Sets; |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame] | 26 | import com.google.inject.Binder; |
| 27 | import com.google.inject.Binding; |
| 28 | import com.google.inject.ConfigurationException; |
| 29 | import com.google.inject.ImplementedBy; |
| 30 | import com.google.inject.Injector; |
| 31 | import com.google.inject.Key; |
| 32 | import com.google.inject.MembersInjector; |
| 33 | import com.google.inject.Module; |
| 34 | import com.google.inject.ProvidedBy; |
| 35 | import com.google.inject.Provider; |
| 36 | import com.google.inject.ProvisionException; |
limpbizkit | 1920322 | 2009-07-24 00:48:12 +0000 | [diff] [blame] | 37 | import com.google.inject.Scope; |
sberlin | 8b64d45 | 2010-12-13 02:44:36 +0000 | [diff] [blame] | 38 | import com.google.inject.Stage; |
limpbizkit | 48f3a91 | 2009-11-23 20:24:31 +0000 | [diff] [blame] | 39 | import com.google.inject.TypeLiteral; |
limpbizkit@gmail.com | 9a227be | 2010-07-03 15:51:31 +0000 | [diff] [blame] | 40 | import com.google.inject.internal.util.SourceProvider; |
limpbizkit | afa4b5d | 2008-08-02 18:40:47 +0000 | [diff] [blame] | 41 | import com.google.inject.spi.BindingTargetVisitor; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 42 | import com.google.inject.spi.ConvertedConstantBinding; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 43 | import com.google.inject.spi.Dependency; |
limpbizkit | 48f3a91 | 2009-11-23 20:24:31 +0000 | [diff] [blame] | 44 | import com.google.inject.spi.HasDependencies; |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 45 | import com.google.inject.spi.InjectionPoint; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 46 | import com.google.inject.spi.ProviderBinding; |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 47 | import com.google.inject.spi.TypeConverterBinding; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 48 | import com.google.inject.util.Providers; |
sberlin | b7a02b0 | 2011-07-08 00:34:16 +0000 | [diff] [blame] | 49 | |
limpbizkit | 48f3a91 | 2009-11-23 20:24:31 +0000 | [diff] [blame] | 50 | import java.lang.annotation.Annotation; |
limpbizkit | b0f3c9b | 2008-11-15 09:19:28 +0000 | [diff] [blame] | 51 | import java.lang.reflect.GenericArrayType; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 52 | import java.lang.reflect.InvocationTargetException; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 53 | import java.lang.reflect.ParameterizedType; |
| 54 | import java.lang.reflect.Type; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 55 | import java.util.Collections; |
sberlin | 7df9cf3 | 2010-05-25 12:25:46 +0000 | [diff] [blame] | 56 | import java.util.HashSet; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 57 | import java.util.List; |
| 58 | import java.util.Map; |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 59 | import java.util.Set; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 60 | |
| 61 | /** |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 62 | * Default {@link Injector} implementation. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 63 | * |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 64 | * @author crazybob@google.com (Bob Lee) |
| 65 | */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame] | 66 | final class InjectorImpl implements Injector, Lookups { |
limpbizkit | 3ea8095 | 2009-07-26 02:01:55 +0000 | [diff] [blame] | 67 | public static final TypeLiteral<String> STRING_TYPE = TypeLiteral.get(String.class); |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 68 | |
sberlin | 8b64d45 | 2010-12-13 02:44:36 +0000 | [diff] [blame] | 69 | /** Options that control how the injector behaves. */ |
| 70 | static class InjectorOptions { |
| 71 | final Stage stage; |
| 72 | final boolean jitDisabled; |
| 73 | final boolean disableCircularProxies; |
Sam Berlin | c756777 | 2012-05-31 19:54:04 -0400 | [diff] [blame] | 74 | final boolean atInjectRequired; |
Christian Edward Gruber | e391585 | 2013-05-16 11:00:54 -0700 | [diff] [blame] | 75 | final boolean exactBindingAnnotationsRequired; |
Christian Edward Gruber | 9e2d95b | 2013-06-26 17:43:11 -0700 | [diff] [blame] | 76 | |
Sam Berlin | c756777 | 2012-05-31 19:54:04 -0400 | [diff] [blame] | 77 | InjectorOptions(Stage stage, boolean jitDisabled, boolean disableCircularProxies, |
Christian Edward Gruber | e391585 | 2013-05-16 11:00:54 -0700 | [diff] [blame] | 78 | boolean atInjectRequired, boolean exactBindingAnnotationsRequired) { |
sberlin | 8b64d45 | 2010-12-13 02:44:36 +0000 | [diff] [blame] | 79 | this.stage = stage; |
| 80 | this.jitDisabled = jitDisabled; |
| 81 | this.disableCircularProxies = disableCircularProxies; |
Sam Berlin | c756777 | 2012-05-31 19:54:04 -0400 | [diff] [blame] | 82 | this.atInjectRequired = atInjectRequired; |
Christian Edward Gruber | e391585 | 2013-05-16 11:00:54 -0700 | [diff] [blame] | 83 | this.exactBindingAnnotationsRequired = exactBindingAnnotationsRequired; |
sberlin | 8b64d45 | 2010-12-13 02:44:36 +0000 | [diff] [blame] | 84 | } |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 85 | |
sberlin | 8b64d45 | 2010-12-13 02:44:36 +0000 | [diff] [blame] | 86 | @Override |
| 87 | public String toString() { |
sberlin | d9c913a | 2011-06-26 21:02:54 +0000 | [diff] [blame] | 88 | return Objects.toStringHelper(getClass()) |
sberlin | 8b64d45 | 2010-12-13 02:44:36 +0000 | [diff] [blame] | 89 | .add("stage", stage) |
| 90 | .add("jitDisabled", jitDisabled) |
| 91 | .add("disableCircularProxies", disableCircularProxies) |
Sam Berlin | c756777 | 2012-05-31 19:54:04 -0400 | [diff] [blame] | 92 | .add("atInjectRequired", atInjectRequired) |
Christian Edward Gruber | e391585 | 2013-05-16 11:00:54 -0700 | [diff] [blame] | 93 | .add("exactBindingAnnotationsRequired", exactBindingAnnotationsRequired) |
sberlin | 8b64d45 | 2010-12-13 02:44:36 +0000 | [diff] [blame] | 94 | .toString(); |
| 95 | } |
| 96 | } |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 97 | |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 98 | /** some limitations on what just in time bindings are allowed. */ |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 99 | enum JitLimitation { |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 100 | /** does not allow just in time bindings */ |
| 101 | NO_JIT, |
| 102 | /** allows existing just in time bindings, but does not allow new ones */ |
| 103 | EXISTING_JIT, |
Tavian Barnes | 6c9726f | 2015-01-07 10:37:17 -0500 | [diff] [blame^] | 104 | /** allows existing just in time bindings & allows new ones to be created */ |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 105 | NEW_OR_EXISTING_JIT, |
| 106 | } |
limpbizkit | 3ea8095 | 2009-07-26 02:01:55 +0000 | [diff] [blame] | 107 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 108 | final State state; |
| 109 | final InjectorImpl parent; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 110 | final BindingsMultimap bindingsMultimap = new BindingsMultimap(); |
sberlin | c2a6625 | 2010-02-13 16:16:44 +0000 | [diff] [blame] | 111 | final InjectorOptions options; |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 112 | |
limpbizkit | 79dc99b | 2008-12-09 19:02:12 +0000 | [diff] [blame] | 113 | /** Just-in-time binding cache. Guarded by state.lock() */ |
| 114 | final Map<Key<?>, BindingImpl<?>> jitBindings = Maps.newHashMap(); |
guice.mirrorbot@gmail.com | efa4e9f | 2011-09-13 17:41:32 +0000 | [diff] [blame] | 115 | /** |
| 116 | * Cache of Keys that we were unable to create JIT bindings for, so we don't |
| 117 | * keep trying. Also guarded by state.lock(). |
| 118 | */ |
| 119 | final Set<Key<?>> failedJitBindings = Sets.newHashSet(); |
limpbizkit | 79dc99b | 2008-12-09 19:02:12 +0000 | [diff] [blame] | 120 | |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 121 | Lookups lookups = new DeferredLookups(this); |
| 122 | |
sberlin | d9c913a | 2011-06-26 21:02:54 +0000 | [diff] [blame] | 123 | InjectorImpl(InjectorImpl parent, State state, InjectorOptions injectorOptions) { |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 124 | this.parent = parent; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 125 | this.state = state; |
sberlin | c2a6625 | 2010-02-13 16:16:44 +0000 | [diff] [blame] | 126 | this.options = injectorOptions; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 127 | |
| 128 | if (parent != null) { |
| 129 | localContext = parent.localContext; |
| 130 | } else { |
Sam Berlin | ca81e59 | 2014-07-01 16:52:59 -0400 | [diff] [blame] | 131 | localContext = new ThreadLocal<Object[]>(); |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 132 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 133 | } |
| 134 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 135 | /** Indexes bindings by type. */ |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 136 | void index() { |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 137 | for (Binding<?> binding : state.getExplicitBindingsThisLevel().values()) { |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 138 | index(binding); |
| 139 | } |
| 140 | } |
| 141 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 142 | <T> void index(Binding<T> binding) { |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 143 | bindingsMultimap.put(binding.getKey().getTypeLiteral(), binding); |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 144 | } |
| 145 | |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 146 | public <T> List<Binding<T>> findBindingsByType(TypeLiteral<T> type) { |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 147 | return bindingsMultimap.getAll(type); |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 148 | } |
| 149 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 150 | /** Returns the binding for {@code key} */ |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 151 | public <T> BindingImpl<T> getBinding(Key<T> key) { |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 152 | Errors errors = new Errors(key); |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 153 | try { |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 154 | BindingImpl<T> result = getBindingOrThrow(key, errors, JitLimitation.EXISTING_JIT); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 155 | errors.throwConfigurationExceptionIfErrorsExist(); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 156 | return result; |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 157 | } catch (ErrorsException e) { |
| 158 | throw new ConfigurationException(errors.merge(e.getErrors()).getMessages()); |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
sberlin | 6760625 | 2010-03-10 22:18:52 +0000 | [diff] [blame] | 162 | public <T> BindingImpl<T> getExistingBinding(Key<T> key) { |
| 163 | // Check explicit bindings, i.e. bindings created by modules. |
| 164 | BindingImpl<T> explicitBinding = state.getExplicitBinding(key); |
| 165 | if (explicitBinding != null) { |
| 166 | return explicitBinding; |
| 167 | } |
| 168 | synchronized (state.lock()) { |
| 169 | // See if any jit bindings have been created for this key. |
| 170 | for (InjectorImpl injector = this; injector != null; injector = injector.parent) { |
| 171 | @SuppressWarnings("unchecked") |
| 172 | BindingImpl<T> jitBinding = (BindingImpl<T>) injector.jitBindings.get(key); |
| 173 | if(jitBinding != null) { |
| 174 | return jitBinding; |
| 175 | } |
| 176 | } |
| 177 | } |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 178 | |
sberlin | 6760625 | 2010-03-10 22:18:52 +0000 | [diff] [blame] | 179 | // If Key is a Provider, we have to see if the type it is providing exists, |
| 180 | // and, if so, we have to create the binding for the provider. |
| 181 | if(isProvider(key)) { |
| 182 | try { |
| 183 | // This is safe because isProvider above ensures that T is a Provider<?> |
Sam Berlin | c756777 | 2012-05-31 19:54:04 -0400 | [diff] [blame] | 184 | @SuppressWarnings({"unchecked", "cast"}) |
sberlin | 6760625 | 2010-03-10 22:18:52 +0000 | [diff] [blame] | 185 | Key<?> providedKey = (Key<?>)getProvidedKey((Key)key, new Errors()); |
| 186 | if(getExistingBinding(providedKey) != null) { |
| 187 | return getBinding(key); |
| 188 | } |
| 189 | } catch(ErrorsException e) { |
| 190 | throw new ConfigurationException(e.getErrors().getMessages()); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // No existing binding exists. |
| 195 | return null; |
| 196 | } |
| 197 | |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 198 | /** |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 199 | * Gets a binding implementation. First, it check to see if the parent has a binding. If the |
| 200 | * parent has a binding and the binding is scoped, it will use that binding. Otherwise, this |
| 201 | * checks for an explicit binding. If no explicit binding is found, it looks for a just-in-time |
| 202 | * binding. |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 203 | */ |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 204 | <T> BindingImpl<T> getBindingOrThrow(Key<T> key, Errors errors, JitLimitation jitType) |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 205 | throws ErrorsException { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 206 | // Check explicit bindings, i.e. bindings created by modules. |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 207 | BindingImpl<T> binding = state.getExplicitBinding(key); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 208 | if (binding != null) { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 209 | return binding; |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 210 | } |
| 211 | |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 212 | // Look for an on-demand binding. |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 213 | return getJustInTimeBinding(key, errors, jitType); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 214 | } |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 215 | |
crazyboblee | 552472f | 2007-09-07 16:52:39 +0000 | [diff] [blame] | 216 | public <T> Binding<T> getBinding(Class<T> type) { |
| 217 | return getBinding(Key.get(type)); |
| 218 | } |
| 219 | |
limpbizkit | edd8d64 | 2008-10-18 19:50:49 +0000 | [diff] [blame] | 220 | public Injector getParent() { |
| 221 | return parent; |
| 222 | } |
| 223 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 224 | public Injector createChildInjector(Iterable<? extends Module> modules) { |
sberlin | c2a6625 | 2010-02-13 16:16:44 +0000 | [diff] [blame] | 225 | return new InternalInjectorCreator() |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 226 | .parentInjector(this) |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 227 | .addModules(modules) |
| 228 | .build(); |
| 229 | } |
| 230 | |
| 231 | public Injector createChildInjector(Module... modules) { |
sberlin | 5221c15 | 2011-07-22 21:13:53 +0000 | [diff] [blame] | 232 | return createChildInjector(ImmutableList.copyOf(modules)); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 233 | } |
| 234 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 235 | /** |
| 236 | * Returns a just-in-time binding for {@code key}, creating it if necessary. |
| 237 | * |
limpbizkit | 79dc99b | 2008-12-09 19:02:12 +0000 | [diff] [blame] | 238 | * @throws ErrorsException if the binding could not be created. |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 239 | */ |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 240 | private <T> BindingImpl<T> getJustInTimeBinding(Key<T> key, Errors errors, JitLimitation jitType) |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 241 | throws ErrorsException { |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 242 | |
guice.mirrorbot@gmail.com | efa4e9f | 2011-09-13 17:41:32 +0000 | [diff] [blame] | 243 | boolean jitOverride = isProvider(key) || isTypeLiteral(key) || isMembersInjector(key); |
limpbizkit | a8dccb3 | 2008-11-11 22:41:56 +0000 | [diff] [blame] | 244 | synchronized (state.lock()) { |
limpbizkit | 79dc99b | 2008-12-09 19:02:12 +0000 | [diff] [blame] | 245 | // first try to find a JIT binding that we've already created |
| 246 | for (InjectorImpl injector = this; injector != null; injector = injector.parent) { |
| 247 | @SuppressWarnings("unchecked") // we only store bindings that match their key |
| 248 | BindingImpl<T> binding = (BindingImpl<T>) injector.jitBindings.get(key); |
| 249 | |
| 250 | if (binding != null) { |
sberlin | 9cdfe3a | 2011-03-02 13:53:17 +0000 | [diff] [blame] | 251 | // If we found a JIT binding and we don't allow them, |
| 252 | // fail. (But allow bindings created through TypeConverters.) |
| 253 | if (options.jitDisabled |
| 254 | && jitType == JitLimitation.NO_JIT |
| 255 | && !jitOverride |
| 256 | && !(binding instanceof ConvertedConstantBindingImpl)) { |
| 257 | throw errors.jitDisabled(key).toException(); |
| 258 | } else { |
| 259 | return binding; |
| 260 | } |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 261 | } |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 262 | } |
Christian Edward Gruber | 9e2d95b | 2013-06-26 17:43:11 -0700 | [diff] [blame] | 263 | |
guice.mirrorbot@gmail.com | efa4e9f | 2011-09-13 17:41:32 +0000 | [diff] [blame] | 264 | // If we previously failed creating this JIT binding and our Errors has |
| 265 | // already recorded an error, then just directly throw that error. |
| 266 | // We need to do this because it's possible we already cleaned up the |
| 267 | // entry in jitBindings (during cleanup), and we may be trying |
| 268 | // to create it again (in the case of a recursive JIT binding). |
| 269 | // We need both of these guards for different reasons |
| 270 | // failedJitBindings.contains: We want to continue processing if we've never |
| 271 | // failed before, so that our initial error message contains |
| 272 | // as much useful information as possible about what errors exist. |
| 273 | // errors.hasErrors: If we haven't already failed, then it's OK to |
| 274 | // continue processing, to make sure the ultimate error message |
| 275 | // is the correct one. |
| 276 | // See: ImplicitBindingsTest#testRecursiveJitBindingsCleanupCorrectly |
| 277 | // for where this guard compes into play. |
| 278 | if (failedJitBindings.contains(key) && errors.hasErrors()) { |
| 279 | throw errors.toException(); |
| 280 | } |
sberlin | 9cdfe3a | 2011-03-02 13:53:17 +0000 | [diff] [blame] | 281 | return createJustInTimeBindingRecursive(key, errors, options.jitDisabled, jitType); |
guice.mirrorbot@gmail.com | efa4e9f | 2011-09-13 17:41:32 +0000 | [diff] [blame] | 282 | } // end synchronized(state.lock()) |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 283 | } |
| 284 | |
limpbizkit | 0ca7f91 | 2009-03-28 19:11:40 +0000 | [diff] [blame] | 285 | /** Returns true if the key type is Provider (but not a subclass of Provider). */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame] | 286 | private static boolean isProvider(Key<?> key) { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 287 | return key.getTypeLiteral().getRawType().equals(Provider.class); |
| 288 | } |
Christian Edward Gruber | 9e2d95b | 2013-06-26 17:43:11 -0700 | [diff] [blame] | 289 | |
sberlin@gmail.com | b1fa81f | 2010-08-26 22:36:19 +0000 | [diff] [blame] | 290 | private static boolean isTypeLiteral(Key<?> key) { |
| 291 | return key.getTypeLiteral().getRawType().equals(TypeLiteral.class); |
| 292 | } |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 293 | |
sberlin | 6760625 | 2010-03-10 22:18:52 +0000 | [diff] [blame] | 294 | private static <T> Key<T> getProvidedKey(Key<Provider<T>> key, Errors errors) throws ErrorsException { |
| 295 | Type providerType = key.getTypeLiteral().getType(); |
| 296 | |
| 297 | // If the Provider has no type parameter (raw Provider)... |
| 298 | if (!(providerType instanceof ParameterizedType)) { |
| 299 | throw errors.cannotInjectRawProvider().toException(); |
| 300 | } |
| 301 | |
| 302 | Type entryType = ((ParameterizedType) providerType).getActualTypeArguments()[0]; |
| 303 | |
| 304 | @SuppressWarnings("unchecked") // safe because T came from Key<Provider<T>> |
| 305 | Key<T> providedKey = (Key<T>) key.ofType(entryType); |
| 306 | return providedKey; |
| 307 | } |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 308 | |
limpbizkit | 0ca7f91 | 2009-03-28 19:11:40 +0000 | [diff] [blame] | 309 | /** Returns true if the key type is MembersInjector (but not a subclass of MembersInjector). */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame] | 310 | private static boolean isMembersInjector(Key<?> key) { |
limpbizkit | 0ca7f91 | 2009-03-28 19:11:40 +0000 | [diff] [blame] | 311 | return key.getTypeLiteral().getRawType().equals(MembersInjector.class) |
Sam Berlin | c00df28 | 2014-07-01 16:53:41 -0400 | [diff] [blame] | 312 | && key.getAnnotationType() == null; |
limpbizkit | 0ca7f91 | 2009-03-28 19:11:40 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | private <T> BindingImpl<MembersInjector<T>> createMembersInjectorBinding( |
| 316 | Key<MembersInjector<T>> key, Errors errors) throws ErrorsException { |
| 317 | Type membersInjectorType = key.getTypeLiteral().getType(); |
| 318 | if (!(membersInjectorType instanceof ParameterizedType)) { |
| 319 | throw errors.cannotInjectRawMembersInjector().toException(); |
| 320 | } |
| 321 | |
| 322 | @SuppressWarnings("unchecked") // safe because T came from Key<MembersInjector<T>> |
| 323 | TypeLiteral<T> instanceType = (TypeLiteral<T>) TypeLiteral.get( |
| 324 | ((ParameterizedType) membersInjectorType).getActualTypeArguments()[0]); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 325 | MembersInjector<T> membersInjector = membersInjectorStore.get(instanceType, errors); |
limpbizkit | 0ca7f91 | 2009-03-28 19:11:40 +0000 | [diff] [blame] | 326 | |
| 327 | InternalFactory<MembersInjector<T>> factory = new ConstantFactory<MembersInjector<T>>( |
| 328 | Initializables.of(membersInjector)); |
| 329 | |
| 330 | |
limpbizkit@gmail.com | 9a227be | 2010-07-03 15:51:31 +0000 | [diff] [blame] | 331 | return new InstanceBindingImpl<MembersInjector<T>>(this, key, SourceProvider.UNKNOWN_SOURCE, |
limpbizkit | 0ca7f91 | 2009-03-28 19:11:40 +0000 | [diff] [blame] | 332 | factory, ImmutableSet.<InjectionPoint>of(), membersInjector); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Creates a synthetic binding to {@code Provider<T>}, i.e. a binding to the provider from |
| 337 | * {@code Binding<T>}. |
| 338 | */ |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 339 | private <T> BindingImpl<Provider<T>> createProviderBinding(Key<Provider<T>> key, Errors errors) |
| 340 | throws ErrorsException { |
sberlin | 6760625 | 2010-03-10 22:18:52 +0000 | [diff] [blame] | 341 | Key<T> providedKey = getProvidedKey(key, errors); |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 342 | BindingImpl<T> delegate = getBindingOrThrow(providedKey, errors, JitLimitation.NO_JIT); |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 343 | return new ProviderBindingImpl<T>(this, key, delegate); |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 344 | } |
| 345 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame] | 346 | private static class ProviderBindingImpl<T> extends BindingImpl<Provider<T>> |
limpbizkit | 1920322 | 2009-07-24 00:48:12 +0000 | [diff] [blame] | 347 | implements ProviderBinding<Provider<T>>, HasDependencies { |
limpbizkit | a886599 | 2008-08-21 07:09:59 +0000 | [diff] [blame] | 348 | final BindingImpl<T> providedBinding; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 349 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 350 | ProviderBindingImpl(InjectorImpl injector, Key<Provider<T>> key, Binding<T> providedBinding) { |
| 351 | super(injector, key, providedBinding.getSource(), createInternalFactory(providedBinding), |
| 352 | Scoping.UNSCOPED); |
limpbizkit | a886599 | 2008-08-21 07:09:59 +0000 | [diff] [blame] | 353 | this.providedBinding = (BindingImpl<T>) providedBinding; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 354 | } |
| 355 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 356 | static <T> InternalFactory<Provider<T>> createInternalFactory(Binding<T> providedBinding) { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 357 | final Provider<T> provider = providedBinding.getProvider(); |
| 358 | return new InternalFactory<Provider<T>>() { |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 359 | public Provider<T> get(Errors errors, InternalContext context, Dependency dependency, boolean linked) { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 360 | return provider; |
| 361 | } |
| 362 | }; |
| 363 | } |
| 364 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 365 | public Key<? extends T> getProvidedKey() { |
| 366 | return providedBinding.getKey(); |
| 367 | } |
| 368 | |
limpbizkit | 8996e80 | 2008-12-28 01:44:29 +0000 | [diff] [blame] | 369 | public <V> V acceptTargetVisitor(BindingTargetVisitor<? super Provider<T>, V> visitor) { |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 370 | return visitor.visit(this); |
| 371 | } |
| 372 | |
| 373 | public void applyTo(Binder binder) { |
| 374 | throw new UnsupportedOperationException("This element represents a synthetic binding."); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 375 | } |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 376 | |
| 377 | @Override public String toString() { |
sberlin | d9c913a | 2011-06-26 21:02:54 +0000 | [diff] [blame] | 378 | return Objects.toStringHelper(ProviderBinding.class) |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 379 | .add("key", getKey()) |
| 380 | .add("providedKey", getProvidedKey()) |
| 381 | .toString(); |
| 382 | } |
limpbizkit | 1920322 | 2009-07-24 00:48:12 +0000 | [diff] [blame] | 383 | |
| 384 | public Set<Dependency<?>> getDependencies() { |
| 385 | return ImmutableSet.<Dependency<?>>of(Dependency.get(getProvidedKey())); |
| 386 | } |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 387 | |
sberlin@gmail.com | 7bcec88 | 2010-06-24 01:03:26 +0000 | [diff] [blame] | 388 | @Override |
| 389 | public boolean equals(Object obj) { |
| 390 | if(obj instanceof ProviderBindingImpl) { |
| 391 | ProviderBindingImpl<?> o = (ProviderBindingImpl<?>)obj; |
| 392 | return getKey().equals(o.getKey()) |
| 393 | && getScoping().equals(o.getScoping()) |
| 394 | && Objects.equal(providedBinding, o.providedBinding); |
| 395 | } else { |
| 396 | return false; |
| 397 | } |
| 398 | } |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 399 | |
sberlin@gmail.com | 7bcec88 | 2010-06-24 01:03:26 +0000 | [diff] [blame] | 400 | @Override |
| 401 | public int hashCode() { |
| 402 | return Objects.hashCode(getKey(), getScoping(), providedBinding); |
| 403 | } |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 404 | } |
| 405 | |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 406 | /** |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 407 | * Converts a constant string binding to the required type. |
| 408 | * |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 409 | * @return the binding if it could be resolved, or null if the binding doesn't exist |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 410 | * @throws com.google.inject.internal.ErrorsException if there was an error resolving the binding |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 411 | */ |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 412 | private <T> BindingImpl<T> convertConstantStringBinding(Key<T> key, Errors errors) |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 413 | throws ErrorsException { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 414 | // Find a constant string binding. |
limpbizkit | 3ea8095 | 2009-07-26 02:01:55 +0000 | [diff] [blame] | 415 | Key<String> stringKey = key.ofType(STRING_TYPE); |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 416 | BindingImpl<String> stringBinding = state.getExplicitBinding(stringKey); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 417 | if (stringBinding == null || !stringBinding.isConstant()) { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 418 | return null; |
| 419 | } |
| 420 | |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 421 | String stringValue = stringBinding.getProvider().get(); |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 422 | Object source = stringBinding.getSource(); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 423 | |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 424 | // Find a matching type converter. |
| 425 | TypeLiteral<T> type = key.getTypeLiteral(); |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 426 | TypeConverterBinding typeConverterBinding = state.getConverter(stringValue, type, errors, source); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 427 | |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 428 | if (typeConverterBinding == null) { |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 429 | // No converter can handle the given type. |
| 430 | return null; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 431 | } |
| 432 | |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 433 | // Try to convert the string. A failed conversion results in an error. |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 434 | try { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 435 | @SuppressWarnings("unchecked") // This cast is safe because we double check below. |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 436 | T converted = (T) typeConverterBinding.getTypeConverter().convert(stringValue, type); |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 437 | |
| 438 | if (converted == null) { |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 439 | throw errors.converterReturnedNull(stringValue, source, type, typeConverterBinding) |
limpbizkit | c5c488e | 2008-06-16 01:42:25 +0000 | [diff] [blame] | 440 | .toException(); |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 441 | } |
| 442 | |
limpbizkit | c0fe03b | 2008-06-02 04:46:53 +0000 | [diff] [blame] | 443 | if (!type.getRawType().isInstance(converted)) { |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 444 | throw errors.conversionTypeError(stringValue, source, type, typeConverterBinding, converted) |
limpbizkit | c5c488e | 2008-06-16 01:42:25 +0000 | [diff] [blame] | 445 | .toException(); |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 446 | } |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 447 | |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 448 | return new ConvertedConstantBindingImpl<T>(this, key, converted, stringBinding, |
| 449 | typeConverterBinding); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 450 | } catch (ErrorsException e) { |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 451 | throw e; |
limpbizkit | 516e2ab | 2009-03-26 22:01:18 +0000 | [diff] [blame] | 452 | } catch (RuntimeException e) { |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 453 | throw errors.conversionError(stringValue, source, type, typeConverterBinding, e) |
limpbizkit | c5c488e | 2008-06-16 01:42:25 +0000 | [diff] [blame] | 454 | .toException(); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 458 | private static class ConvertedConstantBindingImpl<T> |
| 459 | extends BindingImpl<T> implements ConvertedConstantBinding<T> { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 460 | final T value; |
| 461 | final Provider<T> provider; |
| 462 | final Binding<String> originalBinding; |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 463 | final TypeConverterBinding typeConverterBinding; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 464 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 465 | ConvertedConstantBindingImpl( |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 466 | InjectorImpl injector, Key<T> key, T value, Binding<String> originalBinding, |
| 467 | TypeConverterBinding typeConverterBinding) { |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 468 | super(injector, key, originalBinding.getSource(), |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 469 | new ConstantFactory<T>(Initializables.of(value)), Scoping.UNSCOPED); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 470 | this.value = value; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 471 | provider = Providers.of(value); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 472 | this.originalBinding = originalBinding; |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 473 | this.typeConverterBinding = typeConverterBinding; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 474 | } |
| 475 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 476 | @Override public Provider<T> getProvider() { |
| 477 | return provider; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 478 | } |
| 479 | |
limpbizkit | 8996e80 | 2008-12-28 01:44:29 +0000 | [diff] [blame] | 480 | public <V> V acceptTargetVisitor(BindingTargetVisitor<? super T, V> visitor) { |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 481 | return visitor.visit(this); |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | public T getValue() { |
| 485 | return value; |
| 486 | } |
| 487 | |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 488 | public TypeConverterBinding getTypeConverterBinding() { |
| 489 | return typeConverterBinding; |
| 490 | } |
| 491 | |
limpbizkit | c45600e | 2008-12-27 02:57:04 +0000 | [diff] [blame] | 492 | public Key<String> getSourceKey() { |
| 493 | return originalBinding.getKey(); |
| 494 | } |
| 495 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 496 | public Set<Dependency<?>> getDependencies() { |
limpbizkit | c45600e | 2008-12-27 02:57:04 +0000 | [diff] [blame] | 497 | return ImmutableSet.<Dependency<?>>of(Dependency.get(getSourceKey())); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 498 | } |
| 499 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 500 | public void applyTo(Binder binder) { |
| 501 | throw new UnsupportedOperationException("This element represents a synthetic binding."); |
| 502 | } |
| 503 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 504 | @Override public String toString() { |
sberlin | d9c913a | 2011-06-26 21:02:54 +0000 | [diff] [blame] | 505 | return Objects.toStringHelper(ConvertedConstantBinding.class) |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 506 | .add("key", getKey()) |
limpbizkit | c45600e | 2008-12-27 02:57:04 +0000 | [diff] [blame] | 507 | .add("sourceKey", getSourceKey()) |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 508 | .add("value", value) |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 509 | .toString(); |
| 510 | } |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 511 | |
sberlin@gmail.com | 7bcec88 | 2010-06-24 01:03:26 +0000 | [diff] [blame] | 512 | @Override |
| 513 | public boolean equals(Object obj) { |
| 514 | if(obj instanceof ConvertedConstantBindingImpl) { |
| 515 | ConvertedConstantBindingImpl<?> o = (ConvertedConstantBindingImpl<?>)obj; |
| 516 | return getKey().equals(o.getKey()) |
| 517 | && getScoping().equals(o.getScoping()) |
| 518 | && Objects.equal(value, o.value); |
| 519 | } else { |
| 520 | return false; |
| 521 | } |
| 522 | } |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 523 | |
sberlin@gmail.com | 7bcec88 | 2010-06-24 01:03:26 +0000 | [diff] [blame] | 524 | @Override |
| 525 | public int hashCode() { |
| 526 | return Objects.hashCode(getKey(), getScoping(), value); |
| 527 | } |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 528 | } |
| 529 | |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 530 | <T> void initializeBinding(BindingImpl<T> binding, Errors errors) throws ErrorsException { |
sberlin | d5737a6 | 2011-05-02 18:03:35 +0000 | [diff] [blame] | 531 | if (binding instanceof DelayedInitialize) { |
| 532 | ((DelayedInitialize) binding).initialize(this, errors); |
limpbizkit | 91425c3 | 2009-07-17 03:10:13 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
| 536 | <T> void initializeJitBinding(BindingImpl<T> binding, Errors errors) throws ErrorsException { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 537 | // Put the partially constructed binding in the map a little early. This enables us to handle |
| 538 | // circular dependencies. Example: FooImpl -> BarImpl -> FooImpl. |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 539 | // Note: We don't need to synchronize on state.lock() during injector creation. |
sberlin | d5737a6 | 2011-05-02 18:03:35 +0000 | [diff] [blame] | 540 | if (binding instanceof DelayedInitialize) { |
limpbizkit | 5141187 | 2008-05-13 22:15:29 +0000 | [diff] [blame] | 541 | Key<T> key = binding.getKey(); |
| 542 | jitBindings.put(key, binding); |
| 543 | boolean successful = false; |
sberlin | d5737a6 | 2011-05-02 18:03:35 +0000 | [diff] [blame] | 544 | DelayedInitialize delayed = (DelayedInitialize)binding; |
limpbizkit | 5141187 | 2008-05-13 22:15:29 +0000 | [diff] [blame] | 545 | try { |
sberlin | d5737a6 | 2011-05-02 18:03:35 +0000 | [diff] [blame] | 546 | delayed.initialize(this, errors); |
limpbizkit | 5141187 | 2008-05-13 22:15:29 +0000 | [diff] [blame] | 547 | successful = true; |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 548 | } finally { |
limpbizkit | 5141187 | 2008-05-13 22:15:29 +0000 | [diff] [blame] | 549 | if (!successful) { |
sberlin | 7df9cf3 | 2010-05-25 12:25:46 +0000 | [diff] [blame] | 550 | // We do not pass cb.getInternalConstructor as the second parameter |
| 551 | // so that cached exceptions while constructing it get stored. |
| 552 | // See TypeListenerTest#testTypeListenerThrows |
Sam Berlin | d51292d | 2012-02-26 21:23:19 -0500 | [diff] [blame] | 553 | removeFailedJitBinding(binding, null); |
sberlin | 7df9cf3 | 2010-05-25 12:25:46 +0000 | [diff] [blame] | 554 | cleanup(binding, new HashSet<Key>()); |
limpbizkit | 5141187 | 2008-05-13 22:15:29 +0000 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | } |
| 558 | } |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 559 | |
sberlin | 7df9cf3 | 2010-05-25 12:25:46 +0000 | [diff] [blame] | 560 | /** |
| 561 | * Iterates through the binding's dependencies to clean up any stray bindings that were leftover |
| 562 | * from a failed JIT binding. This is required because the bindings are eagerly & |
| 563 | * optimistically added to allow circular dependency support, so dependencies may pass where they |
| 564 | * should have failed. |
| 565 | */ |
| 566 | private boolean cleanup(BindingImpl<?> binding, Set<Key> encountered) { |
| 567 | boolean bindingFailed = false; |
| 568 | Set<Dependency<?>> deps = getInternalDependencies(binding); |
| 569 | for(Dependency dep : deps) { |
| 570 | Key<?> depKey = dep.getKey(); |
| 571 | InjectionPoint ip = dep.getInjectionPoint(); |
| 572 | if(encountered.add(depKey)) { // only check if we haven't looked at this key yet |
| 573 | BindingImpl depBinding = jitBindings.get(depKey); |
| 574 | if(depBinding != null) { // if the binding still exists, validate |
| 575 | boolean failed = cleanup(depBinding, encountered); // if children fail, we fail |
| 576 | if(depBinding instanceof ConstructorBindingImpl) { |
| 577 | ConstructorBindingImpl ctorBinding = (ConstructorBindingImpl)depBinding; |
| 578 | ip = ctorBinding.getInternalConstructor(); |
| 579 | if(!ctorBinding.isInitialized()) { |
| 580 | failed = true; |
| 581 | } |
| 582 | } |
| 583 | if(failed) { |
Sam Berlin | d51292d | 2012-02-26 21:23:19 -0500 | [diff] [blame] | 584 | removeFailedJitBinding(depBinding, ip); |
sberlin | 7df9cf3 | 2010-05-25 12:25:46 +0000 | [diff] [blame] | 585 | bindingFailed = true; |
| 586 | } |
| 587 | } else if(state.getExplicitBinding(depKey) == null) { |
| 588 | // ignore keys if they were explicitly bound, but if neither JIT |
| 589 | // nor explicit, it's also invalid & should let parent know. |
| 590 | bindingFailed = true; |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | return bindingFailed; |
| 595 | } |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 596 | |
sberlin | 7df9cf3 | 2010-05-25 12:25:46 +0000 | [diff] [blame] | 597 | /** Cleans up any state that may have been cached when constructing the JIT binding. */ |
Sam Berlin | d51292d | 2012-02-26 21:23:19 -0500 | [diff] [blame] | 598 | private void removeFailedJitBinding(Binding<?> binding, InjectionPoint ip) { |
| 599 | failedJitBindings.add(binding.getKey()); |
| 600 | jitBindings.remove(binding.getKey()); |
| 601 | membersInjectorStore.remove(binding.getKey().getTypeLiteral()); |
| 602 | provisionListenerStore.remove(binding); |
sberlin | 7df9cf3 | 2010-05-25 12:25:46 +0000 | [diff] [blame] | 603 | if(ip != null) { |
| 604 | constructors.remove(ip); |
| 605 | } |
| 606 | } |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 607 | |
sberlin | 7df9cf3 | 2010-05-25 12:25:46 +0000 | [diff] [blame] | 608 | /** Safely gets the dependencies of possibly not initialized bindings. */ |
| 609 | @SuppressWarnings("unchecked") |
| 610 | private Set<Dependency<?>> getInternalDependencies(BindingImpl<?> binding) { |
| 611 | if(binding instanceof ConstructorBindingImpl) { |
| 612 | return ((ConstructorBindingImpl)binding).getInternalDependencies(); |
sberlin | 47e69ef | 2010-07-03 15:35:50 +0000 | [diff] [blame] | 613 | } else if(binding instanceof HasDependencies) { |
sberlin | 7df9cf3 | 2010-05-25 12:25:46 +0000 | [diff] [blame] | 614 | return ((HasDependencies)binding).getDependencies(); |
sberlin | 47e69ef | 2010-07-03 15:35:50 +0000 | [diff] [blame] | 615 | } else { |
| 616 | return ImmutableSet.of(); |
sberlin | 7df9cf3 | 2010-05-25 12:25:46 +0000 | [diff] [blame] | 617 | } |
| 618 | } |
limpbizkit | 5141187 | 2008-05-13 22:15:29 +0000 | [diff] [blame] | 619 | |
| 620 | /** |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 621 | * Creates a binding for an injectable type with the given scope. Looks for a scope on the type if |
| 622 | * none is specified. |
limpbizkit | 5141187 | 2008-05-13 22:15:29 +0000 | [diff] [blame] | 623 | */ |
limpbizkit | f374014 | 2009-06-22 00:14:02 +0000 | [diff] [blame] | 624 | <T> BindingImpl<T> createUninitializedBinding(Key<T> key, Scoping scoping, Object source, |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 625 | Errors errors, boolean jitBinding) throws ErrorsException { |
limpbizkit | 4272aef | 2008-11-23 08:10:09 +0000 | [diff] [blame] | 626 | Class<?> rawType = key.getTypeLiteral().getRawType(); |
limpbizkit | 8ba9788 | 2008-11-04 02:52:54 +0000 | [diff] [blame] | 627 | |
Sam Berlin | 1285790 | 2014-08-06 11:54:29 -0400 | [diff] [blame] | 628 | ImplementedBy implementedBy = rawType.getAnnotation(ImplementedBy.class); |
| 629 | |
| 630 | // Don't try to inject arrays or enums annotated with @ImplementedBy. |
| 631 | if (rawType.isArray() || (rawType.isEnum() && implementedBy != null)) { |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 632 | throw errors.missingImplementation(key).toException(); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 633 | } |
| 634 | |
limpbizkit | b0f3c9b | 2008-11-15 09:19:28 +0000 | [diff] [blame] | 635 | // Handle TypeLiteral<T> by binding the inner type |
| 636 | if (rawType == TypeLiteral.class) { |
| 637 | @SuppressWarnings("unchecked") // we have to fudge the inner type as Object |
| 638 | BindingImpl<T> binding = (BindingImpl<T>) createTypeLiteralBinding( |
| 639 | (Key<TypeLiteral<Object>>) key, errors); |
| 640 | return binding; |
| 641 | } |
| 642 | |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 643 | // Handle @ImplementedBy |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 644 | if (implementedBy != null) { |
limpbizkit | 8ba9788 | 2008-11-04 02:52:54 +0000 | [diff] [blame] | 645 | Annotations.checkForMisplacedScopeAnnotations(rawType, source, errors); |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 646 | return createImplementedByBinding(key, scoping, implementedBy, errors); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | // Handle @ProvidedBy. |
limpbizkit | 8ba9788 | 2008-11-04 02:52:54 +0000 | [diff] [blame] | 650 | ProvidedBy providedBy = rawType.getAnnotation(ProvidedBy.class); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 651 | if (providedBy != null) { |
limpbizkit | 8ba9788 | 2008-11-04 02:52:54 +0000 | [diff] [blame] | 652 | Annotations.checkForMisplacedScopeAnnotations(rawType, source, errors); |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 653 | return createProvidedByBinding(key, scoping, providedBy, errors); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 656 | |
Sam Berlin | c756777 | 2012-05-31 19:54:04 -0400 | [diff] [blame] | 657 | return ConstructorBindingImpl.create(this, |
| 658 | key, |
| 659 | null, /* use default constructor */ |
| 660 | source, |
| 661 | scoping, |
| 662 | errors, |
| 663 | jitBinding && options.jitDisabled, |
| 664 | options.atInjectRequired); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 665 | } |
| 666 | |
limpbizkit | b0f3c9b | 2008-11-15 09:19:28 +0000 | [diff] [blame] | 667 | /** |
| 668 | * Converts a binding for a {@code Key<TypeLiteral<T>>} to the value {@code TypeLiteral<T>}. It's |
| 669 | * a bit awkward because we have to pull out the inner type in the type literal. |
| 670 | */ |
| 671 | private <T> BindingImpl<TypeLiteral<T>> createTypeLiteralBinding( |
| 672 | Key<TypeLiteral<T>> key, Errors errors) throws ErrorsException { |
| 673 | Type typeLiteralType = key.getTypeLiteral().getType(); |
| 674 | if (!(typeLiteralType instanceof ParameterizedType)) { |
| 675 | throw errors.cannotInjectRawTypeLiteral().toException(); |
| 676 | } |
| 677 | |
| 678 | ParameterizedType parameterizedType = (ParameterizedType) typeLiteralType; |
| 679 | Type innerType = parameterizedType.getActualTypeArguments()[0]; |
| 680 | |
| 681 | // this is unforunate. We don't support building TypeLiterals for type variable like 'T'. If |
| 682 | // this proves problematic, we can probably fix TypeLiteral to support type variables |
| 683 | if (!(innerType instanceof Class) |
| 684 | && !(innerType instanceof GenericArrayType) |
| 685 | && !(innerType instanceof ParameterizedType)) { |
| 686 | throw errors.cannotInjectTypeLiteralOf(innerType).toException(); |
| 687 | } |
| 688 | |
| 689 | @SuppressWarnings("unchecked") // by definition, innerType == T, so this is safe |
| 690 | TypeLiteral<T> value = (TypeLiteral<T>) TypeLiteral.get(innerType); |
| 691 | InternalFactory<TypeLiteral<T>> factory = new ConstantFactory<TypeLiteral<T>>( |
| 692 | Initializables.of(value)); |
| 693 | return new InstanceBindingImpl<TypeLiteral<T>>(this, key, SourceProvider.UNKNOWN_SOURCE, |
| 694 | factory, ImmutableSet.<InjectionPoint>of(), value); |
| 695 | } |
| 696 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 697 | /** Creates a binding for a type annotated with @ProvidedBy. */ |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 698 | <T> BindingImpl<T> createProvidedByBinding(Key<T> key, Scoping scoping, |
| 699 | ProvidedBy providedBy, Errors errors) throws ErrorsException { |
sberlin | d5737a6 | 2011-05-02 18:03:35 +0000 | [diff] [blame] | 700 | Class<?> rawType = key.getTypeLiteral().getRawType(); |
| 701 | Class<? extends Provider<?>> providerType = providedBy.value(); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 702 | |
| 703 | // Make sure it's not the same type. TODO: Can we check for deeper loops? |
limpbizkit | 8ba9788 | 2008-11-04 02:52:54 +0000 | [diff] [blame] | 704 | if (providerType == rawType) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 705 | throw errors.recursiveProviderType().toException(); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 706 | } |
| 707 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 708 | // Assume the provider provides an appropriate type. We double check at runtime. |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 709 | @SuppressWarnings("unchecked") |
sberlin | d5737a6 | 2011-05-02 18:03:35 +0000 | [diff] [blame] | 710 | Key<? extends Provider<T>> providerKey = (Key<? extends Provider<T>>) Key.get(providerType); |
| 711 | ProvidedByInternalFactory<T> internalFactory = |
| 712 | new ProvidedByInternalFactory<T>(rawType, providerType, |
Sam Berlin | d51292d | 2012-02-26 21:23:19 -0500 | [diff] [blame] | 713 | providerKey, !options.disableCircularProxies); |
limpbizkit | 0cb8b86 | 2009-08-10 23:57:56 +0000 | [diff] [blame] | 714 | Object source = rawType; |
Sam Berlin | d51292d | 2012-02-26 21:23:19 -0500 | [diff] [blame] | 715 | BindingImpl<T> binding = LinkedProviderBindingImpl.createWithInitializer( |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 716 | this, |
limpbizkit | 27fc50d | 2008-08-08 07:43:39 +0000 | [diff] [blame] | 717 | key, |
limpbizkit | 0cb8b86 | 2009-08-10 23:57:56 +0000 | [diff] [blame] | 718 | source, |
| 719 | Scoping.<T>scope(key, this, internalFactory, source, scoping), |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 720 | scoping, |
sberlin | d5737a6 | 2011-05-02 18:03:35 +0000 | [diff] [blame] | 721 | providerKey, |
| 722 | internalFactory); |
Sam Berlin | d51292d | 2012-02-26 21:23:19 -0500 | [diff] [blame] | 723 | internalFactory.setProvisionListenerCallback(provisionListenerStore.get(binding)); |
| 724 | return binding; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 725 | } |
| 726 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 727 | /** Creates a binding for a type annotated with @ImplementedBy. */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame] | 728 | private <T> BindingImpl<T> createImplementedByBinding(Key<T> key, Scoping scoping, |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 729 | ImplementedBy implementedBy, Errors errors) |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 730 | throws ErrorsException { |
limpbizkit | 4272aef | 2008-11-23 08:10:09 +0000 | [diff] [blame] | 731 | Class<?> rawType = key.getTypeLiteral().getRawType(); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 732 | Class<?> implementationType = implementedBy.value(); |
| 733 | |
| 734 | // Make sure it's not the same type. TODO: Can we check for deeper cycles? |
limpbizkit | 8ba9788 | 2008-11-04 02:52:54 +0000 | [diff] [blame] | 735 | if (implementationType == rawType) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 736 | throw errors.recursiveImplementationType().toException(); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | // Make sure implementationType extends type. |
limpbizkit | 8ba9788 | 2008-11-04 02:52:54 +0000 | [diff] [blame] | 740 | if (!rawType.isAssignableFrom(implementationType)) { |
| 741 | throw errors.notASubtype(implementationType, rawType).toException(); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 742 | } |
| 743 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 744 | @SuppressWarnings("unchecked") // After the preceding check, this cast is safe. |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 745 | Class<? extends T> subclass = (Class<? extends T>) implementationType; |
| 746 | |
| 747 | // Look up the target binding. |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 748 | final Key<? extends T> targetKey = Key.get(subclass); |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 749 | final BindingImpl<? extends T> targetBinding = getBindingOrThrow(targetKey, errors, JitLimitation.NEW_OR_EXISTING_JIT); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 750 | |
| 751 | InternalFactory<T> internalFactory = new InternalFactory<T>() { |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 752 | public T get(Errors errors, InternalContext context, Dependency<?> dependency, boolean linked) |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 753 | throws ErrorsException { |
sberlin | ba75f35 | 2011-06-12 21:54:43 +0000 | [diff] [blame] | 754 | context.pushState(targetKey, targetBinding.getSource()); |
| 755 | try { |
| 756 | return targetBinding.getInternalFactory().get( |
| 757 | errors.withSource(targetKey), context, dependency, true); |
| 758 | } finally { |
| 759 | context.popState(); |
| 760 | } |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 761 | } |
| 762 | }; |
| 763 | |
limpbizkit | 0cb8b86 | 2009-08-10 23:57:56 +0000 | [diff] [blame] | 764 | Object source = rawType; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 765 | return new LinkedBindingImpl<T>( |
| 766 | this, |
limpbizkit | 27fc50d | 2008-08-08 07:43:39 +0000 | [diff] [blame] | 767 | key, |
limpbizkit | 0cb8b86 | 2009-08-10 23:57:56 +0000 | [diff] [blame] | 768 | source, |
| 769 | Scoping.<T>scope(key, this, internalFactory, source, scoping), |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 770 | scoping, |
| 771 | targetKey); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 772 | } |
| 773 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 774 | /** |
limpbizkit | 79dc99b | 2008-12-09 19:02:12 +0000 | [diff] [blame] | 775 | * Attempts to create a just-in-time binding for {@code key} in the root injector, falling back to |
| 776 | * other ancestor injectors until this injector is tried. |
| 777 | */ |
sberlin | 9cdfe3a | 2011-03-02 13:53:17 +0000 | [diff] [blame] | 778 | private <T> BindingImpl<T> createJustInTimeBindingRecursive(Key<T> key, Errors errors, |
| 779 | boolean jitDisabled, JitLimitation jitType) throws ErrorsException { |
limpbizkit | 79dc99b | 2008-12-09 19:02:12 +0000 | [diff] [blame] | 780 | // ask the parent to create the JIT binding |
sberlin | 9cdfe3a | 2011-03-02 13:53:17 +0000 | [diff] [blame] | 781 | if (parent != null) { |
Tavian Barnes | 1dd0a91 | 2014-12-19 22:38:50 -0500 | [diff] [blame] | 782 | if (jitDisabled && jitType == JitLimitation.NEW_OR_EXISTING_JIT) { |
| 783 | throw errors.jitDisabledInParent(key).toException(); |
| 784 | } else { |
| 785 | try { |
| 786 | return parent.createJustInTimeBindingRecursive(key, new Errors(), jitDisabled, |
| 787 | parent.options.jitDisabled ? JitLimitation.NO_JIT : jitType); |
| 788 | } catch (ErrorsException ignored) { |
| 789 | } |
limpbizkit | 79dc99b | 2008-12-09 19:02:12 +0000 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 793 | // Retrieve the sources before checking for blacklisting to guard against sources becoming null |
| 794 | // due to a full GC happening after calling state.isBlacklisted and |
| 795 | // state.getSourcesForBlacklistedKey. |
| 796 | // TODO(user): Consolidate these two APIs. |
| 797 | Set<Object> sources = state.getSourcesForBlacklistedKey(key); |
limpbizkit | 79dc99b | 2008-12-09 19:02:12 +0000 | [diff] [blame] | 798 | if (state.isBlacklisted(key)) { |
sberlin | cc17f14 | 2011-02-27 00:02:03 +0000 | [diff] [blame] | 799 | throw errors.childBindingAlreadySet(key, sources).toException(); |
limpbizkit | 79dc99b | 2008-12-09 19:02:12 +0000 | [diff] [blame] | 800 | } |
| 801 | |
sberlin | 9cdfe3a | 2011-03-02 13:53:17 +0000 | [diff] [blame] | 802 | BindingImpl<T> binding = createJustInTimeBinding(key, errors, jitDisabled, jitType); |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 803 | state.parent().blacklist(key, state, binding.getSource()); |
limpbizkit | 79dc99b | 2008-12-09 19:02:12 +0000 | [diff] [blame] | 804 | jitBindings.put(key, binding); |
| 805 | return binding; |
| 806 | } |
| 807 | |
| 808 | /** |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 809 | * Returns a new just-in-time binding created by resolving {@code key}. The strategies used to |
| 810 | * create just-in-time bindings are: |
| 811 | * <ol> |
| 812 | * <li>Internalizing Providers. If the requested binding is for {@code Provider<T>}, we delegate |
| 813 | * to the binding for {@code T}. |
| 814 | * <li>Converting constants. |
| 815 | * <li>ImplementedBy and ProvidedBy annotations. Only for unannotated keys. |
| 816 | * <li>The constructor of the raw type. Only for unannotated keys. |
| 817 | * </ol> |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 818 | * |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 819 | * @throws com.google.inject.internal.ErrorsException if the binding cannot be created. |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 820 | */ |
sberlin | 9cdfe3a | 2011-03-02 13:53:17 +0000 | [diff] [blame] | 821 | private <T> BindingImpl<T> createJustInTimeBinding(Key<T> key, Errors errors, |
| 822 | boolean jitDisabled, JitLimitation jitType) throws ErrorsException { |
limpbizkit | b92f540 | 2009-06-03 16:50:27 +0000 | [diff] [blame] | 823 | int numErrorsBefore = errors.size(); |
| 824 | |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 825 | // Retrieve the sources before checking for blacklisting to guard against sources becoming null |
| 826 | // due to a full GC happening after calling state.isBlacklisted and |
| 827 | // state.getSourcesForBlacklistedKey. |
| 828 | // TODO(user): Consolidate these two APIs. |
| 829 | Set<Object> sources = state.getSourcesForBlacklistedKey(key); |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 830 | if (state.isBlacklisted(key)) { |
sberlin | cc17f14 | 2011-02-27 00:02:03 +0000 | [diff] [blame] | 831 | throw errors.childBindingAlreadySet(key, sources).toException(); |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 832 | } |
| 833 | |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 834 | // Handle cases where T is a Provider<?>. |
| 835 | if (isProvider(key)) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 836 | // These casts are safe. We know T extends Provider<X> and that given Key<Provider<X>>, |
| 837 | // createProviderBinding() will return BindingImpl<Provider<X>>. |
Sam Berlin | c756777 | 2012-05-31 19:54:04 -0400 | [diff] [blame] | 838 | @SuppressWarnings({"unchecked", "cast"}) |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame] | 839 | BindingImpl<T> binding = (BindingImpl<T>) createProviderBinding((Key) key, errors); |
crazyboblee | 5d57569 | 2007-09-07 17:42:28 +0000 | [diff] [blame] | 840 | return binding; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 841 | } |
| 842 | |
limpbizkit | 0ca7f91 | 2009-03-28 19:11:40 +0000 | [diff] [blame] | 843 | // Handle cases where T is a MembersInjector<?> |
| 844 | if (isMembersInjector(key)) { |
| 845 | // These casts are safe. T extends MembersInjector<X> and that given Key<MembersInjector<X>>, |
| 846 | // createMembersInjectorBinding() will return BindingImpl<MembersInjector<X>>. |
Sam Berlin | c756777 | 2012-05-31 19:54:04 -0400 | [diff] [blame] | 847 | @SuppressWarnings({"unchecked", "cast"}) |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame] | 848 | BindingImpl<T> binding = (BindingImpl<T>) createMembersInjectorBinding((Key) key, errors); |
limpbizkit | 0ca7f91 | 2009-03-28 19:11:40 +0000 | [diff] [blame] | 849 | return binding; |
| 850 | } |
| 851 | |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 852 | // Try to convert a constant string binding to the requested type. |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 853 | BindingImpl<T> convertedBinding = convertConstantStringBinding(key, errors); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 854 | if (convertedBinding != null) { |
| 855 | return convertedBinding; |
| 856 | } |
Christian Edward Gruber | 9e2d95b | 2013-06-26 17:43:11 -0700 | [diff] [blame] | 857 | |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 858 | if (!isTypeLiteral(key) |
sberlin | 9cdfe3a | 2011-03-02 13:53:17 +0000 | [diff] [blame] | 859 | && jitDisabled |
| 860 | && jitType != JitLimitation.NEW_OR_EXISTING_JIT) { |
| 861 | throw errors.jitDisabled(key).toException(); |
| 862 | } |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 863 | |
| 864 | // If the key has an annotation... |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame] | 865 | if (key.getAnnotationType() != null) { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 866 | // Look for a binding without annotation attributes or return null. |
Christian Edward Gruber | e391585 | 2013-05-16 11:00:54 -0700 | [diff] [blame] | 867 | if (key.hasAttributes() && !options.exactBindingAnnotationsRequired) { |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 868 | try { |
limpbizkit | 2c2c610 | 2008-06-20 13:34:36 +0000 | [diff] [blame] | 869 | Errors ignored = new Errors(); |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 870 | return getBindingOrThrow(key.withoutAttributes(), ignored, JitLimitation.NO_JIT); |
limpbizkit | 2c2c610 | 2008-06-20 13:34:36 +0000 | [diff] [blame] | 871 | } catch (ErrorsException ignored) { |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 872 | // throw with a more appropriate message below |
| 873 | } |
| 874 | } |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 875 | throw errors.missingImplementation(key).toException(); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 876 | } |
| 877 | |
limpbizkit | 4272aef | 2008-11-23 08:10:09 +0000 | [diff] [blame] | 878 | Object source = key.getTypeLiteral().getRawType(); |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 879 | BindingImpl<T> binding = createUninitializedBinding(key, Scoping.UNSCOPED, source, errors, true); |
limpbizkit | b92f540 | 2009-06-03 16:50:27 +0000 | [diff] [blame] | 880 | errors.throwIfNewErrors(numErrorsBefore); |
limpbizkit | 91425c3 | 2009-07-17 03:10:13 +0000 | [diff] [blame] | 881 | initializeJitBinding(binding, errors); |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 882 | return binding; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 883 | } |
| 884 | |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 885 | <T> InternalFactory<? extends T> getInternalFactory(Key<T> key, Errors errors, JitLimitation jitType) |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 886 | throws ErrorsException { |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 887 | return getBindingOrThrow(key, errors, jitType).getInternalFactory(); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 888 | } |
| 889 | |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 890 | public Map<Key<?>, Binding<?>> getBindings() { |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 891 | return state.getExplicitBindingsThisLevel(); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 892 | } |
| 893 | |
limpbizkit | 37387e2 | 2009-07-04 16:20:55 +0000 | [diff] [blame] | 894 | public Map<Key<?>, Binding<?>> getAllBindings() { |
| 895 | synchronized (state.lock()) { |
| 896 | return new ImmutableMap.Builder<Key<?>, Binding<?>>() |
| 897 | .putAll(state.getExplicitBindingsThisLevel()) |
| 898 | .putAll(jitBindings) |
| 899 | .build(); |
| 900 | } |
| 901 | } |
| 902 | |
limpbizkit | 1920322 | 2009-07-24 00:48:12 +0000 | [diff] [blame] | 903 | public Map<Class<? extends Annotation>, Scope> getScopeBindings() { |
sberlin | b2f1760 | 2010-11-19 02:33:32 +0000 | [diff] [blame] | 904 | return ImmutableMap.copyOf(state.getScopes()); |
| 905 | } |
| 906 | |
| 907 | public Set<TypeConverterBinding> getTypeConverterBindings() { |
| 908 | return ImmutableSet.copyOf(state.getConvertersThisLevel()); |
limpbizkit | 1920322 | 2009-07-24 00:48:12 +0000 | [diff] [blame] | 909 | } |
| 910 | |
kevinb9n | 48d1307 | 2007-02-12 18:21:26 +0000 | [diff] [blame] | 911 | private static class BindingsMultimap { |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 912 | final Map<TypeLiteral<?>, List<Binding<?>>> multimap = Maps.newHashMap(); |
kevinb9n | 48d1307 | 2007-02-12 18:21:26 +0000 | [diff] [blame] | 913 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 914 | <T> void put(TypeLiteral<T> type, Binding<T> binding) { |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 915 | List<Binding<?>> bindingsForType = multimap.get(type); |
| 916 | if (bindingsForType == null) { |
| 917 | bindingsForType = Lists.newArrayList(); |
| 918 | multimap.put(type, bindingsForType); |
| 919 | } |
| 920 | bindingsForType.add(binding); |
kevinb9n | 48d1307 | 2007-02-12 18:21:26 +0000 | [diff] [blame] | 921 | } |
| 922 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 923 | |
| 924 | @SuppressWarnings("unchecked") // safe because we only put matching entries into the map |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 925 | <T> List<Binding<T>> getAll(TypeLiteral<T> type) { |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 926 | List<Binding<?>> bindings = multimap.get(type); |
| 927 | return bindings != null |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 928 | ? Collections.<Binding<T>>unmodifiableList((List) multimap.get(type)) |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 929 | : ImmutableList.<Binding<T>>of(); |
kevinb9n | 48d1307 | 2007-02-12 18:21:26 +0000 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 933 | /** |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 934 | * Returns parameter injectors, or {@code null} if there are no parameters. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 935 | */ |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 936 | SingleParameterInjector<?>[] getParametersInjectors( |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 937 | List<Dependency<?>> parameters, Errors errors) throws ErrorsException { |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 938 | if (parameters.isEmpty()) { |
| 939 | return null; |
limpbizkit | 185d2a2 | 2008-06-16 07:22:47 +0000 | [diff] [blame] | 940 | } |
| 941 | |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 942 | int numErrorsBefore = errors.size(); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 943 | SingleParameterInjector<?>[] result = new SingleParameterInjector<?>[parameters.size()]; |
| 944 | int i = 0; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 945 | for (Dependency<?> parameter : parameters) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 946 | try { |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 947 | result[i++] = createParameterInjector(parameter, errors.withSource(parameter)); |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 948 | } catch (ErrorsException rethrownBelow) { |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 949 | // rethrown below |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 950 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 951 | } |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 952 | |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 953 | errors.throwIfNewErrors(numErrorsBefore); |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 954 | return result; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 955 | } |
| 956 | |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 957 | <T> SingleParameterInjector<T> createParameterInjector(final Dependency<T> dependency, |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 958 | final Errors errors) throws ErrorsException { |
sberlin | ba75f35 | 2011-06-12 21:54:43 +0000 | [diff] [blame] | 959 | BindingImpl<? extends T> binding = getBindingOrThrow(dependency.getKey(), errors, JitLimitation.NO_JIT); |
| 960 | return new SingleParameterInjector<T>(dependency, binding); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 961 | } |
| 962 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 963 | /** Invokes a method. */ |
crazyboblee | 0b3189c | 2007-02-24 00:14:51 +0000 | [diff] [blame] | 964 | interface MethodInvoker { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 965 | Object invoke(Object target, Object... parameters) |
| 966 | throws IllegalAccessException, InvocationTargetException; |
crazyboblee | 0b3189c | 2007-02-24 00:14:51 +0000 | [diff] [blame] | 967 | } |
| 968 | |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 969 | /** Cached constructor injectors for each type */ |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 970 | final ConstructorInjectorStore constructors = new ConstructorInjectorStore(this); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 971 | |
| 972 | /** Cached field and method injectors for each type. */ |
| 973 | MembersInjectorStore membersInjectorStore; |
Sam Berlin | d57f8ec | 2014-04-08 12:25:33 -0400 | [diff] [blame] | 974 | |
sberlin | 132a5db | 2011-06-05 18:32:05 +0000 | [diff] [blame] | 975 | /** Cached provision listener callbacks for each key. */ |
| 976 | ProvisionListenerCallbackStore provisionListenerStore; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 977 | |
limpbizkit | 97eac0f | 2009-03-28 18:25:35 +0000 | [diff] [blame] | 978 | @SuppressWarnings("unchecked") // the members injector type is consistent with instance's type |
| 979 | public void injectMembers(Object instance) { |
| 980 | MembersInjector membersInjector = getMembersInjector(instance.getClass()); |
| 981 | membersInjector.injectMembers(instance); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 982 | } |
| 983 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 984 | public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> typeLiteral) { |
limpbizkit | 97eac0f | 2009-03-28 18:25:35 +0000 | [diff] [blame] | 985 | Errors errors = new Errors(typeLiteral); |
| 986 | try { |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 987 | return membersInjectorStore.get(typeLiteral, errors); |
limpbizkit | 97eac0f | 2009-03-28 18:25:35 +0000 | [diff] [blame] | 988 | } catch (ErrorsException e) { |
| 989 | throw new ConfigurationException(errors.merge(e.getErrors()).getMessages()); |
| 990 | } |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | public <T> MembersInjector<T> getMembersInjector(Class<T> type) { |
limpbizkit | 97eac0f | 2009-03-28 18:25:35 +0000 | [diff] [blame] | 994 | return getMembersInjector(TypeLiteral.get(type)); |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 995 | } |
| 996 | |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 997 | public <T> Provider<T> getProvider(Class<T> type) { |
| 998 | return getProvider(Key.get(type)); |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 1001 | <T> Provider<T> getProviderOrThrow(final Key<T> key, Errors errors) throws ErrorsException { |
sberlin | ba75f35 | 2011-06-12 21:54:43 +0000 | [diff] [blame] | 1002 | final BindingImpl<? extends T> binding = getBindingOrThrow(key, errors, JitLimitation.NO_JIT); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 1003 | final Dependency<T> dependency = Dependency.get(key); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1004 | |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 1005 | return new Provider<T>() { |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 1006 | public T get() { |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 1007 | final Errors errors = new Errors(dependency); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 1008 | try { |
| 1009 | T t = callInContext(new ContextualCallable<T>() { |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 1010 | public T call(InternalContext context) throws ErrorsException { |
sberlin | ba75f35 | 2011-06-12 21:54:43 +0000 | [diff] [blame] | 1011 | Dependency previous = context.pushDependency(dependency, binding.getSource()); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 1012 | try { |
sberlin | ba75f35 | 2011-06-12 21:54:43 +0000 | [diff] [blame] | 1013 | return binding.getInternalFactory().get(errors, context, dependency, false); |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 1014 | } finally { |
sberlin | ba75f35 | 2011-06-12 21:54:43 +0000 | [diff] [blame] | 1015 | context.popStateAndSetDependency(previous); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 1016 | } |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 1017 | } |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 1018 | }); |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 1019 | errors.throwIfNewErrors(0); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 1020 | return t; |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 1021 | } catch (ErrorsException e) { |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 1022 | throw new ProvisionException(errors.merge(e.getErrors()).getMessages()); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 1023 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1024 | } |
crazyboblee | d7908e8 | 2007-02-16 01:04:53 +0000 | [diff] [blame] | 1025 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 1026 | @Override public String toString() { |
sberlin | ba75f35 | 2011-06-12 21:54:43 +0000 | [diff] [blame] | 1027 | return binding.getInternalFactory().toString(); |
crazyboblee | d7908e8 | 2007-02-16 01:04:53 +0000 | [diff] [blame] | 1028 | } |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 1029 | }; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 1032 | public <T> Provider<T> getProvider(final Key<T> key) { |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 1033 | Errors errors = new Errors(key); |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 1034 | try { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 1035 | Provider<T> result = getProviderOrThrow(key, errors); |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 1036 | errors.throwIfNewErrors(0); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 1037 | return result; |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 1038 | } catch (ErrorsException e) { |
| 1039 | throw new ConfigurationException(errors.merge(e.getErrors()).getMessages()); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 1040 | } |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
kevinb9n | 27f8a58 | 2007-02-28 22:54:06 +0000 | [diff] [blame] | 1043 | public <T> T getInstance(Key<T> key) { |
| 1044 | return getProvider(key).get(); |
| 1045 | } |
| 1046 | |
| 1047 | public <T> T getInstance(Class<T> type) { |
| 1048 | return getProvider(type).get(); |
| 1049 | } |
| 1050 | |
Sam Berlin | ca81e59 | 2014-07-01 16:52:59 -0400 | [diff] [blame] | 1051 | private final ThreadLocal<Object[]> localContext; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1052 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 1053 | /** Looks up thread local context. Creates (and removes) a new context if necessary. */ |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 1054 | <T> T callInContext(ContextualCallable<T> callable) throws ErrorsException { |
limpbizkit | 833265f | 2009-05-06 18:14:32 +0000 | [diff] [blame] | 1055 | Object[] reference = localContext.get(); |
Sam Berlin | ca81e59 | 2014-07-01 16:52:59 -0400 | [diff] [blame] | 1056 | if (reference == null) { |
| 1057 | reference = new Object[1]; |
| 1058 | localContext.set(reference); |
| 1059 | } |
limpbizkit | 833265f | 2009-05-06 18:14:32 +0000 | [diff] [blame] | 1060 | if (reference[0] == null) { |
| 1061 | reference[0] = new InternalContext(); |
| 1062 | try { |
| 1063 | return callable.call((InternalContext)reference[0]); |
| 1064 | } finally { |
| 1065 | // Only clear the context if this call created it. |
| 1066 | reference[0] = null; |
| 1067 | } |
| 1068 | } else { |
| 1069 | // Someone else will clean up this context. |
| 1070 | return callable.call((InternalContext)reference[0]); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1071 | } |
| 1072 | } |
| 1073 | |
guice.mirrorbot@gmail.com | efa4e9f | 2011-09-13 17:41:32 +0000 | [diff] [blame] | 1074 | @Override |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 1075 | public String toString() { |
sberlin | d9c913a | 2011-06-26 21:02:54 +0000 | [diff] [blame] | 1076 | return Objects.toStringHelper(Injector.class) |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 1077 | .add("bindings", state.getExplicitBindingsThisLevel().values()) |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 1078 | .toString(); |
| 1079 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1080 | } |