limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2008 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.inject; |
| 18 | |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 20 | import com.google.common.collect.Sets; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 21 | import com.google.inject.commands.BindCommand; |
| 22 | import com.google.inject.commands.BindConstantCommand; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 23 | import com.google.inject.commands.BindScoping.Visitor; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 24 | import com.google.inject.commands.BindTarget; |
limpbizkit | 0575714 | 2008-06-02 06:58:47 +0000 | [diff] [blame] | 25 | import com.google.inject.internal.Annotations; |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 26 | import com.google.inject.internal.ErrorMessage; |
limpbizkit | 0575714 | 2008-06-02 06:58:47 +0000 | [diff] [blame] | 27 | import com.google.inject.internal.ResolveFailedException; |
| 28 | import com.google.inject.internal.StackTraceElements; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 29 | import java.lang.annotation.Annotation; |
| 30 | import java.lang.reflect.Type; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 31 | import java.util.Collections; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 32 | import java.util.List; |
| 33 | import java.util.Map; |
| 34 | import java.util.Set; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Handles {@link Binder#bind} and {@link Binder#bindConstant} commands. |
| 38 | * |
| 39 | * @author crazybob@google.com (Bob Lee) |
| 40 | * @author jessewilson@google.com (Jesse Wilson) |
| 41 | */ |
| 42 | class BindCommandProcessor extends CommandProcessor { |
| 43 | |
| 44 | private final InjectorImpl injector; |
| 45 | private final Map<Class<? extends Annotation>, Scope> scopes; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 46 | private final List<CreationListener> creationListeners = Lists.newArrayList(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 47 | private final Map<Key<?>, BindingImpl<?>> bindings; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 48 | private final Set<Object> outstandingInjections; |
| 49 | private final List<Runnable> untargettedBindings = Lists.newArrayList(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 50 | |
| 51 | BindCommandProcessor(InjectorImpl injector, |
| 52 | Map<Class<? extends Annotation>, Scope> scopes, |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 53 | Map<Key<?>, BindingImpl<?>> bindings, |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 54 | Set<Object> outstandingInjections) { |
limpbizkit | ad94bcb | 2008-04-28 00:22:43 +0000 | [diff] [blame] | 55 | super(injector.errorHandler); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 56 | this.injector = injector; |
| 57 | this.scopes = scopes; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 58 | this.bindings = bindings; |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 59 | this.outstandingInjections = outstandingInjections; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override public <T> Boolean visitBind(BindCommand<T> command) { |
| 63 | final Object source = command.getSource(); |
| 64 | |
| 65 | final Key<T> key = command.getKey(); |
| 66 | Class<? super T> rawType = key.getTypeLiteral().getRawType(); |
| 67 | |
| 68 | if (rawType == Provider.class) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 69 | addError(source, ErrorMessage.bindingToProvider()); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 70 | return true; |
| 71 | } |
| 72 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 73 | validateKey(command.getSource(), command.getKey()); |
| 74 | |
limpbizkit | 0575714 | 2008-06-02 06:58:47 +0000 | [diff] [blame] | 75 | final LoadStrategy loadStrategy = command.getScoping().isEagerSingleton() |
| 76 | ? LoadStrategy.EAGER |
| 77 | : LoadStrategy.LAZY; |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 78 | final Scope scope = command.getScoping().acceptVisitor(new Visitor<Scope>() { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 79 | public Scope visitEagerSingleton() { |
| 80 | return Scopes.SINGLETON; |
| 81 | } |
| 82 | |
| 83 | public Scope visitScope(Scope scope) { |
| 84 | return scope; |
| 85 | } |
| 86 | |
| 87 | public Scope visitScopeAnnotation(Class<? extends Annotation> scopeAnnotation) { |
| 88 | Scope scope = scopes.get(scopeAnnotation); |
| 89 | if (scope != null) { |
| 90 | return scope; |
| 91 | } else { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 92 | addError(source, ErrorMessage.scopeNotFound( |
| 93 | "@" + scopeAnnotation.getSimpleName())); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 94 | return Scopes.NO_SCOPE; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | public Scope visitNoScoping() { |
| 99 | return null; |
| 100 | } |
| 101 | }); |
| 102 | |
| 103 | command.getTarget().acceptVisitor(new BindTarget.Visitor<T, Void>() { |
| 104 | public Void visitToInstance(T instance) { |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 105 | ConstantFactory<? extends T> factory = new ConstantFactory<T>(instance); |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 106 | outstandingInjections.add(instance); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 107 | InternalFactory<? extends T> scopedFactory |
| 108 | = Scopes.scope(key, injector, factory, scope); |
limpbizkit | 0575714 | 2008-06-02 06:58:47 +0000 | [diff] [blame] | 109 | putBinding(new InstanceBindingImpl<T>( |
| 110 | injector, key, source, scopedFactory, instance)); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 111 | return null; |
| 112 | } |
| 113 | |
| 114 | public Void visitToProvider(Provider<? extends T> provider) { |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 115 | InternalFactoryToProviderAdapter<? extends T> factory |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 116 | = new InternalFactoryToProviderAdapter<T>(provider, source); |
kevinb9n | 1601ae5 | 2008-06-03 22:21:04 +0000 | [diff] [blame] | 117 | outstandingInjections.add(provider); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 118 | InternalFactory<? extends T> scopedFactory |
| 119 | = Scopes.scope(key, injector, factory, scope); |
limpbizkit | 0575714 | 2008-06-02 06:58:47 +0000 | [diff] [blame] | 120 | putBinding(new ProviderInstanceBindingImpl<T>( |
| 121 | injector, key, source, scopedFactory, scope, provider, loadStrategy)); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 122 | return null; |
| 123 | } |
| 124 | |
| 125 | public Void visitToProviderKey(Key<? extends Provider<? extends T>> providerKey) { |
| 126 | final BoundProviderFactory<T> boundProviderFactory = |
| 127 | new BoundProviderFactory<T>(providerKey, source); |
| 128 | creationListeners.add(boundProviderFactory); |
| 129 | InternalFactory<? extends T> scopedFactory = Scopes.scope( |
| 130 | key, injector, (InternalFactory<? extends T>) boundProviderFactory, scope); |
limpbizkit | 0575714 | 2008-06-02 06:58:47 +0000 | [diff] [blame] | 131 | putBinding(new LinkedProviderBindingImpl<T>( |
| 132 | injector, key, source, scopedFactory, scope, providerKey, loadStrategy)); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 133 | return null; |
| 134 | } |
| 135 | |
| 136 | public Void visitToKey(Key<? extends T> targetKey) { |
| 137 | if (key.equals(targetKey)) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 138 | addError(source, ErrorMessage.recursiveBinding()); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | FactoryProxy<T> factory = new FactoryProxy<T>(key, targetKey, source); |
| 142 | creationListeners.add(factory); |
| 143 | InternalFactory<? extends T> scopedFactory |
| 144 | = Scopes.scope(key, injector, factory, scope); |
limpbizkit | 0575714 | 2008-06-02 06:58:47 +0000 | [diff] [blame] | 145 | putBinding(new LinkedBindingImpl<T>( |
| 146 | injector, key, source, scopedFactory, scope, targetKey, loadStrategy)); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 147 | return null; |
| 148 | } |
| 149 | |
| 150 | public Void visitUntargetted() { |
limpbizkit | f44e9cc | 2008-03-26 06:33:29 +0000 | [diff] [blame] | 151 | final Type type = key.getTypeLiteral().getType(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 152 | |
| 153 | // Error: Missing implementation. |
| 154 | // Example: bind(Date.class).annotatedWith(Red.class); |
| 155 | // We can't assume abstract types aren't injectable. They may have an |
| 156 | // @ImplementedBy annotation or something. |
| 157 | if (key.hasAnnotationType() || !(type instanceof Class<?>)) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 158 | addError(source, ErrorMessage.missingImplementation()); |
limpbizkit | 0575714 | 2008-06-02 06:58:47 +0000 | [diff] [blame] | 159 | putBinding(invalidBinding(injector, key, source)); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 160 | return null; |
| 161 | } |
| 162 | |
limpbizkit | 5141187 | 2008-05-13 22:15:29 +0000 | [diff] [blame] | 163 | // This cast is safe after the preceeding check. |
| 164 | @SuppressWarnings("unchecked") |
| 165 | Class<T> clazz = (Class<T>) type; |
| 166 | final BindingImpl<T> binding; |
| 167 | try { |
limpbizkit | 0575714 | 2008-06-02 06:58:47 +0000 | [diff] [blame] | 168 | binding = injector.createUnitializedBinding(clazz, scope, source, loadStrategy); |
| 169 | putBinding(binding); |
limpbizkit | 5141187 | 2008-05-13 22:15:29 +0000 | [diff] [blame] | 170 | } catch (ResolveFailedException e) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 171 | injector.errorHandler.handle(e.getMessage(source)); |
limpbizkit | 0575714 | 2008-06-02 06:58:47 +0000 | [diff] [blame] | 172 | putBinding(invalidBinding(injector, key, source)); |
limpbizkit | 5141187 | 2008-05-13 22:15:29 +0000 | [diff] [blame] | 173 | return null; |
| 174 | } |
| 175 | |
limpbizkit | f44e9cc | 2008-03-26 06:33:29 +0000 | [diff] [blame] | 176 | untargettedBindings.add(new Runnable() { |
| 177 | public void run() { |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 178 | try { |
limpbizkit | 5141187 | 2008-05-13 22:15:29 +0000 | [diff] [blame] | 179 | injector.initializeBinding(binding); |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 180 | } catch (ResolveFailedException e) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 181 | injector.errorHandler.handle(e.getMessage(source)); |
limpbizkit | f44e9cc | 2008-03-26 06:33:29 +0000 | [diff] [blame] | 182 | } |
limpbizkit | f44e9cc | 2008-03-26 06:33:29 +0000 | [diff] [blame] | 183 | } |
| 184 | }); |
| 185 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 186 | return null; |
| 187 | } |
| 188 | }); |
| 189 | |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | private <T> void validateKey(Object source, Key<T> key) { |
| 194 | if (key.hasAnnotationType()) { |
| 195 | Class<? extends Annotation> annotationType = key.getAnnotationType(); |
| 196 | |
| 197 | if (!Annotations.isRetainedAtRuntime(annotationType)) { |
| 198 | addError(StackTraceElements.forType(annotationType), |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 199 | ErrorMessage.missingRuntimeRetention(source)); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | if (!Key.isBindingAnnotation(annotationType)) { |
| 203 | addError(StackTraceElements.forType(annotationType), |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 204 | ErrorMessage.missingBindingAnnotation(source)); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | <T> InvalidBindingImpl<T> invalidBinding(InjectorImpl injector, Key<T> key, Object source) { |
| 210 | return new InvalidBindingImpl<T>(injector, key, source); |
| 211 | } |
| 212 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 213 | @Override public Boolean visitBindConstant(BindConstantCommand command) { |
limpbizkit | d6967b9 | 2008-05-16 15:28:51 +0000 | [diff] [blame] | 214 | BindTarget<?> target = command.getTarget(); |
| 215 | if (target == null) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 216 | addError(command.getSource(), ErrorMessage.missingConstantValues()); |
limpbizkit | d6967b9 | 2008-05-16 15:28:51 +0000 | [diff] [blame] | 217 | return true; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 218 | } |
| 219 | |
limpbizkit | d6967b9 | 2008-05-16 15:28:51 +0000 | [diff] [blame] | 220 | Object value = target.get(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 221 | validateKey(command.getSource(), command.getKey()); |
| 222 | ConstantFactory<Object> factory = new ConstantFactory<Object>(value); |
limpbizkit | f44e9cc | 2008-03-26 06:33:29 +0000 | [diff] [blame] | 223 | putBinding(new ConstantBindingImpl<Object>( |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 224 | injector, command.getKey(), command.getSource(), factory, value)); |
| 225 | |
| 226 | return true; |
| 227 | } |
| 228 | |
limpbizkit | f44e9cc | 2008-03-26 06:33:29 +0000 | [diff] [blame] | 229 | public void createUntargettedBindings() { |
| 230 | for (Runnable untargettedBinding : untargettedBindings) { |
| 231 | untargettedBinding.run(); |
| 232 | } |
| 233 | } |
| 234 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 235 | public void runCreationListeners(InjectorImpl injector) { |
| 236 | for (CreationListener creationListener : creationListeners) { |
| 237 | creationListener.notify(injector); |
| 238 | } |
| 239 | } |
| 240 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 241 | private void putBinding(BindingImpl<?> binding) { |
| 242 | Key<?> key = binding.getKey(); |
| 243 | Binding<?> original = bindings.get(key); |
| 244 | |
| 245 | Class<?> rawType = key.getRawType(); |
| 246 | if (FORBIDDEN_TYPES.contains(rawType)) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 247 | addError(binding.getSource(), ErrorMessage.cannotBindToGuiceType( |
| 248 | rawType.getSimpleName())); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 249 | return; |
| 250 | } |
| 251 | |
| 252 | if (bindings.containsKey(key)) { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 253 | addError(binding.getSource(), ErrorMessage.bindingAlreadySet(key, |
| 254 | original.getSource())); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 255 | } else { |
| 256 | bindings.put(key, binding); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | private static Set<Class<?>> FORBIDDEN_TYPES = forbiddenTypes(); |
| 261 | |
| 262 | @SuppressWarnings("unchecked") // For generic array creation. |
| 263 | private static Set<Class<?>> forbiddenTypes() { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 264 | Set<Class<?>> set = Sets.newHashSet(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 265 | |
| 266 | Collections.addAll(set, |
| 267 | |
| 268 | // It's unfortunate that we have to maintain a blacklist of specific |
| 269 | // classes, but we can't easily block the whole package because of |
| 270 | // all our unit tests. |
| 271 | |
| 272 | AbstractModule.class, |
| 273 | Binder.class, |
| 274 | Binding.class, |
| 275 | Key.class, |
| 276 | Module.class, |
| 277 | Provider.class, |
| 278 | Scope.class, |
| 279 | TypeLiteral.class); |
| 280 | return Collections.unmodifiableSet(set); |
| 281 | } |
| 282 | |
| 283 | interface CreationListener { |
| 284 | void notify(InjectorImpl injector); |
| 285 | } |
| 286 | } |