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 | a98bc7a | 2008-08-29 16:52:44 +0000 | [diff] [blame] | 21 | import com.google.inject.spi.Dependency; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 22 | |
| 23 | /** |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 24 | * 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^] | 25 | * 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] | 26 | */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame] | 27 | final class FactoryProxy<T> implements InternalFactory<T>, BindingProcessor.CreationListener { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 28 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 29 | private final InjectorImpl injector; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 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 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 36 | FactoryProxy(InjectorImpl injector, Key<T> key, Key<? extends T> targetKey, Object source) { |
| 37 | this.injector = injector; |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 38 | this.key = key; |
| 39 | this.targetKey = targetKey; |
| 40 | this.source = source; |
| 41 | } |
| 42 | |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 43 | public void notify(final Errors errors) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 44 | try { |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame^] | 45 | targetFactory = injector.getInternalFactory(targetKey, errors.withSource(source), JitLimitation.NEW_OR_EXISTING_JIT); |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 46 | } catch (ErrorsException e) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 47 | errors.merge(e.getErrors()); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 48 | } |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 49 | } |
| 50 | |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame^] | 51 | public T get(Errors errors, InternalContext context, Dependency<?> dependency, boolean linked) |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 52 | throws ErrorsException { |
sberlin | 888a264 | 2010-02-11 22:07:07 +0000 | [diff] [blame^] | 53 | return targetFactory.get(errors.withSource(targetKey), context, dependency, true); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 54 | } |
| 55 | |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 56 | @Override public String toString() { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 57 | return new ToStringBuilder(FactoryProxy.class) |
| 58 | .add("key", key) |
| 59 | .add("provider", targetFactory) |
| 60 | .toString(); |
| 61 | } |
| 62 | } |