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 | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame^] | 23 | import com.google.inject.spi.InjectionPoint; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * A placeholder which enables us to swap in the real factory once the |
| 27 | * container is created. |
| 28 | */ |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 29 | class FactoryProxy<T> implements InternalFactory<T>, BindCommandProcessor.CreationListener { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 30 | |
| 31 | private final Key<T> key; |
| 32 | private final Key<? extends T> targetKey; |
| 33 | private final Object source; |
| 34 | |
limpbizkit | e4647a6 | 2008-05-25 18:03:35 +0000 | [diff] [blame] | 35 | private InternalFactory<? extends T> targetFactory; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 36 | |
| 37 | FactoryProxy(Key<T> key, Key<? extends T> targetKey, Object source) { |
| 38 | this.key = key; |
| 39 | this.targetKey = targetKey; |
| 40 | this.source = source; |
| 41 | } |
| 42 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 43 | public void notify(final InjectorImpl injector, final Errors errors) { |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame^] | 44 | errors.pushSource(source); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 45 | try { |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame^] | 46 | targetFactory = injector.getInternalFactory(targetKey, errors); |
| 47 | } catch (ErrorsException e) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 48 | errors.merge(e.getErrors()); |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame^] | 49 | } finally { |
| 50 | errors.popSource(source); |
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 | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 54 | public T get(Errors errors, InternalContext context, InjectionPoint<?> injectionPoint) |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame^] | 55 | throws ErrorsException { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 56 | return targetFactory.get(errors, context, injectionPoint); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | public String toString() { |
| 60 | return new ToStringBuilder(FactoryProxy.class) |
| 61 | .add("key", key) |
| 62 | .add("provider", targetFactory) |
| 63 | .toString(); |
| 64 | } |
| 65 | } |