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 | |
| 19 | import java.lang.reflect.InvocationHandler; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 20 | import java.lang.reflect.InvocationTargetException; |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 21 | import java.lang.reflect.Method; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 22 | import java.lang.reflect.Proxy; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 23 | import java.util.ArrayList; |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 24 | import java.util.List; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Context of a dependency construction. Used to manage circular references. |
| 28 | * |
| 29 | * @author crazybob@google.com (Bob Lee) |
| 30 | */ |
| 31 | class 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 | |
kevinb9n | 48d1307 | 2007-02-12 18:21:26 +0000 | [diff] [blame^] | 63 | T createProxy(Class<T> expectedType) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 64 | // 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()) { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 69 | // TODO: Report better error. |
crazyboblee | 63b592b | 2007-01-25 02:45:24 +0000 | [diff] [blame] | 70 | throw new ConfigurationException( |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 71 | expectedType.getName() + " is not an interface."); |
| 72 | } |
| 73 | |
| 74 | if (invocationHandlers == null) { |
| 75 | invocationHandlers = new ArrayList<DelegatingInvocationHandler<T>>(); |
| 76 | } |
| 77 | |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 78 | DelegatingInvocationHandler<T> invocationHandler |
| 79 | = new DelegatingInvocationHandler<T>(); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 80 | invocationHandlers.add(invocationHandler); |
| 81 | |
kevinb9n | 48d1307 | 2007-02-12 18:21:26 +0000 | [diff] [blame^] | 82 | Object object = Proxy.newProxyInstance(expectedType.getClassLoader(), |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 83 | new Class[] { expectedType }, invocationHandler); |
kevinb9n | 48d1307 | 2007-02-12 18:21:26 +0000 | [diff] [blame^] | 84 | return expectedType.cast(object); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void setProxyDelegates(T delegate) { |
| 88 | if (invocationHandlers != null) { |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 89 | for (DelegatingInvocationHandler<T> handler : invocationHandlers) { |
| 90 | handler.setDelegate(delegate); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static class DelegatingInvocationHandler<T> implements InvocationHandler { |
| 96 | |
| 97 | T delegate; |
| 98 | |
| 99 | public Object invoke(Object proxy, Method method, Object[] args) |
| 100 | throws Throwable { |
| 101 | if (delegate == null) { |
| 102 | throw new IllegalStateException( |
| 103 | "Not finished constructing. Please don't call methods on this" |
| 104 | + " object until the caller's construction is complete."); |
| 105 | } |
| 106 | |
| 107 | try { |
| 108 | return method.invoke(delegate, args); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 109 | } |
| 110 | catch (IllegalAccessException e) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 111 | throw new RuntimeException(e); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 112 | } |
| 113 | catch (IllegalArgumentException e) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 114 | throw new RuntimeException(e); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 115 | } |
| 116 | catch (InvocationTargetException e) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 117 | throw e.getTargetException(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void setDelegate(T delegate) { |
| 122 | this.delegate = delegate; |
| 123 | } |
| 124 | } |
| 125 | } |