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 | |
| 17 | package com.google.inject; |
| 18 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkState; |
| 20 | import com.google.common.base.ReferenceType; |
| 21 | import com.google.common.collect.Lists; |
| 22 | import com.google.common.collect.Maps; |
| 23 | import com.google.common.collect.Multimap; |
| 24 | import com.google.common.collect.Multimaps; |
| 25 | import com.google.common.collect.Sets; |
| 26 | import com.google.inject.internal.Classes; |
| 27 | import com.google.inject.internal.ErrorHandler; |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 28 | import com.google.inject.internal.ErrorMessage; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 29 | import com.google.inject.internal.GuiceFastClass; |
| 30 | import com.google.inject.internal.Keys; |
| 31 | import com.google.inject.internal.MatcherAndConverter; |
| 32 | import com.google.inject.internal.ReferenceCache; |
| 33 | import com.google.inject.internal.ResolveFailedException; |
| 34 | import com.google.inject.internal.ResolvingCallable; |
| 35 | import com.google.inject.internal.StackTraceElements; |
| 36 | import com.google.inject.internal.ToStringBuilder; |
| 37 | import com.google.inject.spi.BindingVisitor; |
| 38 | import com.google.inject.spi.ConvertedConstantBinding; |
| 39 | import com.google.inject.spi.Dependency; |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 40 | import com.google.inject.spi.Message; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 41 | import com.google.inject.spi.ProviderBinding; |
| 42 | import com.google.inject.spi.SourceProviders; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 43 | import com.google.inject.util.Providers; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 44 | import java.lang.annotation.Annotation; |
| 45 | import java.lang.reflect.AnnotatedElement; |
| 46 | import java.lang.reflect.Field; |
| 47 | import java.lang.reflect.InvocationTargetException; |
| 48 | import java.lang.reflect.Member; |
| 49 | import java.lang.reflect.Method; |
| 50 | import java.lang.reflect.Modifier; |
| 51 | import java.lang.reflect.ParameterizedType; |
| 52 | import java.lang.reflect.Type; |
| 53 | import java.util.ArrayList; |
| 54 | import java.util.Arrays; |
| 55 | import java.util.Collection; |
| 56 | import java.util.Collections; |
| 57 | import java.util.List; |
| 58 | import java.util.Map; |
| 59 | import java.util.Set; |
kevinb9n | 225310e | 2007-02-20 04:12:01 +0000 | [diff] [blame] | 60 | import net.sf.cglib.reflect.FastClass; |
| 61 | import net.sf.cglib.reflect.FastMethod; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 62 | |
| 63 | /** |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 64 | * Default {@link Injector} implementation. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 65 | * |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 66 | * @author crazybob@google.com (Bob Lee) |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 67 | * @see InjectorBuilder |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 68 | */ |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 69 | class InjectorImpl implements Injector { |
dan.halem | 5d18743 | 2008-02-22 22:52:28 +0000 | [diff] [blame] | 70 | final Injector parentInjector; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 71 | final Map<Key<?>, BindingImpl<?>> explicitBindings = Maps.newHashMap(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 72 | final BindingsMultimap bindingsMultimap = new BindingsMultimap(); |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 73 | final Map<Class<? extends Annotation>, Scope> scopes = Maps.newHashMap(); |
| 74 | final List<MatcherAndConverter<?>> converters = Lists.newArrayList(); |
| 75 | final Map<Key<?>, BindingImpl<?>> parentBindings = Maps.newHashMap(); |
| 76 | final Set<Object> outstandingInjections = Sets.newIdentityHashSet(ReferenceType.STRONG); |
limpbizkit | b1d8ab4 | 2008-04-27 08:11:37 +0000 | [diff] [blame] | 77 | final ErrorHandler errorHandler; |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 78 | Reflection reflection; |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 79 | |
limpbizkit | b1d8ab4 | 2008-04-27 08:11:37 +0000 | [diff] [blame] | 80 | InjectorImpl(Injector parentInjector, ErrorHandler errorHandler) { |
dan.halem | 5d18743 | 2008-02-22 22:52:28 +0000 | [diff] [blame] | 81 | this.parentInjector = parentInjector; |
limpbizkit | b1d8ab4 | 2008-04-27 08:11:37 +0000 | [diff] [blame] | 82 | this.errorHandler = errorHandler; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 83 | } |
| 84 | |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 85 | void validateOustandingInjections() { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 86 | for (Object toInject : outstandingInjections) { |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 87 | injectors.get(toInject.getClass()); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 92 | * Performs creation-time injections on all objects that require it. Whenever fulfilling an |
| 93 | * injection depends on another object that requires injection, we use {@link |
| 94 | * InternalContext#ensureMemberInjected} to inject that member first. |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 95 | * |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 96 | * <p>If the two objects are codependent (directly or transitively), ordering of injection is |
| 97 | * arbitrary. |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 98 | */ |
| 99 | void fulfillOutstandingInjections() { |
| 100 | callInContext(new ContextualCallable<Void>() { |
| 101 | public Void call(InternalContext context) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 102 | // loop over a defensive copy, since ensureMemberInjected() mutates the set |
| 103 | for (Object toInject : Lists.newArrayList(outstandingInjections)) { |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 104 | context.ensureMemberInjected(toInject); |
| 105 | } |
| 106 | return null; |
| 107 | } |
| 108 | }); |
| 109 | |
| 110 | if (!outstandingInjections.isEmpty()) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 111 | throw new AssertionError("failed to satisfy " + outstandingInjections); |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 115 | /** Indexes bindings by type. */ |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 116 | void index() { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 117 | for (BindingImpl<?> binding : explicitBindings.values()) { |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 118 | index(binding); |
| 119 | } |
| 120 | } |
| 121 | |
crazyboblee | 1fc4978 | 2007-02-25 21:02:47 +0000 | [diff] [blame] | 122 | <T> void index(BindingImpl<T> binding) { |
crazyboblee | c3e8849 | 2007-02-25 22:36:58 +0000 | [diff] [blame] | 123 | bindingsMultimap.put(binding.getKey().getTypeLiteral(), binding); |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 124 | } |
| 125 | |
kevinb9n | 225310e | 2007-02-20 04:12:01 +0000 | [diff] [blame] | 126 | // not test-covered |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 127 | public <T> List<Binding<T>> findBindingsByType(TypeLiteral<T> type) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 128 | return Collections.<Binding<T>>unmodifiableList(bindingsMultimap.getAll(type)); |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 129 | } |
| 130 | |
kevinb9n | 225310e | 2007-02-20 04:12:01 +0000 | [diff] [blame] | 131 | // not test-covered |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 132 | <T> List<String> getNamesOfBindingAnnotations(TypeLiteral<T> type) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 133 | List<String> names = Lists.newArrayList(); |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 134 | for (Binding<T> binding : findBindingsByType(type)) { |
crazyboblee | b1f2e68 | 2007-02-15 05:14:32 +0000 | [diff] [blame] | 135 | Key<T> key = binding.getKey(); |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 136 | names.add(key.hasAnnotationType() ? key.getAnnotationName() : "[no annotation]"); |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 137 | } |
| 138 | return names; |
| 139 | } |
| 140 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 141 | /** This is only used during Injector building. */ |
crazyboblee | 9a3861b | 2007-02-20 03:37:31 +0000 | [diff] [blame] | 142 | void withDefaultSource(Object defaultSource, Runnable runnable) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 143 | // TODO: inline/get rid of? |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 144 | SourceProviders.withDefault(defaultSource, runnable); |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 145 | } |
| 146 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 147 | /** Returns the binding for {@code key}, or {@code null} if that binding cannot be resolved. */ |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 148 | public <T> BindingImpl<T> getBinding(Key<T> key) { |
| 149 | try { |
| 150 | return getBindingOrThrow(key); |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 151 | } |
| 152 | catch (ResolveFailedException e) { |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 153 | return null; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 158 | * Gets a binding implementation. First, it check to see if the parent has a binding. If the |
| 159 | * parent has a binding and the binding is scoped, it will use that binding. Otherwise, this |
| 160 | * checks for an explicit binding. If no explicit binding is found, it looks for a just-in-time |
| 161 | * binding. |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 162 | */ |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 163 | public <T> BindingImpl<T> getBindingOrThrow(Key<T> key) throws ResolveFailedException { |
dan.halem | 5d18743 | 2008-02-22 22:52:28 +0000 | [diff] [blame] | 164 | if (parentInjector != null) { |
| 165 | BindingImpl<T> bindingImpl = getParentBinding(key); |
| 166 | if (bindingImpl != null) { |
| 167 | return bindingImpl; |
| 168 | } |
| 169 | } |
| 170 | |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 171 | // Check explicit bindings, i.e. bindings created by modules. |
| 172 | BindingImpl<T> binding = getExplicitBindingImpl(key); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 173 | if (binding != null) { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 174 | return binding; |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 175 | } |
| 176 | |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 177 | // Look for an on-demand binding. |
| 178 | return getJitBindingImpl(key); |
| 179 | } |
crazyboblee | f33d23e | 2007-02-12 04:17:48 +0000 | [diff] [blame] | 180 | |
dan.halem | 5d18743 | 2008-02-22 22:52:28 +0000 | [diff] [blame] | 181 | /** |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 182 | * Checks the parent injector for a scoped binding, and if available, creates an appropriate |
| 183 | * binding local to this injector and remembers it. |
| 184 | * |
| 185 | * TODO: think about this wrt parent jit bindings |
dan.halem | 5d18743 | 2008-02-22 22:52:28 +0000 | [diff] [blame] | 186 | */ |
| 187 | @SuppressWarnings("unchecked") |
| 188 | private <T> BindingImpl<T> getParentBinding(Key<T> key) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 189 | synchronized (parentBindings) { |
dan.halem | 5d18743 | 2008-02-22 22:52:28 +0000 | [diff] [blame] | 190 | // null values will mean that the parent doesn't have this binding |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 191 | Binding<T> binding = (Binding<T>) parentBindings.get(key); |
| 192 | if (binding != null) { |
| 193 | return (BindingImpl<T>) binding; |
dan.halem | 5d18743 | 2008-02-22 22:52:28 +0000 | [diff] [blame] | 194 | } |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 195 | try { |
| 196 | binding = parentInjector.getBinding(key); |
| 197 | } |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 198 | catch (CreationException e) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 199 | // if this happens, the parent can't create this key, and we ignore it |
| 200 | } |
| 201 | |
| 202 | BindingImpl<T> bindingImpl = null; |
| 203 | if (binding != null |
| 204 | && binding.getScope() != null |
| 205 | && !binding.getScope().equals(Scopes.NO_SCOPE)) { |
| 206 | bindingImpl = new ProviderInstanceBindingImpl( |
| 207 | this, |
| 208 | key, |
| 209 | binding.getSource(), |
| 210 | new InternalFactoryToProviderAdapter(binding.getProvider(), binding.getSource()), |
| 211 | Scopes.NO_SCOPE, |
| 212 | binding.getProvider(), |
| 213 | LoadStrategy.LAZY); |
| 214 | } |
| 215 | parentBindings.put(key, bindingImpl); // this kinda scares me |
| 216 | return bindingImpl; |
dan.halem | 5d18743 | 2008-02-22 22:52:28 +0000 | [diff] [blame] | 217 | } |
dan.halem | 5d18743 | 2008-02-22 22:52:28 +0000 | [diff] [blame] | 218 | } |
| 219 | |
crazyboblee | 552472f | 2007-09-07 16:52:39 +0000 | [diff] [blame] | 220 | public <T> Binding<T> getBinding(Class<T> type) { |
| 221 | return getBinding(Key.get(type)); |
| 222 | } |
| 223 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 224 | /** Gets a binding which was specified explicitly in a module. */ |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 225 | @SuppressWarnings("unchecked") |
| 226 | <T> BindingImpl<T> getExplicitBindingImpl(Key<T> key) { |
| 227 | return (BindingImpl<T>) explicitBindings.get(key); |
| 228 | } |
| 229 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 230 | /* |
| 231 | * Gets a just-in-time binding. This could be an injectable class (including those with |
| 232 | * @ImplementedBy), an automatically converted constant, a Provider<X> binding, etc. |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 233 | */ |
| 234 | @SuppressWarnings("unchecked") |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 235 | <T> BindingImpl<T> getJitBindingImpl(Key<T> key) throws ResolveFailedException { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 236 | synchronized (jitBindings) { |
| 237 | // Support null values. |
| 238 | if (!jitBindings.containsKey(key)) { |
| 239 | BindingImpl<T> binding = createBindingJustInTime(key); |
| 240 | jitBindings.put(key, binding); |
| 241 | return binding; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 242 | } |
| 243 | else { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 244 | return (BindingImpl<T>) jitBindings.get(key); |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 245 | } |
limpbizkit | c808df0 | 2007-08-25 03:25:13 +0000 | [diff] [blame] | 246 | } |
crazyboblee | 07e4182 | 2006-11-21 01:27:08 +0000 | [diff] [blame] | 247 | } |
| 248 | |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 249 | /** Just-in-time binding cache. */ |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 250 | final Map<Key<?>, BindingImpl<?>> jitBindings = Maps.newHashMap(); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 251 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 252 | /* Returns true if the key type is Provider<?> (but not a subclass of Provider<?>). */ |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 253 | static boolean isProvider(Key<?> key) { |
| 254 | return key.getTypeLiteral().getRawType().equals(Provider.class); |
| 255 | } |
| 256 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 257 | /** Creates a synthetic binding to Provider<T>, i.e. a binding to the provider from Binding<T>. */ |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 258 | private <T> BindingImpl<Provider<T>> createProviderBinding( |
limpbizkit | 0575714 | 2008-06-02 06:58:47 +0000 | [diff] [blame] | 259 | Key<Provider<T>> key, LoadStrategy loadStrategy) throws ResolveFailedException { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 260 | Type providerType = key.getTypeLiteral().getType(); |
| 261 | |
| 262 | // If the Provider has no type parameter (raw Provider)... |
| 263 | if (!(providerType instanceof ParameterizedType)) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 264 | throw new ResolveFailedException(ErrorMessage.cannotInjectRawProvider()); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 265 | } |
| 266 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 267 | Type entryType = ((ParameterizedType) providerType).getActualTypeArguments()[0]; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 268 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 269 | @SuppressWarnings("unchecked") // safe because T came from Key<Provider<T>> |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 270 | Key<T> providedKey = (Key<T>) key.ofType(entryType); |
| 271 | |
limpbizkit | 0575714 | 2008-06-02 06:58:47 +0000 | [diff] [blame] | 272 | return new ProviderBindingImpl<T>(this, key, getBindingOrThrow(providedKey), loadStrategy); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 273 | } |
| 274 | |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 275 | void handleMissingBinding(Object source, Key<?> key) { |
| 276 | List<String> otherNames = getNamesOfBindingAnnotations(key.getTypeLiteral()); |
| 277 | |
| 278 | if (source instanceof Member) { |
| 279 | source = StackTraceElements.forMember((Member) source); |
| 280 | } |
| 281 | |
| 282 | if (otherNames.isEmpty()) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 283 | errorHandler.handle(source, ErrorMessage.missingBinding(key)); |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 284 | } |
| 285 | else { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 286 | errorHandler.handle(source, ErrorMessage.missingBindingButOthersExist(key, otherNames)); |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 290 | static class ProviderBindingImpl<T> extends BindingImpl<Provider<T>> |
| 291 | implements ProviderBinding<T> { |
| 292 | |
| 293 | final Binding<T> providedBinding; |
| 294 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 295 | ProviderBindingImpl( |
| 296 | InjectorImpl injector, |
| 297 | Key<Provider<T>> key, |
| 298 | Binding<T> providedBinding, |
| 299 | LoadStrategy loadStrategy) { |
| 300 | super( |
| 301 | injector, |
| 302 | key, |
| 303 | SourceProviders.UNKNOWN_SOURCE, |
| 304 | createInternalFactory(providedBinding), |
| 305 | Scopes.NO_SCOPE, |
| 306 | loadStrategy); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 307 | this.providedBinding = providedBinding; |
| 308 | } |
| 309 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 310 | static <T> InternalFactory<Provider<T>> createInternalFactory(Binding<T> providedBinding) { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 311 | final Provider<T> provider = providedBinding.getProvider(); |
| 312 | return new InternalFactory<Provider<T>>() { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 313 | public Provider<T> get(InternalContext context, InjectionPoint injectionPoint) { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 314 | return provider; |
| 315 | } |
| 316 | }; |
| 317 | } |
| 318 | |
| 319 | public void accept(BindingVisitor<? super Provider<T>> bindingVisitor) { |
| 320 | bindingVisitor.visit(this); |
| 321 | } |
| 322 | |
| 323 | public Binding<T> getTarget() { |
| 324 | return providedBinding; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | <T> BindingImpl<T> invalidBinding(Class<T> clazz) { |
| 329 | return invalidBinding(Key.get(clazz)); |
| 330 | } |
| 331 | |
| 332 | <T> BindingImpl<T> invalidBinding(Key<T> key) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 333 | return new InvalidBindingImpl<T>(this, key, SourceProviders.defaultSource()); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /** |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 337 | * Converts a constant string binding to the required type. |
| 338 | * |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 339 | * <p>If the required type is eligible for conversion and a constant string binding is found but |
| 340 | * the actual conversion fails, an error is generated. |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 341 | * |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 342 | * <p>If the type is not eligible for conversion or a constant string binding is not found, this |
| 343 | * method returns null. |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 344 | */ |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 345 | private <T> BindingImpl<T> convertConstantStringBinding(Key<T> key) |
| 346 | throws ResolveFailedException { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 347 | // Find a constant string binding. |
| 348 | Key<String> stringKey = key.ofType(String.class); |
| 349 | BindingImpl<String> stringBinding = getExplicitBindingImpl(stringKey); |
| 350 | if (stringBinding == null || !stringBinding.isConstant()) { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 351 | return null; |
| 352 | } |
| 353 | |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 354 | String stringValue = stringBinding.getProvider().get(); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 355 | |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 356 | // Find a matching type converter. |
| 357 | TypeLiteral<T> type = key.getTypeLiteral(); |
| 358 | MatcherAndConverter<?> matchingConverter = null; |
| 359 | for (MatcherAndConverter<?> converter : converters) { |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 360 | if (converter.getTypeMatcher().matches(type)) { |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 361 | if (matchingConverter != null) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 362 | throw new ResolveFailedException(ErrorMessage.ambiguousTypeConversion( |
| 363 | stringValue, type, matchingConverter, converter)); |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 364 | } |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 365 | matchingConverter = converter; |
| 366 | } |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 367 | } |
| 368 | |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 369 | if (matchingConverter == null) { |
| 370 | // No converter can handle the given type. |
| 371 | return null; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 372 | } |
| 373 | |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 374 | // Try to convert the string. A failed conversion results in an error. |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 375 | try { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 376 | @SuppressWarnings("unchecked") // This cast is safe because we double check below. |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 377 | T converted = (T) matchingConverter.getTypeConverter() |
| 378 | .convert(stringValue, key.getTypeLiteral()); |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 379 | |
| 380 | if (converted == null) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 381 | throw new ResolveFailedException(ErrorMessage.converterReturnedNull()); |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 382 | } |
| 383 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 384 | // We have to filter out primitive types because an Integer is not an instance of int, and we |
| 385 | // provide converters for all the primitive types and know that they work anyway. |
limpbizkit | c0fe03b | 2008-06-02 04:46:53 +0000 | [diff] [blame] | 386 | if (!type.getRawType().isInstance(converted)) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 387 | throw new ResolveFailedException(ErrorMessage.conversionTypeError(converted, type)); |
crazyboblee | 7c9d779 | 2007-09-09 03:41:05 +0000 | [diff] [blame] | 388 | } |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 389 | return new ConvertedConstantBindingImpl<T>(this, key, converted, stringBinding); |
| 390 | } |
| 391 | catch (ResolveFailedException e) { |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 392 | throw e; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 393 | } |
| 394 | catch (Exception e) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 395 | throw new ResolveFailedException(ErrorMessage.conversionError(stringValue, |
| 396 | stringBinding.getSource(), type, matchingConverter, e.getMessage())); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
| 400 | private static class ConvertedConstantBindingImpl<T> extends BindingImpl<T> |
| 401 | implements ConvertedConstantBinding<T> { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 402 | final T value; |
| 403 | final Provider<T> provider; |
| 404 | final Binding<String> originalBinding; |
| 405 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 406 | ConvertedConstantBindingImpl( |
| 407 | InjectorImpl injector, Key<T> key, T value, Binding<String> originalBinding) { |
| 408 | super(injector, key, SourceProviders.UNKNOWN_SOURCE, new ConstantFactory<T>(value), |
| 409 | Scopes.NO_SCOPE, LoadStrategy.LAZY); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 410 | this.value = value; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 411 | provider = Providers.of(value); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 412 | this.originalBinding = originalBinding; |
| 413 | } |
| 414 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 415 | @Override public Provider<T> getProvider() { |
| 416 | return provider; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | public void accept(BindingVisitor<? super T> bindingVisitor) { |
| 420 | bindingVisitor.visit(this); |
| 421 | } |
| 422 | |
| 423 | public T getValue() { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 424 | return value; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | public Binding<String> getOriginal() { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 428 | return originalBinding; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 429 | } |
| 430 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 431 | @Override public String toString() { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 432 | return new ToStringBuilder(ConvertedConstantBinding.class) |
| 433 | .add("key", key) |
| 434 | .add("value", value) |
| 435 | .add("original", originalBinding) |
| 436 | .toString(); |
| 437 | } |
| 438 | } |
| 439 | |
limpbizkit | 0575714 | 2008-06-02 06:58:47 +0000 | [diff] [blame] | 440 | <T> BindingImpl<T> createBindingFromType(Class<T> type, LoadStrategy loadStrategy) |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 441 | throws ResolveFailedException { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 442 | BindingImpl<T> binding |
| 443 | = createUnitializedBinding(type, null, SourceProviders.defaultSource(), loadStrategy); |
limpbizkit | 5141187 | 2008-05-13 22:15:29 +0000 | [diff] [blame] | 444 | initializeBinding(binding); |
| 445 | return binding; |
| 446 | } |
| 447 | |
| 448 | <T> void initializeBinding(BindingImpl<T> binding) throws ResolveFailedException { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 449 | // Put the partially constructed binding in the map a little early. This enables us to handle |
| 450 | // circular dependencies. Example: FooImpl -> BarImpl -> FooImpl. |
| 451 | // Note: We don't need to synchronize on jitBindings during injector creation. |
limpbizkit | 5141187 | 2008-05-13 22:15:29 +0000 | [diff] [blame] | 452 | if (binding instanceof ClassBindingImpl<?>) { |
| 453 | Key<T> key = binding.getKey(); |
| 454 | jitBindings.put(key, binding); |
| 455 | boolean successful = false; |
| 456 | try { |
| 457 | binding.initialize(this); |
| 458 | successful = true; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 459 | } |
| 460 | finally { |
limpbizkit | 5141187 | 2008-05-13 22:15:29 +0000 | [diff] [blame] | 461 | if (!successful) { |
| 462 | jitBindings.remove(key); |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | /** |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 469 | * Creates a binding for an injectable type with the given scope. Looks for a scope on the type if |
| 470 | * none is specified. |
limpbizkit | 5141187 | 2008-05-13 22:15:29 +0000 | [diff] [blame] | 471 | */ |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 472 | <T> BindingImpl<T> createUnitializedBinding( |
| 473 | Class<T> type, Scope scope, Object source, LoadStrategy loadStrategy) |
| 474 | throws ResolveFailedException { |
limpbizkit | c0fe03b | 2008-06-02 04:46:53 +0000 | [diff] [blame] | 475 | // Don't try to inject arrays, or enums. |
| 476 | if (type.isArray() || type.isEnum()) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 477 | throw new ResolveFailedException(ErrorMessage.missingBinding(type)); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | // Handle @ImplementedBy |
| 481 | ImplementedBy implementedBy = type.getAnnotation(ImplementedBy.class); |
| 482 | if (implementedBy != null) { |
crazyboblee | d71c19e | 2007-09-08 01:55:10 +0000 | [diff] [blame] | 483 | // TODO: Scope internal factory. |
limpbizkit | 0575714 | 2008-06-02 06:58:47 +0000 | [diff] [blame] | 484 | return createImplementedByBinding(type, implementedBy, loadStrategy); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | // Handle @ProvidedBy. |
| 488 | ProvidedBy providedBy = type.getAnnotation(ProvidedBy.class); |
| 489 | if (providedBy != null) { |
crazyboblee | d71c19e | 2007-09-08 01:55:10 +0000 | [diff] [blame] | 490 | // TODO: Scope internal factory. |
limpbizkit | 0575714 | 2008-06-02 06:58:47 +0000 | [diff] [blame] | 491 | return createProvidedByBinding(type, providedBy, loadStrategy); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 492 | } |
| 493 | |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 494 | // We can't inject abstract classes. |
| 495 | // TODO: Method interceptors could actually enable us to implement |
| 496 | // abstract types. Should we remove this restriction? |
| 497 | if (Modifier.isAbstract(type.getModifiers())) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 498 | throw new ResolveFailedException(ErrorMessage.cannotInjectAbstractType(type)); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | // Error: Inner class. |
| 502 | if (Classes.isInnerClass(type)) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 503 | throw new ResolveFailedException(ErrorMessage.cannotInjectInnerClass(type)); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | if (scope == null) { |
| 507 | scope = Scopes.getScopeForType(type, scopes, errorHandler); |
| 508 | } |
| 509 | |
| 510 | Key<T> key = Key.get(type); |
| 511 | |
limpbizkit | 0575714 | 2008-06-02 06:58:47 +0000 | [diff] [blame] | 512 | LateBoundConstructor<T> lateBoundConstructor = new LateBoundConstructor<T>(); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 513 | InternalFactory<? extends T> scopedFactory |
| 514 | = Scopes.scope(key, this, lateBoundConstructor, scope); |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 515 | return new ClassBindingImpl<T>( |
| 516 | this, key, source, scopedFactory, scope, lateBoundConstructor, loadStrategy); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | static class LateBoundConstructor<T> implements InternalFactory<T> { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 520 | ConstructorInjector<T> constructorInjector; |
| 521 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 522 | void bind(InjectorImpl injector, Class<T> implementation) throws ResolveFailedException { |
| 523 | constructorInjector = injector.getConstructor(implementation); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | @SuppressWarnings("unchecked") |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 527 | public T get(InternalContext context, InjectionPoint<?> injectionPoint) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 528 | checkState(constructorInjector != null, "Construct before bind, " + constructorInjector); |
limpbizkit | 00d513f | 2008-05-17 07:51:07 +0000 | [diff] [blame] | 529 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 530 | // This may not actually be safe because it could return a super type of T (if that's all the |
| 531 | // client needs), but it should be OK in practice thanks to the wonders of erasure. |
| 532 | return (T) constructorInjector.construct(context, injectionPoint.getKey().getRawType()); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 536 | /** Creates a binding for a type annotated with @ProvidedBy. */ |
| 537 | <T> BindingImpl<T> createProvidedByBinding(final Class<T> type, ProvidedBy providedBy, |
| 538 | LoadStrategy loadStrategy) throws ResolveFailedException { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 539 | final Class<? extends Provider<?>> providerType = providedBy.value(); |
| 540 | |
| 541 | // Make sure it's not the same type. TODO: Can we check for deeper loops? |
| 542 | if (providerType == type) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 543 | throw new ResolveFailedException(ErrorMessage.recursiveProviderType()); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 544 | } |
| 545 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 546 | // TODO: Make sure the provided type extends type. We at least check the type at runtime below. |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 547 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 548 | // Assume the provider provides an appropriate type. We double check at runtime. |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 549 | @SuppressWarnings("unchecked") |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 550 | Key<? extends Provider<T>> providerKey = (Key<? extends Provider<T>>) Key.get(providerType); |
| 551 | final BindingImpl<? extends Provider<?>> providerBinding = getBindingOrThrow(providerKey); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 552 | |
| 553 | InternalFactory<T> internalFactory = new InternalFactory<T>() { |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 554 | public T get(InternalContext context, InjectionPoint injectionPoint) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 555 | Provider<?> provider = providerBinding.internalFactory.get(context, injectionPoint); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 556 | Object o = provider.get(); |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 557 | if (o != null && !type.isInstance(o)) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 558 | errorHandler.handle(StackTraceElements.forType(type), |
| 559 | ErrorMessage.subtypeNotProvided(providerType, type)); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 560 | throw new AssertionError(); |
| 561 | } |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 562 | |
| 563 | @SuppressWarnings("unchecked") // protected by isInstance() check above |
| 564 | T t = (T) o; |
| 565 | return t; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 566 | } |
| 567 | }; |
| 568 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 569 | return new LinkedProviderBindingImpl<T>( |
| 570 | this, |
| 571 | Key.get(type), |
| 572 | StackTraceElements.forType(type), |
| 573 | internalFactory, |
| 574 | Scopes.NO_SCOPE, |
| 575 | providerKey, |
| 576 | loadStrategy); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 577 | } |
| 578 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 579 | /** Creates a binding for a type annotated with @ImplementedBy. */ |
| 580 | <T> BindingImpl<T> createImplementedByBinding( |
| 581 | Class<T> type, ImplementedBy implementedBy, LoadStrategy loadStrategy) |
| 582 | throws ResolveFailedException { |
| 583 | // TODO: Use scope annotation on type if present. Right now, we always use NO_SCOPE. |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 584 | Class<?> implementationType = implementedBy.value(); |
| 585 | |
| 586 | // Make sure it's not the same type. TODO: Can we check for deeper cycles? |
| 587 | if (implementationType == type) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 588 | throw new ResolveFailedException(ErrorMessage.recursiveImplementationType()); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | // Make sure implementationType extends type. |
| 592 | if (!type.isAssignableFrom(implementationType)) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 593 | throw new ResolveFailedException(ErrorMessage.notASubtype(implementationType, type)); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | // After the preceding check, this cast is safe. |
| 597 | @SuppressWarnings("unchecked") |
| 598 | Class<? extends T> subclass = (Class<? extends T>) implementationType; |
| 599 | |
| 600 | // Look up the target binding. |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 601 | final BindingImpl<? extends T> targetBinding = getBindingOrThrow(Key.get(subclass)); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 602 | |
| 603 | InternalFactory<T> internalFactory = new InternalFactory<T>() { |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 604 | public T get(InternalContext context, InjectionPoint<?> injectionPoint) { |
| 605 | return targetBinding.internalFactory.get(context, injectionPoint); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 606 | } |
| 607 | }; |
| 608 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 609 | return new LinkedBindingImpl<T>( |
| 610 | this, |
| 611 | Key.get(type), |
| 612 | StackTraceElements.forType(type), |
| 613 | internalFactory, |
| 614 | Scopes.NO_SCOPE, |
| 615 | Key.get(subclass), |
| 616 | loadStrategy); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 617 | } |
| 618 | |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 619 | <T> BindingImpl<T> createBindingJustInTime(Key<T> key) throws ResolveFailedException { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 620 | // Handle cases where T is a Provider<?>. |
| 621 | if (isProvider(key)) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 622 | // These casts are safe. We know T extends Provider<X> and that given Key<Provider<X>>, |
| 623 | // createProviderBinding() will return BindingImpl<Provider<X>>. |
| 624 | @SuppressWarnings("unchecked") |
| 625 | BindingImpl<T> binding = (BindingImpl<T>) createProviderBinding((Key) key, LoadStrategy.LAZY); |
crazyboblee | 5d57569 | 2007-09-07 17:42:28 +0000 | [diff] [blame] | 626 | return binding; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 627 | } |
| 628 | |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 629 | // Try to convert a constant string binding to the requested type. |
| 630 | BindingImpl<T> convertedBinding = convertConstantStringBinding(key); |
| 631 | if (convertedBinding != null) { |
| 632 | return convertedBinding; |
| 633 | } |
| 634 | |
| 635 | // If the key has an annotation... |
| 636 | if (key.hasAnnotationType()) { |
| 637 | // Look for a binding without annotation attributes or return null. |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 638 | if (key.hasAttributes()) { |
| 639 | try { |
| 640 | return getBindingOrThrow(key.withoutAttributes()); |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 641 | } |
| 642 | catch (ResolveFailedException ignored) { |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 643 | // throw with a more appropriate message below |
| 644 | } |
| 645 | } |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 646 | throw new ResolveFailedException(ErrorMessage.missingBinding(key)); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 647 | } |
| 648 | |
limpbizkit | 9b0be9f | 2008-03-28 06:12:41 +0000 | [diff] [blame] | 649 | // Create a binding based on the raw type. |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 650 | @SuppressWarnings("unchecked") |
limpbizkit | 9b0be9f | 2008-03-28 06:12:41 +0000 | [diff] [blame] | 651 | Class<T> clazz = (Class<T>) key.getTypeLiteral().getRawType(); |
limpbizkit | 0575714 | 2008-06-02 06:58:47 +0000 | [diff] [blame] | 652 | return createBindingFromType(clazz, LoadStrategy.LAZY); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 653 | } |
| 654 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 655 | <T> InternalFactory<? extends T> getInternalFactory(Key<T> key) throws ResolveFailedException { |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 656 | return getBindingOrThrow(key).internalFactory; |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 657 | } |
| 658 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 659 | /** Field and method injectors. */ |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 660 | final Map<Class<?>, List<SingleMemberInjector>> injectors |
| 661 | = new ReferenceCache<Class<?>, List<SingleMemberInjector>>() { |
| 662 | protected List<SingleMemberInjector> create(Class<?> key) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 663 | List<SingleMemberInjector> injectors = Lists.newArrayList(); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 664 | addInjectors(key, injectors); |
| 665 | return injectors; |
| 666 | } |
| 667 | }; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 668 | |
| 669 | /** |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 670 | * Recursively adds injectors for fields and methods from the given class to the given list. |
| 671 | * Injects parent classes before sub classes. |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 672 | */ |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 673 | void addInjectors(Class clazz, List<SingleMemberInjector> injectors) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 674 | if (clazz == Object.class) { |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | // Add injectors for superclass first. |
| 679 | addInjectors(clazz.getSuperclass(), injectors); |
| 680 | |
| 681 | // TODO (crazybob): Filter out overridden members. |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 682 | addSingleInjectorsForFields(clazz.getDeclaredFields(), false, injectors); |
| 683 | addSingleInjectorsForMethods(clazz.getDeclaredMethods(), false, injectors); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 684 | } |
| 685 | |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 686 | void addSingleInjectorsForMethods(Method[] methods, boolean statics, |
| 687 | List<SingleMemberInjector> injectors) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 688 | addInjectorsForMembers(Arrays.asList(methods), statics, injectors, |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 689 | new SingleInjectorFactory<Method>() { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 690 | public SingleMemberInjector create(InjectorImpl injector, Method method) |
| 691 | throws ResolveFailedException { |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 692 | return new SingleMethodInjector(injector, method); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 693 | } |
| 694 | }); |
| 695 | } |
| 696 | |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 697 | void addSingleInjectorsForFields(Field[] fields, boolean statics, |
| 698 | List<SingleMemberInjector> injectors) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 699 | addInjectorsForMembers(Arrays.asList(fields), statics, injectors, |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 700 | new SingleInjectorFactory<Field>() { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 701 | public SingleMemberInjector create(InjectorImpl injector, Field field) |
| 702 | throws ResolveFailedException { |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 703 | return new SingleFieldInjector(injector, field); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 704 | } |
| 705 | }); |
| 706 | } |
| 707 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 708 | <M extends Member & AnnotatedElement> void addInjectorsForMembers(List<M> members, |
| 709 | boolean statics, List<SingleMemberInjector> injectors, |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 710 | SingleInjectorFactory<M> injectorFactory) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 711 | for (M member : members) { |
| 712 | if (isStatic(member) == statics) { |
crazyboblee | 9a3861b | 2007-02-20 03:37:31 +0000 | [diff] [blame] | 713 | Inject inject = member.getAnnotation(Inject.class); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 714 | if (inject != null) { |
| 715 | try { |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 716 | injectors.add(injectorFactory.create(this, member)); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 717 | } |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 718 | catch (ResolveFailedException e) { |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 719 | if (!inject.optional()) { |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 720 | // TODO: Report errors for more than one parameter per member. |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 721 | errorHandler.handle(e.getMessage(member)); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | |
crazyboblee | 1fc4978 | 2007-02-25 21:02:47 +0000 | [diff] [blame] | 729 | Map<Key<?>, BindingImpl<?>> internalBindings() { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 730 | return explicitBindings; |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 731 | } |
| 732 | |
kevinb9n | 225310e | 2007-02-20 04:12:01 +0000 | [diff] [blame] | 733 | // not test-covered |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 734 | public Map<Key<?>, Binding<?>> getBindings() { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 735 | return Collections.<Key<?>, Binding<?>>unmodifiableMap(explicitBindings); |
crazyboblee | a6e7398 | 2007-02-02 00:21:07 +0000 | [diff] [blame] | 736 | } |
| 737 | |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 738 | interface SingleInjectorFactory<M extends Member & AnnotatedElement> { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 739 | SingleMemberInjector create(InjectorImpl injector, M member) throws ResolveFailedException; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | private boolean isStatic(Member member) { |
| 743 | return Modifier.isStatic(member.getModifiers()); |
| 744 | } |
| 745 | |
kevinb9n | 48d1307 | 2007-02-12 18:21:26 +0000 | [diff] [blame] | 746 | private static class BindingsMultimap { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 747 | final Multimap<TypeLiteral<?>, Binding<?>> multimap = Multimaps.newArrayListMultimap(); |
kevinb9n | 48d1307 | 2007-02-12 18:21:26 +0000 | [diff] [blame] | 748 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 749 | <T> void put(TypeLiteral<T> type, BindingImpl<T> binding) { |
| 750 | multimap.put(type, binding); |
kevinb9n | 48d1307 | 2007-02-12 18:21:26 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | // safe because we only put matching entries into the map |
| 754 | @SuppressWarnings("unchecked") |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 755 | <T> List<Binding<T>> getAll(TypeLiteral<T> type) { |
| 756 | return (List<Binding<T>>) (List) multimap.get(type); |
kevinb9n | 48d1307 | 2007-02-12 18:21:26 +0000 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 760 | class SingleFieldInjector implements SingleMemberInjector { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 761 | final Field field; |
| 762 | final InternalFactory<?> factory; |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 763 | final InjectionPoint<?> injectionPoint; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 764 | |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 765 | public SingleFieldInjector(final InjectorImpl injector, Field field) |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 766 | throws ResolveFailedException { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 767 | this.field = field; |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 768 | |
| 769 | // Ewwwww... |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 770 | field.setAccessible(true); |
| 771 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 772 | final Key<?> key |
| 773 | = Keys.get(field.getGenericType(), field, field.getAnnotations(), errorHandler); |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 774 | factory = new ResolvingCallable<InternalFactory<?>>() { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 775 | public InternalFactory<?> call() throws ResolveFailedException { |
| 776 | return injector.getInternalFactory(key); |
| 777 | } |
| 778 | }.runWithDefaultSource(StackTraceElements.forMember(field)); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 779 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 780 | injectionPoint = InjectionPoint.newInstance( |
| 781 | field, Nullability.forAnnotations(field.getAnnotations()), key, injector); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 782 | } |
| 783 | |
crazyboblee | d71c19e | 2007-09-08 01:55:10 +0000 | [diff] [blame] | 784 | public Collection<Dependency<?>> getDependencies() { |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 785 | return Collections.<Dependency<?>>singleton(injectionPoint); |
crazyboblee | d71c19e | 2007-09-08 01:55:10 +0000 | [diff] [blame] | 786 | } |
| 787 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 788 | public void inject(InternalContext context, Object o) { |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 789 | context.setInjectionPoint(injectionPoint); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 790 | try { |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 791 | Object value = factory.get(context, injectionPoint); |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 792 | field.set(o, value); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 793 | } |
| 794 | catch (IllegalAccessException e) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 795 | throw new AssertionError(e); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 796 | } |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 797 | catch (CreationException e) { |
kevinb9n | 9119a63 | 2007-02-22 01:28:24 +0000 | [diff] [blame] | 798 | throw e; |
| 799 | } |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 800 | catch (ProvisionException provisionException) { |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 801 | provisionException.addContext(injectionPoint); |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 802 | throw provisionException; |
| 803 | } |
| 804 | catch (RuntimeException runtimeException) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 805 | throw new ProvisionException( |
| 806 | ErrorMessage.errorInjectingField().toString(), runtimeException); |
kevinb9n | 9119a63 | 2007-02-22 01:28:24 +0000 | [diff] [blame] | 807 | } |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 808 | finally { |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 809 | context.setInjectionPoint(null); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | /** |
| 815 | * Gets parameter injectors. |
| 816 | * |
| 817 | * @param member to which the parameters belong |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 818 | * @return injections |
| 819 | */ |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 820 | SingleParameterInjector<?>[] getParametersInjectors(Member member, List<Parameter<?>> parameters) |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 821 | throws ResolveFailedException { |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 822 | SingleParameterInjector<?>[] parameterInjectors |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 823 | = new SingleParameterInjector<?>[parameters.size()]; |
crazyboblee | 1596921 | 2007-02-01 02:01:59 +0000 | [diff] [blame] | 824 | int index = 0; |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 825 | for (Parameter<?> parameter : parameters) { |
| 826 | parameterInjectors[index] = createParameterInjector(parameter, member); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 827 | index++; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 828 | } |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 829 | return parameterInjectors; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 830 | } |
| 831 | |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 832 | <T> SingleParameterInjector<T> createParameterInjector( |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 833 | final Parameter<T> parameter, Member member) throws ResolveFailedException { |
| 834 | InternalFactory<? extends T> factory = new ResolvingCallable<InternalFactory<? extends T>>() { |
| 835 | public InternalFactory<? extends T> call() throws ResolveFailedException { |
| 836 | return getInternalFactory(parameter.getKey()); |
| 837 | } |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 838 | }.runWithDefaultSource(StackTraceElements.forMember(member)); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 839 | |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 840 | InjectionPoint<T> injectionPoint = InjectionPoint.newInstance( |
limpbizkit | 916f548 | 2008-04-16 20:51:14 +0000 | [diff] [blame] | 841 | member, parameter.getIndex(), parameter.getNullability(), parameter.getKey(), this); |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 842 | return new SingleParameterInjector<T>(injectionPoint, factory); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 843 | } |
| 844 | |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 845 | static class SingleMethodInjector implements SingleMemberInjector { |
crazyboblee | 0b3189c | 2007-02-24 00:14:51 +0000 | [diff] [blame] | 846 | final MethodInvoker methodInvoker; |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 847 | final SingleParameterInjector<?>[] parameterInjectors; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 848 | |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 849 | public SingleMethodInjector(InjectorImpl injector, final Method method) |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 850 | throws ResolveFailedException { |
crazyboblee | 0b3189c | 2007-02-24 00:14:51 +0000 | [diff] [blame] | 851 | // We can't use FastMethod if the method is private. |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 852 | if (Modifier.isPrivate(method.getModifiers()) || Modifier.isProtected(method.getModifiers())) |
| 853 | { |
crazyboblee | 0b3189c | 2007-02-24 00:14:51 +0000 | [diff] [blame] | 854 | method.setAccessible(true); |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 855 | methodInvoker = new MethodInvoker() { |
| 856 | public Object invoke(Object target, Object... parameters) |
| 857 | throws IllegalAccessException, InvocationTargetException { |
crazyboblee | 0b3189c | 2007-02-24 00:14:51 +0000 | [diff] [blame] | 858 | return method.invoke(target, parameters); |
| 859 | } |
| 860 | }; |
| 861 | } |
| 862 | else { |
| 863 | FastClass fastClass = GuiceFastClass.create(method.getDeclaringClass()); |
| 864 | final FastMethod fastMethod = fastClass.getMethod(method); |
| 865 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 866 | methodInvoker = new MethodInvoker() { |
crazyboblee | 0b3189c | 2007-02-24 00:14:51 +0000 | [diff] [blame] | 867 | public Object invoke(Object target, Object... parameters) |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 868 | throws IllegalAccessException, InvocationTargetException { |
crazyboblee | 0b3189c | 2007-02-24 00:14:51 +0000 | [diff] [blame] | 869 | return fastMethod.invoke(target, parameters); |
| 870 | } |
| 871 | }; |
| 872 | } |
| 873 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 874 | parameterInjectors = method.getGenericParameterTypes().length > 0 |
| 875 | ? injector.getParametersInjectors( |
| 876 | method, Parameter.forMethod(injector.errorHandler, method)) |
crazyboblee | 4727ee2 | 2007-01-30 03:13:38 +0000 | [diff] [blame] | 877 | : null; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | public void inject(InternalContext context, Object o) { |
| 881 | try { |
crazyboblee | 0b3189c | 2007-02-24 00:14:51 +0000 | [diff] [blame] | 882 | methodInvoker.invoke(o, getParameters(context, parameterInjectors)); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 883 | } |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 884 | catch (IllegalAccessException e) { |
| 885 | throw new AssertionError(e); |
| 886 | } |
limpbizkit | c808df0 | 2007-08-25 03:25:13 +0000 | [diff] [blame] | 887 | catch (InvocationTargetException e) { |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 888 | Throwable cause = e.getCause() != null ? e.getCause() : e; |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 889 | throw new ProvisionException(ErrorMessage.errorInjectingMethod().toString(), cause); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 890 | } |
| 891 | } |
crazyboblee | d71c19e | 2007-09-08 01:55:10 +0000 | [diff] [blame] | 892 | |
| 893 | public Collection<Dependency<?>> getDependencies() { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 894 | List<Dependency<?>> dependencies = new ArrayList<Dependency<?>>(parameterInjectors.length); |
crazyboblee | d71c19e | 2007-09-08 01:55:10 +0000 | [diff] [blame] | 895 | for (SingleParameterInjector<?> parameterInjector : parameterInjectors) { |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 896 | dependencies.add(parameterInjector.injectionPoint); |
crazyboblee | d71c19e | 2007-09-08 01:55:10 +0000 | [diff] [blame] | 897 | } |
| 898 | return Collections.unmodifiableList(dependencies); |
| 899 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 900 | } |
| 901 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 902 | /** Invokes a method. */ |
crazyboblee | 0b3189c | 2007-02-24 00:14:51 +0000 | [diff] [blame] | 903 | interface MethodInvoker { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 904 | Object invoke(Object target, Object... parameters) |
| 905 | throws IllegalAccessException, InvocationTargetException; |
crazyboblee | 0b3189c | 2007-02-24 00:14:51 +0000 | [diff] [blame] | 906 | } |
| 907 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 908 | final Map<Class<?>, Object> constructors = new ReferenceCache<Class<?>, Object>() { |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 909 | @SuppressWarnings("unchecked") |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 910 | protected Object create(Class<?> implementation) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 911 | // TODO: reduce duplication between this and createUnitializedBinding |
crazyboblee | d0c4b8b | 2007-09-06 02:47:04 +0000 | [diff] [blame] | 912 | if (!Classes.isConcrete(implementation)) { |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 913 | return new ResolveFailedException( |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 914 | ErrorMessage.cannotInjectAbstractType(implementation)); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 915 | } |
crazyboblee | d0c4b8b | 2007-09-06 02:47:04 +0000 | [diff] [blame] | 916 | if (Classes.isInnerClass(implementation)) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 917 | return new ResolveFailedException( |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 918 | ErrorMessage.cannotInjectInnerClass(implementation)); |
kevinb9n | 9da90b3 | 2007-04-20 15:28:04 +0000 | [diff] [blame] | 919 | } |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 920 | return new ConstructorInjector(InjectorImpl.this, implementation); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 921 | } |
| 922 | }; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 923 | |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 924 | static class SingleParameterInjector<T> { |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 925 | final InjectionPoint<T> injectionPoint; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 926 | final InternalFactory<? extends T> factory; |
| 927 | |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 928 | public SingleParameterInjector(InjectionPoint<T> injectionPoint, |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 929 | InternalFactory<? extends T> factory) { |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 930 | this.injectionPoint = injectionPoint; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 931 | this.factory = factory; |
| 932 | } |
| 933 | |
crazyboblee | 664a82e | 2007-01-25 23:09:51 +0000 | [diff] [blame] | 934 | T inject(InternalContext context) { |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 935 | context.setInjectionPoint(injectionPoint); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 936 | try { |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 937 | return factory.get(context, injectionPoint); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 938 | } |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 939 | catch (CreationException e) { |
kevinb9n | 9119a63 | 2007-02-22 01:28:24 +0000 | [diff] [blame] | 940 | throw e; |
| 941 | } |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 942 | catch (ProvisionException provisionException) { |
| 943 | provisionException.addContext(injectionPoint); |
| 944 | throw provisionException; |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 945 | } |
| 946 | catch (RuntimeException runtimeException) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 947 | throw new ProvisionException( |
| 948 | ErrorMessage.errorInjectingMethod().toString(), runtimeException); |
kevinb9n | 9119a63 | 2007-02-22 01:28:24 +0000 | [diff] [blame] | 949 | } |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 950 | finally { |
limpbizkit | 05a6c5e | 2008-06-05 00:29:11 +0000 | [diff] [blame] | 951 | context.setInjectionPoint(null); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | } |
| 955 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 956 | /** Iterates over parameter injectors and creates an array of parameter values. */ |
| 957 | static Object[] getParameters( |
| 958 | InternalContext context, SingleParameterInjector[] parameterInjectors) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 959 | if (parameterInjectors == null) { |
| 960 | return null; |
| 961 | } |
| 962 | |
| 963 | Object[] parameters = new Object[parameterInjectors.length]; |
| 964 | for (int i = 0; i < parameters.length; i++) { |
crazyboblee | 664a82e | 2007-01-25 23:09:51 +0000 | [diff] [blame] | 965 | parameters[i] = parameterInjectors[i].inject(context); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 966 | } |
| 967 | return parameters; |
| 968 | } |
| 969 | |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 970 | void injectMembers(Object o, InternalContext context) { |
limpbizkit | c808df0 | 2007-08-25 03:25:13 +0000 | [diff] [blame] | 971 | if (o == null) { |
| 972 | return; |
| 973 | } |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 974 | List<SingleMemberInjector> injectorsForClass = injectors.get(o.getClass()); |
| 975 | for (SingleMemberInjector injector : injectorsForClass) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 976 | injector.inject(context, o); |
| 977 | } |
| 978 | } |
| 979 | |
kevinb9n | 225310e | 2007-02-20 04:12:01 +0000 | [diff] [blame] | 980 | // Not test-covered |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 981 | public void injectMembers(final Object o) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 982 | callInContext(new ContextualCallable<Void>() { |
| 983 | public Void call(InternalContext context) { |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 984 | injectMembers(o, context); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 985 | return null; |
| 986 | } |
| 987 | }); |
| 988 | } |
| 989 | |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 990 | public <T> Provider<T> getProvider(Class<T> type) { |
| 991 | return getProvider(Key.get(type)); |
crazyboblee | e5fbbb0 | 2007-02-05 07:00:27 +0000 | [diff] [blame] | 992 | } |
| 993 | |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 994 | <T> Provider<T> getProviderOrThrow(final Key<T> key) throws ResolveFailedException { |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 995 | final InternalFactory<? extends T> factory = getInternalFactory(key); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 996 | |
crazyboblee | bd9544e | 2007-02-25 20:32:11 +0000 | [diff] [blame] | 997 | return new Provider<T>() { |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 998 | public T get() { |
| 999 | return callInContext(new ContextualCallable<T>() { |
| 1000 | public T call(InternalContext context) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 1001 | InjectionPoint<T> injectionPoint = InjectionPoint.newInstance(key, InjectorImpl.this); |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 1002 | context.setInjectionPoint(injectionPoint); |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 1003 | try { |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 1004 | return factory.get(context, injectionPoint); |
| 1005 | } |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 1006 | catch (ProvisionException provisionException) { |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 1007 | provisionException.addContext(injectionPoint); |
| 1008 | throw provisionException; |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 1009 | } |
| 1010 | finally { |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 1011 | context.setInjectionPoint(null); |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 1012 | } |
| 1013 | } |
| 1014 | }); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1015 | } |
crazyboblee | d7908e8 | 2007-02-16 01:04:53 +0000 | [diff] [blame] | 1016 | |
| 1017 | public String toString() { |
| 1018 | return factory.toString(); |
| 1019 | } |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 1020 | }; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 1023 | public <T> Provider<T> getProvider(final Key<T> key) { |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 1024 | try { |
| 1025 | return getProviderOrThrow(key); |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 1026 | } |
| 1027 | catch (ResolveFailedException e) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 1028 | throw new CreationException(new Message( |
| 1029 | ErrorMessage.bindingNotFound(key, e.getMessage()).toString())); |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 1030 | } |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
kevinb9n | 27f8a58 | 2007-02-28 22:54:06 +0000 | [diff] [blame] | 1033 | public <T> T getInstance(Key<T> key) { |
| 1034 | return getProvider(key).get(); |
| 1035 | } |
| 1036 | |
| 1037 | public <T> T getInstance(Class<T> type) { |
| 1038 | return getProvider(type).get(); |
| 1039 | } |
| 1040 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 1041 | final ThreadLocal<InternalContext[]> localContext = new ThreadLocal<InternalContext[]>() { |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 1042 | protected InternalContext[] initialValue() { |
| 1043 | return new InternalContext[1]; |
| 1044 | } |
| 1045 | }; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1046 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 1047 | /** Looks up thread local context. Creates (and removes) a new context if necessary. */ |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1048 | <T> T callInContext(ContextualCallable<T> callable) { |
| 1049 | InternalContext[] reference = localContext.get(); |
| 1050 | if (reference[0] == null) { |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 1051 | reference[0] = new InternalContext(this); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1052 | try { |
| 1053 | return callable.call(reference[0]); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 1054 | } |
| 1055 | finally { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1056 | // Only remove the context if this call created it. |
| 1057 | reference[0] = null; |
| 1058 | } |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 1059 | } |
| 1060 | else { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1061 | // Someone else will clean up this context. |
| 1062 | return callable.call(reference[0]); |
| 1063 | } |
| 1064 | } |
| 1065 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 1066 | /** Gets a constructor function for a given implementation class. */ |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1067 | @SuppressWarnings("unchecked") |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 1068 | <T> ConstructorInjector<T> getConstructor(Class<T> implementation) throws ResolveFailedException { |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 1069 | Object o = constructors.get(implementation); |
| 1070 | if (o instanceof ResolveFailedException) { |
| 1071 | throw (ResolveFailedException) o; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 1072 | } |
| 1073 | else if (o instanceof ConstructorInjector<?>) { |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 1074 | return (ConstructorInjector<T>) o; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 1075 | } |
| 1076 | else { |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 1077 | throw new AssertionError(); |
| 1078 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 1081 | /** Injects a field or method in a given object. */ |
limpbizkit | 8b23745 | 2008-04-22 06:47:36 +0000 | [diff] [blame] | 1082 | public interface SingleMemberInjector { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1083 | void inject(InternalContext context, Object o); |
crazyboblee | d71c19e | 2007-09-08 01:55:10 +0000 | [diff] [blame] | 1084 | Collection<Dependency<?>> getDependencies(); |
| 1085 | } |
| 1086 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 1087 | List<Dependency<?>> getModifiableFieldAndMethodDependenciesFor(Class<?> clazz) { |
crazyboblee | d71c19e | 2007-09-08 01:55:10 +0000 | [diff] [blame] | 1088 | List<SingleMemberInjector> injectors = this.injectors.get(clazz); |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 1089 | List<Dependency<?>> dependencies = Lists.newArrayList(); |
crazyboblee | d71c19e | 2007-09-08 01:55:10 +0000 | [diff] [blame] | 1090 | for (SingleMemberInjector singleMemberInjector : injectors) { |
| 1091 | dependencies.addAll(singleMemberInjector.getDependencies()); |
| 1092 | } |
| 1093 | return dependencies; |
| 1094 | } |
| 1095 | |
| 1096 | Collection<Dependency<?>> getFieldAndMethodDependenciesFor(Class<?> clazz) { |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 1097 | return Collections.unmodifiableList(getModifiableFieldAndMethodDependenciesFor(clazz)); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 1100 | public String toString() { |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 1101 | return new ToStringBuilder(Injector.class) |
crazyboblee | 712705c | 2007-09-07 03:20:30 +0000 | [diff] [blame] | 1102 | .add("bindings", explicitBindings) |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 1103 | .toString(); |
| 1104 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1105 | } |