blob: 3e035e4b65741354de79c3aa1103a02c5fc734d5 [file] [log] [blame]
crazyboblee66b415a2006-08-25 02:01:19 +00001/**
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
17package com.google.inject;
18
crazybobleeb052dd82007-01-25 22:33:56 +000019import java.util.HashMap;
kevinb9na99dca72007-02-11 04:48:57 +000020import java.util.Map;
crazyboblee66b415a2006-08-25 02:01:19 +000021
22/**
23 * Internal context. Used to coordinate injections and support circular
24 * dependencies.
25 *
26 * @author crazybob@google.com (Bob Lee)
27 */
28class InternalContext {
29
limpbizkit8b237452008-04-22 06:47:36 +000030 private final InjectorImpl injector;
31 private Map<Object, ConstructionContext<?>> constructionContexts;
32 private InjectionPoint injectionPoint;
crazyboblee66b415a2006-08-25 02:01:19 +000033
limpbizkit8b237452008-04-22 06:47:36 +000034 public InternalContext(InjectorImpl injector) {
kevinb9na2915a92007-02-28 06:20:30 +000035 this.injector = injector;
crazyboblee66b415a2006-08-25 02:01:19 +000036 }
37
limpbizkit8b237452008-04-22 06:47:36 +000038 public InjectorImpl getInjector() {
kevinb9na2915a92007-02-28 06:20:30 +000039 return injector;
crazyboblee66b415a2006-08-25 02:01:19 +000040 }
41
crazyboblee66b415a2006-08-25 02:01:19 +000042 @SuppressWarnings("unchecked")
limpbizkit8b237452008-04-22 06:47:36 +000043 public <T> ConstructionContext<T> getConstructionContext(Object key) {
crazybobleeb052dd82007-01-25 22:33:56 +000044 if (constructionContexts == null) {
45 constructionContexts = new HashMap<Object, ConstructionContext<?>>();
46 ConstructionContext<T> constructionContext = new ConstructionContext<T>();
crazyboblee66b415a2006-08-25 02:01:19 +000047 constructionContexts.put(key, constructionContext);
crazybobleeb052dd82007-01-25 22:33:56 +000048 return constructionContext;
kevinb9na99dca72007-02-11 04:48:57 +000049 }
50 else {
51 ConstructionContext<T> constructionContext
52 = (ConstructionContext<T>) constructionContexts.get(key);
crazybobleeb052dd82007-01-25 22:33:56 +000053 if (constructionContext == null) {
54 constructionContext = new ConstructionContext<T>();
55 constructionContexts.put(key, constructionContext);
56 }
57 return constructionContext;
crazyboblee66b415a2006-08-25 02:01:19 +000058 }
crazyboblee66b415a2006-08-25 02:01:19 +000059 }
60
limpbizkitfcf2b8c2007-10-21 18:23:43 +000061 public InjectionPoint getInjectionPoint() {
62 return injectionPoint;
limpbizkitb946ca22007-08-25 15:56:50 +000063 }
64
limpbizkitfcf2b8c2007-10-21 18:23:43 +000065 public void setInjectionPoint(InjectionPoint injectionPoint) {
66 this.injectionPoint = injectionPoint;
limpbizkitc808df02007-08-25 03:25:13 +000067 }
limpbizkit51515b52008-03-11 03:46:25 +000068
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 }
crazyboblee66b415a2006-08-25 02:01:19 +000080}