blob: 817fc1510c723da823d52895844ada452071b513 [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
19import java.lang.reflect.InvocationHandler;
crazyboblee66b415a2006-08-25 02:01:19 +000020import java.lang.reflect.InvocationTargetException;
kevinb9na99dca72007-02-11 04:48:57 +000021import java.lang.reflect.Method;
crazyboblee66b415a2006-08-25 02:01:19 +000022import java.lang.reflect.Proxy;
crazyboblee66b415a2006-08-25 02:01:19 +000023import java.util.ArrayList;
kevinb9na99dca72007-02-11 04:48:57 +000024import java.util.List;
crazyboblee66b415a2006-08-25 02:01:19 +000025
26/**
27 * Context of a dependency construction. Used to manage circular references.
28 *
29 * @author crazybob@google.com (Bob Lee)
30 */
31class ConstructionContext<T> {
32
33 T currentReference;
34 boolean constructing;
35
36 List<DelegatingInvocationHandler<T>> invocationHandlers;
37
38 T getCurrentReference() {
39 return currentReference;
40 }
41
42 void removeCurrentReference() {
43 this.currentReference = null;
44 }
45
46 void setCurrentReference(T currentReference) {
47 this.currentReference = currentReference;
48 }
49
50 boolean isConstructing() {
51 return constructing;
52 }
53
54 void startConstruction() {
55 this.constructing = true;
56 }
57
58 void finishConstruction() {
59 this.constructing = false;
60 invocationHandlers = null;
61 }
62
crazybobleeedda4362007-03-02 02:55:35 +000063 Object createProxy(Class<?> expectedType) {
crazyboblee66b415a2006-08-25 02:01:19 +000064 // TODO: if I create a proxy which implements all the interfaces of
65 // the implementation type, I'll be able to get away with one proxy
66 // instance (as opposed to one per caller).
67
68 if (!expectedType.isInterface()) {
crazybobleee3adfd62007-02-02 21:30:08 +000069 // TODO: Report better error.
crazyboblee0789b192007-02-13 02:43:28 +000070 throw new ConfigurationException("Tried proxying "
71 + expectedType.getName() + " to support a circular dependency, but"
72 + " it is not an interface.");
crazyboblee66b415a2006-08-25 02:01:19 +000073 }
74
75 if (invocationHandlers == null) {
76 invocationHandlers = new ArrayList<DelegatingInvocationHandler<T>>();
77 }
78
kevinb9na99dca72007-02-11 04:48:57 +000079 DelegatingInvocationHandler<T> invocationHandler
80 = new DelegatingInvocationHandler<T>();
crazyboblee66b415a2006-08-25 02:01:19 +000081 invocationHandlers.add(invocationHandler);
82
kevinb9n48d13072007-02-12 18:21:26 +000083 Object object = Proxy.newProxyInstance(expectedType.getClassLoader(),
kevinb9na99dca72007-02-11 04:48:57 +000084 new Class[] { expectedType }, invocationHandler);
kevinb9n48d13072007-02-12 18:21:26 +000085 return expectedType.cast(object);
crazyboblee66b415a2006-08-25 02:01:19 +000086 }
87
88 void setProxyDelegates(T delegate) {
89 if (invocationHandlers != null) {
kevinb9na99dca72007-02-11 04:48:57 +000090 for (DelegatingInvocationHandler<T> handler : invocationHandlers) {
91 handler.setDelegate(delegate);
crazyboblee66b415a2006-08-25 02:01:19 +000092 }
93 }
94 }
95
96 static class DelegatingInvocationHandler<T> implements InvocationHandler {
97
98 T delegate;
99
100 public Object invoke(Object proxy, Method method, Object[] args)
101 throws Throwable {
102 if (delegate == null) {
crazyboblee0789b192007-02-13 02:43:28 +0000103 throw new IllegalStateException("This is a proxy used to support"
104 + " circular references involving constructors. The object we're"
105 + " proxying is not constructed yet. Please wait until after"
106 + " injection has completed to use this object.");
crazyboblee66b415a2006-08-25 02:01:19 +0000107 }
108
109 try {
kevinb9n225310e2007-02-20 04:12:01 +0000110 // This appears to be not test-covered
crazyboblee66b415a2006-08-25 02:01:19 +0000111 return method.invoke(delegate, args);
kevinb9na99dca72007-02-11 04:48:57 +0000112 }
113 catch (IllegalAccessException e) {
crazyboblee66b415a2006-08-25 02:01:19 +0000114 throw new RuntimeException(e);
kevinb9na99dca72007-02-11 04:48:57 +0000115 }
116 catch (IllegalArgumentException e) {
crazyboblee66b415a2006-08-25 02:01:19 +0000117 throw new RuntimeException(e);
kevinb9na99dca72007-02-11 04:48:57 +0000118 }
119 catch (InvocationTargetException e) {
crazyboblee66b415a2006-08-25 02:01:19 +0000120 throw e.getTargetException();
121 }
122 }
123
124 void setDelegate(T delegate) {
125 this.delegate = delegate;
126 }
127 }
128}