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 | |
| 19 | import com.google.inject.commands.BindCommand; |
| 20 | import com.google.inject.commands.BindConstantCommand; |
| 21 | import com.google.inject.commands.BindScoping; |
| 22 | import com.google.inject.commands.BindTarget; |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 23 | import com.google.inject.internal.*; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 24 | |
| 25 | import java.lang.annotation.Annotation; |
| 26 | import java.lang.reflect.Type; |
| 27 | import java.util.*; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Handles {@link Binder#bind} and {@link Binder#bindConstant} commands. |
| 31 | * |
| 32 | * @author crazybob@google.com (Bob Lee) |
| 33 | * @author jessewilson@google.com (Jesse Wilson) |
| 34 | */ |
| 35 | class BindCommandProcessor extends CommandProcessor { |
| 36 | |
| 37 | private final InjectorImpl injector; |
| 38 | private final Map<Class<? extends Annotation>, Scope> scopes; |
| 39 | private final List<CreationListener> creationListeners |
| 40 | = new ArrayList<CreationListener>(); |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 41 | private final List<ContextualCallable<Void>> eagerSingletonCreators |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 42 | = new ArrayList<ContextualCallable<Void>>(); |
| 43 | private final Stage stage; |
| 44 | private final Map<Key<?>, BindingImpl<?>> bindings; |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 45 | private final Map<Object, Void> outstandingInjections; |
limpbizkit | f44e9cc | 2008-03-26 06:33:29 +0000 | [diff] [blame] | 46 | private final List<Runnable> untargettedBindings = new ArrayList<Runnable>(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 47 | |
| 48 | BindCommandProcessor(InjectorImpl injector, |
| 49 | Map<Class<? extends Annotation>, Scope> scopes, |
| 50 | Stage stage, |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 51 | Map<Key<?>, BindingImpl<?>> bindings, |
| 52 | Map<Object, Void> outstandingInjections) { |
limpbizkit | ad94bcb | 2008-04-28 00:22:43 +0000 | [diff] [blame] | 53 | super(injector.errorHandler); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 54 | this.injector = injector; |
| 55 | this.scopes = scopes; |
| 56 | this.stage = stage; |
| 57 | this.bindings = bindings; |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 58 | this.outstandingInjections = outstandingInjections; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override public <T> Boolean visitBind(BindCommand<T> command) { |
| 62 | final Object source = command.getSource(); |
| 63 | |
| 64 | final Key<T> key = command.getKey(); |
| 65 | Class<? super T> rawType = key.getTypeLiteral().getRawType(); |
| 66 | |
| 67 | if (rawType == Provider.class) { |
| 68 | addError(source, ErrorMessages.BINDING_TO_PROVIDER); |
| 69 | return true; |
| 70 | } |
| 71 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 72 | validateKey(command.getSource(), command.getKey()); |
| 73 | |
| 74 | // TODO(jessewilson): Scope annotation on type, like @Singleton |
| 75 | final boolean shouldPreload = command.getScoping().isEagerSingleton(); |
| 76 | final Scope scope = command.getScoping().acceptVisitor(new BindScoping.Visitor<Scope>() { |
| 77 | public Scope visitEagerSingleton() { |
| 78 | return Scopes.SINGLETON; |
| 79 | } |
| 80 | |
| 81 | public Scope visitScope(Scope scope) { |
| 82 | return scope; |
| 83 | } |
| 84 | |
| 85 | public Scope visitScopeAnnotation(Class<? extends Annotation> scopeAnnotation) { |
| 86 | Scope scope = scopes.get(scopeAnnotation); |
| 87 | if (scope != null) { |
| 88 | return scope; |
| 89 | } else { |
| 90 | addError(source, ErrorMessages.SCOPE_NOT_FOUND, |
| 91 | "@" + scopeAnnotation.getSimpleName()); |
| 92 | return Scopes.NO_SCOPE; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | public Scope visitNoScoping() { |
| 97 | return null; |
| 98 | } |
| 99 | }); |
| 100 | |
| 101 | command.getTarget().acceptVisitor(new BindTarget.Visitor<T, Void>() { |
| 102 | public Void visitToInstance(T instance) { |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 103 | ConstantFactory<? extends T> factory = new ConstantFactory<T>(instance); |
| 104 | outstandingInjections.put(instance, null); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 105 | InternalFactory<? extends T> scopedFactory |
| 106 | = Scopes.scope(key, injector, factory, scope); |
| 107 | createBinding(source, shouldPreload, new InstanceBindingImpl<T>( |
| 108 | injector, key, source, scopedFactory, instance)); |
| 109 | return null; |
| 110 | } |
| 111 | |
| 112 | public Void visitToProvider(Provider<? extends T> provider) { |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 113 | InternalFactoryToProviderAdapter<? extends T> factory |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 114 | = new InternalFactoryToProviderAdapter<T>(provider, source); |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 115 | outstandingInjections.put(provider, null); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 116 | InternalFactory<? extends T> scopedFactory |
| 117 | = Scopes.scope(key, injector, factory, scope); |
| 118 | createBinding(source, shouldPreload, new ProviderInstanceBindingImpl<T>( |
| 119 | injector, key, source, scopedFactory, scope, provider)); |
| 120 | return null; |
| 121 | } |
| 122 | |
| 123 | public Void visitToProviderKey(Key<? extends Provider<? extends T>> providerKey) { |
| 124 | final BoundProviderFactory<T> boundProviderFactory = |
| 125 | new BoundProviderFactory<T>(providerKey, source); |
| 126 | creationListeners.add(boundProviderFactory); |
| 127 | InternalFactory<? extends T> scopedFactory = Scopes.scope( |
| 128 | key, injector, (InternalFactory<? extends T>) boundProviderFactory, scope); |
| 129 | createBinding(source, shouldPreload, new LinkedProviderBindingImpl<T>( |
| 130 | injector, key, source, scopedFactory, scope, providerKey)); |
| 131 | return null; |
| 132 | } |
| 133 | |
| 134 | public Void visitToKey(Key<? extends T> targetKey) { |
| 135 | if (key.equals(targetKey)) { |
| 136 | addError(source, ErrorMessages.RECURSIVE_BINDING); |
| 137 | } |
| 138 | |
| 139 | FactoryProxy<T> factory = new FactoryProxy<T>(key, targetKey, source); |
| 140 | creationListeners.add(factory); |
| 141 | InternalFactory<? extends T> scopedFactory |
| 142 | = Scopes.scope(key, injector, factory, scope); |
| 143 | createBinding(source, shouldPreload, new LinkedBindingImpl<T>( |
| 144 | injector, key, source, scopedFactory, scope, targetKey)); |
| 145 | return null; |
| 146 | } |
| 147 | |
| 148 | public Void visitUntargetted() { |
limpbizkit | f44e9cc | 2008-03-26 06:33:29 +0000 | [diff] [blame] | 149 | final Type type = key.getTypeLiteral().getType(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 150 | |
| 151 | // Error: Missing implementation. |
| 152 | // Example: bind(Date.class).annotatedWith(Red.class); |
| 153 | // We can't assume abstract types aren't injectable. They may have an |
| 154 | // @ImplementedBy annotation or something. |
| 155 | if (key.hasAnnotationType() || !(type instanceof Class<?>)) { |
| 156 | addError(source, ErrorMessages.MISSING_IMPLEMENTATION); |
| 157 | createBinding(source, shouldPreload, invalidBinding(injector, key, source)); |
| 158 | return null; |
| 159 | } |
| 160 | |
limpbizkit | 5141187 | 2008-05-13 22:15:29 +0000 | [diff] [blame] | 161 | // This cast is safe after the preceeding check. |
| 162 | @SuppressWarnings("unchecked") |
| 163 | Class<T> clazz = (Class<T>) type; |
| 164 | final BindingImpl<T> binding; |
| 165 | try { |
| 166 | binding = injector.createUnitializedBinding(clazz, scope, source); |
| 167 | createBinding(source, shouldPreload, binding); |
| 168 | } catch (ResolveFailedException e) { |
| 169 | injector.errorHandler.handle(source, e.getMessage()); |
| 170 | createBinding(source, shouldPreload, invalidBinding(injector, key, source)); |
| 171 | return null; |
| 172 | } |
| 173 | |
limpbizkit | f44e9cc | 2008-03-26 06:33:29 +0000 | [diff] [blame] | 174 | untargettedBindings.add(new Runnable() { |
| 175 | public void run() { |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 176 | try { |
limpbizkit | 5141187 | 2008-05-13 22:15:29 +0000 | [diff] [blame] | 177 | injector.initializeBinding(binding); |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 178 | } catch (ResolveFailedException e) { |
| 179 | injector.errorHandler.handle(source, e.getMessage()); |
limpbizkit | f44e9cc | 2008-03-26 06:33:29 +0000 | [diff] [blame] | 180 | } |
limpbizkit | f44e9cc | 2008-03-26 06:33:29 +0000 | [diff] [blame] | 181 | } |
| 182 | }); |
| 183 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 184 | return null; |
| 185 | } |
| 186 | }); |
| 187 | |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | private <T> void validateKey(Object source, Key<T> key) { |
| 192 | if (key.hasAnnotationType()) { |
| 193 | Class<? extends Annotation> annotationType = key.getAnnotationType(); |
| 194 | |
| 195 | if (!Annotations.isRetainedAtRuntime(annotationType)) { |
| 196 | addError(StackTraceElements.forType(annotationType), |
| 197 | ErrorMessages.MISSING_RUNTIME_RETENTION, source); |
| 198 | } |
| 199 | |
| 200 | if (!Key.isBindingAnnotation(annotationType)) { |
| 201 | addError(StackTraceElements.forType(annotationType), |
| 202 | ErrorMessages.MISSING_BINDING_ANNOTATION, source); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | <T> InvalidBindingImpl<T> invalidBinding(InjectorImpl injector, Key<T> key, Object source) { |
| 208 | return new InvalidBindingImpl<T>(injector, key, source); |
| 209 | } |
| 210 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 211 | @Override public Boolean visitBindConstant(BindConstantCommand command) { |
limpbizkit | d6967b9 | 2008-05-16 15:28:51 +0000 | [diff] [blame] | 212 | BindTarget<?> target = command.getTarget(); |
| 213 | if (target == null) { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 214 | addError(command.getSource(), ErrorMessages.MISSING_CONSTANT_VALUE); |
limpbizkit | d6967b9 | 2008-05-16 15:28:51 +0000 | [diff] [blame] | 215 | return true; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 216 | } |
| 217 | |
limpbizkit | d6967b9 | 2008-05-16 15:28:51 +0000 | [diff] [blame] | 218 | Object value = target.get(); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 219 | validateKey(command.getSource(), command.getKey()); |
| 220 | ConstantFactory<Object> factory = new ConstantFactory<Object>(value); |
limpbizkit | f44e9cc | 2008-03-26 06:33:29 +0000 | [diff] [blame] | 221 | putBinding(new ConstantBindingImpl<Object>( |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 222 | injector, command.getKey(), command.getSource(), factory, value)); |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | private <T> void createBinding(Object source, boolean shouldPreload, |
| 228 | BindingImpl<T> binding) { |
| 229 | putBinding(binding); |
| 230 | |
| 231 | // Register to preload if necessary. |
| 232 | if (binding.getScope() == Scopes.SINGLETON) { |
| 233 | if (stage == Stage.PRODUCTION || shouldPreload) { |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 234 | eagerSingletonCreators.add(new EagerSingletonCreator(binding.key, binding.internalFactory)); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 235 | } |
| 236 | } else { |
| 237 | if (shouldPreload) { |
| 238 | addError(source, ErrorMessages.PRELOAD_NOT_ALLOWED); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
limpbizkit | f44e9cc | 2008-03-26 06:33:29 +0000 | [diff] [blame] | 243 | public void createUntargettedBindings() { |
| 244 | for (Runnable untargettedBinding : untargettedBindings) { |
| 245 | untargettedBinding.run(); |
| 246 | } |
| 247 | } |
| 248 | |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 249 | public void createEagerSingletons(InjectorImpl injector) { |
| 250 | for (ContextualCallable<Void> preloader : eagerSingletonCreators) { |
| 251 | injector.callInContext(preloader); |
| 252 | } |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | public void runCreationListeners(InjectorImpl injector) { |
| 256 | for (CreationListener creationListener : creationListeners) { |
| 257 | creationListener.notify(injector); |
| 258 | } |
| 259 | } |
| 260 | |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 261 | private static class EagerSingletonCreator implements ContextualCallable<Void> { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 262 | private final Key<?> key; |
| 263 | private final InternalFactory<?> factory; |
| 264 | |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 265 | public EagerSingletonCreator(Key<?> key, InternalFactory<?> factory) { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 266 | this.key = key; |
| 267 | this.factory = Objects.nonNull(factory, "factory"); |
| 268 | } |
| 269 | |
| 270 | public Void call(InternalContext context) { |
| 271 | InjectionPoint<?> injectionPoint |
limpbizkit | 8b23745 | 2008-04-22 06:47:36 +0000 | [diff] [blame] | 272 | = InjectionPoint.newInstance(key, context.getInjector()); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 273 | context.setInjectionPoint(injectionPoint); |
| 274 | try { |
| 275 | factory.get(context, injectionPoint); |
| 276 | return null; |
| 277 | } catch(ProvisionException provisionException) { |
| 278 | provisionException.addContext(injectionPoint); |
| 279 | throw provisionException; |
| 280 | } finally { |
| 281 | context.setInjectionPoint(null); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | private void putBinding(BindingImpl<?> binding) { |
| 287 | Key<?> key = binding.getKey(); |
| 288 | Binding<?> original = bindings.get(key); |
| 289 | |
| 290 | Class<?> rawType = key.getRawType(); |
| 291 | if (FORBIDDEN_TYPES.contains(rawType)) { |
| 292 | addError(binding.getSource(), ErrorMessages.CANNOT_BIND_TO_GUICE_TYPE, |
| 293 | rawType.getSimpleName()); |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | if (bindings.containsKey(key)) { |
| 298 | addError(binding.getSource(), ErrorMessages.BINDING_ALREADY_SET, key, |
| 299 | original.getSource()); |
| 300 | } else { |
| 301 | bindings.put(key, binding); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | private static Set<Class<?>> FORBIDDEN_TYPES = forbiddenTypes(); |
| 306 | |
| 307 | @SuppressWarnings("unchecked") // For generic array creation. |
| 308 | private static Set<Class<?>> forbiddenTypes() { |
| 309 | Set<Class<?>> set = new HashSet<Class<?>>(); |
| 310 | |
| 311 | Collections.addAll(set, |
| 312 | |
| 313 | // It's unfortunate that we have to maintain a blacklist of specific |
| 314 | // classes, but we can't easily block the whole package because of |
| 315 | // all our unit tests. |
| 316 | |
| 317 | AbstractModule.class, |
| 318 | Binder.class, |
| 319 | Binding.class, |
| 320 | Key.class, |
| 321 | Module.class, |
| 322 | Provider.class, |
| 323 | Scope.class, |
| 324 | TypeLiteral.class); |
| 325 | return Collections.unmodifiableSet(set); |
| 326 | } |
| 327 | |
| 328 | interface CreationListener { |
| 329 | void notify(InjectorImpl injector); |
| 330 | } |
| 331 | } |