blob: 6764fb4df521155cea09d2524163f131e2f9ddbe [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
limpbizkit76c24b12008-12-25 04:32:41 +000017package com.google.inject.internal;
crazyboblee66b415a2006-08-25 02:01:19 +000018
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 */
limpbizkit76c24b12008-12-25 04:32:41 +000031public class ConstructionContext<T> {
crazyboblee66b415a2006-08-25 02:01:19 +000032
33 T currentReference;
34 boolean constructing;
35
36 List<DelegatingInvocationHandler<T>> invocationHandlers;
37
limpbizkit76c24b12008-12-25 04:32:41 +000038 public T getCurrentReference() {
crazyboblee66b415a2006-08-25 02:01:19 +000039 return currentReference;
40 }
41
limpbizkit76c24b12008-12-25 04:32:41 +000042 public void removeCurrentReference() {
crazyboblee66b415a2006-08-25 02:01:19 +000043 this.currentReference = null;
44 }
45
limpbizkit76c24b12008-12-25 04:32:41 +000046 public void setCurrentReference(T currentReference) {
crazyboblee66b415a2006-08-25 02:01:19 +000047 this.currentReference = currentReference;
48 }
49
limpbizkit76c24b12008-12-25 04:32:41 +000050 public boolean isConstructing() {
crazyboblee66b415a2006-08-25 02:01:19 +000051 return constructing;
52 }
53
limpbizkit76c24b12008-12-25 04:32:41 +000054 public void startConstruction() {
crazyboblee66b415a2006-08-25 02:01:19 +000055 this.constructing = true;
56 }
57
limpbizkit76c24b12008-12-25 04:32:41 +000058 public void finishConstruction() {
crazyboblee66b415a2006-08-25 02:01:19 +000059 this.constructing = false;
60 invocationHandlers = null;
61 }
62
limpbizkit76c24b12008-12-25 04:32:41 +000063 public Object createProxy(Errors errors, Class<?> expectedType) throws ErrorsException {
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()) {
limpbizkit9dc32d42008-06-15 11:29:10 +000069 throw errors.cannotSatisfyCircularDependency(expectedType).toException();
crazyboblee66b415a2006-08-25 02:01:19 +000070 }
71
72 if (invocationHandlers == null) {
73 invocationHandlers = new ArrayList<DelegatingInvocationHandler<T>>();
74 }
75
kevinb9na99dca72007-02-11 04:48:57 +000076 DelegatingInvocationHandler<T> invocationHandler
77 = new DelegatingInvocationHandler<T>();
crazyboblee66b415a2006-08-25 02:01:19 +000078 invocationHandlers.add(invocationHandler);
79
limpbizkit869a3c02008-06-27 01:09:41 +000080 ClassLoader classLoader = BytecodeGen.getClassLoader(expectedType);
81 return expectedType.cast(Proxy.newProxyInstance(classLoader,
82 new Class[] { expectedType }, invocationHandler));
crazyboblee66b415a2006-08-25 02:01:19 +000083 }
84
limpbizkit76c24b12008-12-25 04:32:41 +000085 public void setProxyDelegates(T delegate) {
crazyboblee66b415a2006-08-25 02:01:19 +000086 if (invocationHandlers != null) {
kevinb9na99dca72007-02-11 04:48:57 +000087 for (DelegatingInvocationHandler<T> handler : invocationHandlers) {
88 handler.setDelegate(delegate);
crazyboblee66b415a2006-08-25 02:01:19 +000089 }
90 }
91 }
92
93 static class DelegatingInvocationHandler<T> implements InvocationHandler {
94
95 T delegate;
96
97 public Object invoke(Object proxy, Method method, Object[] args)
98 throws Throwable {
99 if (delegate == null) {
crazyboblee0789b192007-02-13 02:43:28 +0000100 throw new IllegalStateException("This is a proxy used to support"
101 + " circular references involving constructors. The object we're"
102 + " proxying is not constructed yet. Please wait until after"
103 + " injection has completed to use this object.");
crazyboblee66b415a2006-08-25 02:01:19 +0000104 }
105
106 try {
kevinb9n225310e2007-02-20 04:12:01 +0000107 // This appears to be not test-covered
crazyboblee66b415a2006-08-25 02:01:19 +0000108 return method.invoke(delegate, args);
kevinb9na99dca72007-02-11 04:48:57 +0000109 }
110 catch (IllegalAccessException e) {
crazyboblee66b415a2006-08-25 02:01:19 +0000111 throw new RuntimeException(e);
kevinb9na99dca72007-02-11 04:48:57 +0000112 }
113 catch (IllegalArgumentException e) {
crazyboblee66b415a2006-08-25 02:01:19 +0000114 throw new RuntimeException(e);
kevinb9na99dca72007-02-11 04:48:57 +0000115 }
116 catch (InvocationTargetException e) {
crazyboblee66b415a2006-08-25 02:01:19 +0000117 throw e.getTargetException();
118 }
119 }
120
121 void setDelegate(T delegate) {
122 this.delegate = delegate;
123 }
124 }
125}