crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2006 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 | package com.google.inject; |
| 18 | |
crazyboblee | b052dd8 | 2007-01-25 22:33:56 +0000 | [diff] [blame] | 19 | import java.util.HashMap; |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 20 | import java.util.Map; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * Internal context. Used to coordinate injections and support circular |
| 24 | * dependencies. |
| 25 | * |
| 26 | * @author crazybob@google.com (Bob Lee) |
| 27 | */ |
| 28 | class InternalContext { |
| 29 | |
limpbizkit | 8b23745 | 2008-04-22 06:47:36 +0000 | [diff] [blame] | 30 | private final InjectorImpl injector; |
| 31 | private Map<Object, ConstructionContext<?>> constructionContexts; |
| 32 | private InjectionPoint injectionPoint; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 33 | |
limpbizkit | 8b23745 | 2008-04-22 06:47:36 +0000 | [diff] [blame] | 34 | public InternalContext(InjectorImpl injector) { |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 35 | this.injector = injector; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 36 | } |
| 37 | |
limpbizkit | 8b23745 | 2008-04-22 06:47:36 +0000 | [diff] [blame] | 38 | public InjectorImpl getInjector() { |
kevinb9n | a2915a9 | 2007-02-28 06:20:30 +0000 | [diff] [blame] | 39 | return injector; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 40 | } |
| 41 | |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 42 | @SuppressWarnings("unchecked") |
limpbizkit | 8b23745 | 2008-04-22 06:47:36 +0000 | [diff] [blame] | 43 | public <T> ConstructionContext<T> getConstructionContext(Object key) { |
crazyboblee | b052dd8 | 2007-01-25 22:33:56 +0000 | [diff] [blame] | 44 | if (constructionContexts == null) { |
| 45 | constructionContexts = new HashMap<Object, ConstructionContext<?>>(); |
| 46 | ConstructionContext<T> constructionContext = new ConstructionContext<T>(); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 47 | constructionContexts.put(key, constructionContext); |
crazyboblee | b052dd8 | 2007-01-25 22:33:56 +0000 | [diff] [blame] | 48 | return constructionContext; |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 49 | } |
| 50 | else { |
| 51 | ConstructionContext<T> constructionContext |
| 52 | = (ConstructionContext<T>) constructionContexts.get(key); |
crazyboblee | b052dd8 | 2007-01-25 22:33:56 +0000 | [diff] [blame] | 53 | if (constructionContext == null) { |
| 54 | constructionContext = new ConstructionContext<T>(); |
| 55 | constructionContexts.put(key, constructionContext); |
| 56 | } |
| 57 | return constructionContext; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 58 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 59 | } |
| 60 | |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 61 | public InjectionPoint getInjectionPoint() { |
| 62 | return injectionPoint; |
limpbizkit | b946ca2 | 2007-08-25 15:56:50 +0000 | [diff] [blame] | 63 | } |
| 64 | |
limpbizkit | fcf2b8c | 2007-10-21 18:23:43 +0000 | [diff] [blame] | 65 | public void setInjectionPoint(InjectionPoint injectionPoint) { |
| 66 | this.injectionPoint = injectionPoint; |
limpbizkit | c808df0 | 2007-08-25 03:25:13 +0000 | [diff] [blame] | 67 | } |
limpbizkit | 51515b5 | 2008-03-11 03:46:25 +0000 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Ensures that an object requiring injection at Injector-creation time has |
| 71 | * been injected before its use. |
| 72 | */ |
| 73 | public void ensureMemberInjected(Object toInject) { |
| 74 | if (!injector.outstandingInjections.keySet().remove(toInject)) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | injector.injectMembers(toInject); |
| 79 | } |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 80 | } |