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 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame] | 17 | package com.google.inject.internal; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 18 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame] | 19 | import com.google.inject.Key; |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 20 | import com.google.inject.internal.InjectorImpl.JitLimitation; |
limpbizkit@gmail.com | 9a227be | 2010-07-03 15:51:31 +0000 | [diff] [blame] | 21 | import com.google.inject.internal.util.ToStringBuilder; |
limpbizkit | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 22 | import com.google.inject.spi.Dependency; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 23 | |
| 24 | /** |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 25 | * A placeholder which enables us to swap in the real factory once the injector is created. |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 26 | * Used for a linked binding, so that getting the linked binding returns the link's factory. |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 27 | */ |
sberlin | 07170cc | 2011-03-11 03:02:39 +0000 | [diff] [blame] | 28 | final class FactoryProxy<T> implements InternalFactory<T>, CreationListener { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 29 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 30 | private final InjectorImpl injector; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 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 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 37 | FactoryProxy(InjectorImpl injector, Key<T> key, Key<? extends T> targetKey, Object source) { |
| 38 | this.injector = injector; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 39 | this.key = key; |
| 40 | this.targetKey = targetKey; |
| 41 | this.source = source; |
| 42 | } |
| 43 | |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 44 | public void notify(final Errors errors) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 45 | try { |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 46 | targetFactory = injector.getInternalFactory(targetKey, errors.withSource(source), JitLimitation.NEW_OR_EXISTING_JIT); |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 47 | } catch (ErrorsException e) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 48 | errors.merge(e.getErrors()); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 49 | } |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 50 | } |
| 51 | |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame] | 52 | public T get(Errors errors, InternalContext context, Dependency<?> dependency, boolean linked) |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 53 | throws ErrorsException { |
sberlin | ba75f35 | 2011-06-12 21:54:43 +0000 | [diff] [blame] | 54 | context.pushState(targetKey, source); |
| 55 | try { |
| 56 | return targetFactory.get(errors.withSource(targetKey), context, dependency, true); |
| 57 | } finally { |
| 58 | context.popState(); |
| 59 | } |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 60 | } |
| 61 | |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 62 | @Override public String toString() { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 63 | return new ToStringBuilder(FactoryProxy.class) |
| 64 | .add("key", key) |
| 65 | .add("provider", targetFactory) |
| 66 | .toString(); |
| 67 | } |
| 68 | } |