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 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 17 | package com.google.inject.internal; |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 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 | */ |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 31 | public class ConstructionContext<T> { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 32 | |
| 33 | T currentReference; |
| 34 | boolean constructing; |
| 35 | |
| 36 | List<DelegatingInvocationHandler<T>> invocationHandlers; |
| 37 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 38 | public T getCurrentReference() { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 39 | return currentReference; |
| 40 | } |
| 41 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 42 | public void removeCurrentReference() { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 43 | this.currentReference = null; |
| 44 | } |
| 45 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 46 | public void setCurrentReference(T currentReference) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 47 | this.currentReference = currentReference; |
| 48 | } |
| 49 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 50 | public boolean isConstructing() { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 51 | return constructing; |
| 52 | } |
| 53 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 54 | public void startConstruction() { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 55 | this.constructing = true; |
| 56 | } |
| 57 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 58 | public void finishConstruction() { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 59 | this.constructing = false; |
| 60 | invocationHandlers = null; |
| 61 | } |
| 62 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 63 | public Object createProxy(Errors errors, Class<?> expectedType) throws ErrorsException { |
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()) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 69 | throw errors.cannotSatisfyCircularDependency(expectedType).toException(); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | if (invocationHandlers == null) { |
| 73 | invocationHandlers = new ArrayList<DelegatingInvocationHandler<T>>(); |
| 74 | } |
| 75 | |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 76 | DelegatingInvocationHandler<T> invocationHandler |
| 77 | = new DelegatingInvocationHandler<T>(); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 78 | invocationHandlers.add(invocationHandler); |
| 79 | |
limpbizkit | 869a3c0 | 2008-06-27 01:09:41 +0000 | [diff] [blame] | 80 | ClassLoader classLoader = BytecodeGen.getClassLoader(expectedType); |
| 81 | return expectedType.cast(Proxy.newProxyInstance(classLoader, |
| 82 | new Class[] { expectedType }, invocationHandler)); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 83 | } |
| 84 | |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 85 | public void setProxyDelegates(T delegate) { |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 86 | if (invocationHandlers != null) { |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 87 | for (DelegatingInvocationHandler<T> handler : invocationHandlers) { |
| 88 | handler.setDelegate(delegate); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 89 | } |
| 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) { |
crazyboblee | 0789b19 | 2007-02-13 02:43:28 +0000 | [diff] [blame] | 100 | 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."); |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | try { |
kevinb9n | 225310e | 2007-02-20 04:12:01 +0000 | [diff] [blame] | 107 | // This appears to be not test-covered |
crazyboblee | 66b415a | 2006-08-25 02:01:19 +0000 | [diff] [blame] | 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 | } |