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 | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 20 | import com.google.inject.internal.ResolveFailedException; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 21 | import com.google.inject.internal.ToStringBuilder; |
| 22 | |
| 23 | /** |
| 24 | * A placeholder which enables us to swap in the real factory once the |
| 25 | * container is created. |
| 26 | */ |
| 27 | class FactoryProxy<T> implements InternalFactory<T>, |
| 28 | BindCommandProcessor.CreationListener { |
| 29 | |
| 30 | private final Key<T> key; |
| 31 | private final Key<? extends T> targetKey; |
| 32 | private final Object source; |
| 33 | |
limpbizkit | e4647a6 | 2008-05-25 18:03:35 +0000 | [diff] [blame] | 34 | private InternalFactory<? extends T> targetFactory; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 35 | |
| 36 | FactoryProxy(Key<T> key, Key<? extends T> targetKey, Object source) { |
| 37 | this.key = key; |
| 38 | this.targetKey = targetKey; |
| 39 | this.source = source; |
| 40 | } |
| 41 | |
| 42 | public void notify(final InjectorImpl injector) { |
| 43 | injector.withDefaultSource(source, new Runnable() { |
| 44 | public void run() { |
limpbizkit | 3b1cd58 | 2008-04-28 00:06:01 +0000 | [diff] [blame] | 45 | try { |
| 46 | targetFactory = injector.getInternalFactory(targetKey); |
| 47 | } catch (ResolveFailedException e) { |
| 48 | injector.errorHandler.handle(source, e.getMessage()); |
| 49 | } |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 50 | } |
| 51 | }); |
| 52 | } |
| 53 | |
| 54 | public T get(InternalContext context, InjectionPoint<?> injectionPoint) { |
| 55 | return targetFactory.get(context, injectionPoint); |
| 56 | } |
| 57 | |
| 58 | public String toString() { |
| 59 | return new ToStringBuilder(FactoryProxy.class) |
| 60 | .add("key", key) |
| 61 | .add("provider", targetFactory) |
| 62 | .toString(); |
| 63 | } |
| 64 | } |