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 | |
| 18 | package com.google.inject; |
| 19 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 20 | import com.google.inject.internal.Errors; |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 21 | import com.google.inject.internal.ErrorsException; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 22 | import com.google.inject.internal.ToStringBuilder; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 23 | import com.google.inject.spi.Dependency; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame^] | 24 | import com.google.inject.spi.PrivateEnvironment; |
| 25 | import java.util.Map; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 26 | |
| 27 | /** |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame^] | 28 | * A placeholder which enables us to swap in the real factory once the injector is created. |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 29 | */ |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 30 | class FactoryProxy<T> implements InternalFactory<T>, BindingProcessor.CreationListener { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 31 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame^] | 32 | private final InjectorImpl injector; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 33 | private final Key<T> key; |
| 34 | private final Key<? extends T> targetKey; |
| 35 | private final Object source; |
| 36 | |
limpbizkit | e4647a6 | 2008-05-25 18:03:35 +0000 | [diff] [blame] | 37 | private InternalFactory<? extends T> targetFactory; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 38 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame^] | 39 | FactoryProxy(InjectorImpl injector, Key<T> key, Key<? extends T> targetKey, Object source) { |
| 40 | this.injector = injector; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 41 | this.key = key; |
| 42 | this.targetKey = targetKey; |
| 43 | this.source = source; |
| 44 | } |
| 45 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame^] | 46 | public void notify(Map<PrivateEnvironment, InjectorImpl> privateInjectors, final Errors errors) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 47 | try { |
limpbizkit | 2c2c610 | 2008-06-20 13:34:36 +0000 | [diff] [blame] | 48 | targetFactory = injector.getInternalFactory(targetKey, errors.withSource(source)); |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 49 | } catch (ErrorsException e) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 50 | errors.merge(e.getErrors()); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 51 | } |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 52 | } |
| 53 | |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 54 | public T get(Errors errors, InternalContext context, Dependency<?> dependency) |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 55 | throws ErrorsException { |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 56 | return targetFactory.get(errors.withSource(targetKey), context, dependency); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 57 | } |
| 58 | |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 59 | @Override public String toString() { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 60 | return new ToStringBuilder(FactoryProxy.class) |
| 61 | .add("key", key) |
| 62 | .add("provider", targetFactory) |
| 63 | .toString(); |
| 64 | } |
| 65 | } |