blob: 0ce62d92c79b78c3e15e8e377d88ca847d2d7ca3 [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
limpbizkitdf98fcd2008-06-14 05:02:15 +000019import com.google.inject.internal.ErrorMessage;
20import com.google.inject.spi.Message;
crazyboblee66b415a2006-08-25 02:01:19 +000021import java.lang.reflect.InvocationHandler;
crazyboblee66b415a2006-08-25 02:01:19 +000022import java.lang.reflect.InvocationTargetException;
kevinb9na99dca72007-02-11 04:48:57 +000023import java.lang.reflect.Method;
crazyboblee66b415a2006-08-25 02:01:19 +000024import java.lang.reflect.Proxy;
crazyboblee66b415a2006-08-25 02:01:19 +000025import java.util.ArrayList;
kevinb9na99dca72007-02-11 04:48:57 +000026import java.util.List;
crazyboblee66b415a2006-08-25 02:01:19 +000027
28/**
29 * Context of a dependency construction. Used to manage circular references.
30 *
31 * @author crazybob@google.com (Bob Lee)
32 */
33class ConstructionContext<T> {
34
35 T currentReference;
36 boolean constructing;
37
38 List<DelegatingInvocationHandler<T>> invocationHandlers;
39
40 T getCurrentReference() {
41 return currentReference;
42 }
43
44 void removeCurrentReference() {
45 this.currentReference = null;
46 }
47
48 void setCurrentReference(T currentReference) {
49 this.currentReference = currentReference;
50 }
51
52 boolean isConstructing() {
53 return constructing;
54 }
55
56 void startConstruction() {
57 this.constructing = true;
58 }
59
60 void finishConstruction() {
61 this.constructing = false;
62 invocationHandlers = null;
63 }
64
crazybobleeedda4362007-03-02 02:55:35 +000065 Object createProxy(Class<?> expectedType) {
crazyboblee66b415a2006-08-25 02:01:19 +000066 // TODO: if I create a proxy which implements all the interfaces of
67 // the implementation type, I'll be able to get away with one proxy
68 // instance (as opposed to one per caller).
69
70 if (!expectedType.isInterface()) {
crazybobleee3adfd62007-02-02 21:30:08 +000071 // TODO: Report better error.
limpbizkitdf98fcd2008-06-14 05:02:15 +000072 throw new CreationException(
73 new Message(ErrorMessage.cannotSatisfyCircularDependency(expectedType).toString()));
crazyboblee66b415a2006-08-25 02:01:19 +000074 }
75
76 if (invocationHandlers == null) {
77 invocationHandlers = new ArrayList<DelegatingInvocationHandler<T>>();
78 }
79
kevinb9na99dca72007-02-11 04:48:57 +000080 DelegatingInvocationHandler<T> invocationHandler
81 = new DelegatingInvocationHandler<T>();
crazyboblee66b415a2006-08-25 02:01:19 +000082 invocationHandlers.add(invocationHandler);
83
kevinb9n48d13072007-02-12 18:21:26 +000084 Object object = Proxy.newProxyInstance(expectedType.getClassLoader(),
kevinb9na99dca72007-02-11 04:48:57 +000085 new Class[] { expectedType }, invocationHandler);
kevinb9n48d13072007-02-12 18:21:26 +000086 return expectedType.cast(object);
crazyboblee66b415a2006-08-25 02:01:19 +000087 }
88
89 void setProxyDelegates(T delegate) {
90 if (invocationHandlers != null) {
kevinb9na99dca72007-02-11 04:48:57 +000091 for (DelegatingInvocationHandler<T> handler : invocationHandlers) {
92 handler.setDelegate(delegate);
crazyboblee66b415a2006-08-25 02:01:19 +000093 }
94 }
95 }
96
97 static class DelegatingInvocationHandler<T> implements InvocationHandler {
98
99 T delegate;
100
101 public Object invoke(Object proxy, Method method, Object[] args)
102 throws Throwable {
103 if (delegate == null) {
crazyboblee0789b192007-02-13 02:43:28 +0000104 throw new IllegalStateException("This is a proxy used to support"
105 + " circular references involving constructors. The object we're"
106 + " proxying is not constructed yet. Please wait until after"
107 + " injection has completed to use this object.");
crazyboblee66b415a2006-08-25 02:01:19 +0000108 }
109
110 try {
kevinb9n225310e2007-02-20 04:12:01 +0000111 // This appears to be not test-covered
crazyboblee66b415a2006-08-25 02:01:19 +0000112 return method.invoke(delegate, args);
kevinb9na99dca72007-02-11 04:48:57 +0000113 }
114 catch (IllegalAccessException e) {
crazyboblee66b415a2006-08-25 02:01:19 +0000115 throw new RuntimeException(e);
kevinb9na99dca72007-02-11 04:48:57 +0000116 }
117 catch (IllegalArgumentException e) {
crazyboblee66b415a2006-08-25 02:01:19 +0000118 throw new RuntimeException(e);
kevinb9na99dca72007-02-11 04:48:57 +0000119 }
120 catch (InvocationTargetException e) {
crazyboblee66b415a2006-08-25 02:01:19 +0000121 throw e.getTargetException();
122 }
123 }
124
125 void setDelegate(T delegate) {
126 this.delegate = delegate;
127 }
128 }
129}