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 | |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 19 | import com.google.inject.internal.ErrorMessage; |
| 20 | import com.google.inject.spi.Message; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 21 | import java.lang.reflect.InvocationHandler; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 22 | import java.lang.reflect.InvocationTargetException; |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 23 | import java.lang.reflect.Method; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 24 | import java.lang.reflect.Proxy; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 25 | import java.util.ArrayList; |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 26 | import java.util.List; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Context of a dependency construction. Used to manage circular references. |
| 30 | * |
| 31 | * @author crazybob@google.com (Bob Lee) |
| 32 | */ |
| 33 | class 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 | |
crazyboblee | edda436 | 2007-03-02 02:55:35 +0000 | [diff] [blame] | 65 | Object createProxy(Class<?> expectedType) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 66 | // 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()) { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 71 | // TODO: Report better error. |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame^] | 72 | throw new CreationException( |
| 73 | new Message(ErrorMessage.cannotSatisfyCircularDependency(expectedType).toString())); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | if (invocationHandlers == null) { |
| 77 | invocationHandlers = new ArrayList<DelegatingInvocationHandler<T>>(); |
| 78 | } |
| 79 | |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 80 | DelegatingInvocationHandler<T> invocationHandler |
| 81 | = new DelegatingInvocationHandler<T>(); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 82 | invocationHandlers.add(invocationHandler); |
| 83 | |
kevinb9n | 48d1307 | 2007-02-12 18:21:26 +0000 | [diff] [blame] | 84 | Object object = Proxy.newProxyInstance(expectedType.getClassLoader(), |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 85 | new Class[] { expectedType }, invocationHandler); |
kevinb9n | 48d1307 | 2007-02-12 18:21:26 +0000 | [diff] [blame] | 86 | return expectedType.cast(object); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void setProxyDelegates(T delegate) { |
| 90 | if (invocationHandlers != null) { |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 91 | for (DelegatingInvocationHandler<T> handler : invocationHandlers) { |
| 92 | handler.setDelegate(delegate); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 93 | } |
| 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) { |
crazyboblee | 0789b19 | 2007-02-13 02:43:28 +0000 | [diff] [blame] | 104 | 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."); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | try { |
kevinb9n | 225310e | 2007-02-20 04:12:01 +0000 | [diff] [blame] | 111 | // This appears to be not test-covered |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 112 | return method.invoke(delegate, args); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 113 | } |
| 114 | catch (IllegalAccessException e) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 115 | throw new RuntimeException(e); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 116 | } |
| 117 | catch (IllegalArgumentException e) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 118 | throw new RuntimeException(e); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 119 | } |
| 120 | catch (InvocationTargetException e) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 121 | throw e.getTargetException(); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void setDelegate(T delegate) { |
| 126 | this.delegate = delegate; |
| 127 | } |
| 128 | } |
| 129 | } |