crazyboblee | b8cf1e5 | 2007-02-02 21:48:16 +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 | */ |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 16 | |
| 17 | package com.google.inject; |
| 18 | |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 19 | import java.lang.reflect.Constructor; |
| 20 | import java.lang.reflect.InvocationTargetException; |
crazyboblee | 9a3861b | 2007-02-20 03:37:31 +0000 | [diff] [blame] | 21 | import com.google.inject.util.StackTraceElements; |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Injects constructors. |
| 25 | * |
| 26 | * @author crazybob@google.com (Bob Lee) |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 27 | */ |
crazyboblee | 0789b19 | 2007-02-13 02:43:28 +0000 | [diff] [blame] | 28 | class ConstructorInjector<T> { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 29 | |
| 30 | final Class<T> implementation; |
| 31 | final ContainerImpl.Injector[] injectors; |
| 32 | final ContainerImpl.ParameterInjector<?>[] parameterInjectors; |
| 33 | final ConstructionProxy<T> constructionProxy; |
| 34 | |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 35 | ConstructorInjector(ContainerImpl container, Class<T> implementation) { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 36 | this.implementation = implementation; |
crazyboblee | 0789b19 | 2007-02-13 02:43:28 +0000 | [diff] [blame] | 37 | Constructor<T> constructor = findConstructorIn(container, implementation); |
| 38 | parameterInjectors = createParameterInjector(container, constructor); |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 39 | injectors = container.injectors.get(implementation) |
| 40 | .toArray(new ContainerImpl.Injector[0]); |
| 41 | constructionProxy = container.constructionProxyFactory.get(constructor); |
| 42 | } |
| 43 | |
crazyboblee | 0789b19 | 2007-02-13 02:43:28 +0000 | [diff] [blame] | 44 | /** |
| 45 | * Used to create an invalid injector. |
| 46 | */ |
| 47 | private ConstructorInjector() { |
| 48 | implementation = null; |
| 49 | injectors = null; |
| 50 | parameterInjectors = null; |
| 51 | constructionProxy = null; |
| 52 | } |
| 53 | |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 54 | ContainerImpl.ParameterInjector<?>[] createParameterInjector( |
crazyboblee | 0789b19 | 2007-02-13 02:43:28 +0000 | [diff] [blame] | 55 | ContainerImpl container, Constructor<T> constructor) { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 56 | try { |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 57 | return constructor.getParameterTypes().length == 0 |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 58 | ? null // default constructor. |
| 59 | : container.getParametersInjectors( |
| 60 | constructor, |
| 61 | constructor.getParameterAnnotations(), |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 62 | constructor.getGenericParameterTypes() |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 63 | ); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 64 | } |
| 65 | catch (ContainerImpl.MissingDependencyException e) { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 66 | e.handle(container.errorHandler); |
| 67 | return null; |
| 68 | } |
| 69 | } |
| 70 | |
crazyboblee | 0789b19 | 2007-02-13 02:43:28 +0000 | [diff] [blame] | 71 | private Constructor<T> findConstructorIn(ContainerImpl container, |
| 72 | Class<T> implementation) { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 73 | Constructor<T> found = null; |
crazyboblee | 4602a6f | 2007-02-15 02:45:18 +0000 | [diff] [blame] | 74 | @SuppressWarnings("unchecked") |
kevinb9n | 48d1307 | 2007-02-12 18:21:26 +0000 | [diff] [blame] | 75 | Constructor<T>[] constructors |
| 76 | = (Constructor<T>[]) implementation.getDeclaredConstructors(); |
| 77 | for (Constructor<T> constructor : constructors) { |
crazyboblee | 0bfdbc6 | 2007-02-20 21:47:19 +0000 | [diff] [blame] | 78 | Inject inject = constructor.getAnnotation(Inject.class); |
| 79 | if (inject != null) { |
| 80 | if (inject.optional()) { |
| 81 | container.errorHandler.handle( |
| 82 | StackTraceElements.forMember(constructor), |
| 83 | ErrorMessages.OPTIONAL_CONSTRUCTOR); |
| 84 | } |
| 85 | |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 86 | if (found != null) { |
| 87 | container.errorHandler.handle( |
crazyboblee | 9a3861b | 2007-02-20 03:37:31 +0000 | [diff] [blame] | 88 | StackTraceElements.forMember(found), |
| 89 | ErrorMessages.TOO_MANY_CONSTRUCTORS); |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 90 | return ContainerImpl.invalidConstructor(); |
| 91 | } |
| 92 | found = constructor; |
| 93 | } |
| 94 | } |
| 95 | if (found != null) { |
| 96 | return found; |
| 97 | } |
| 98 | |
| 99 | // If no annotated constructor is found, look for a no-arg constructor |
| 100 | // instead. |
| 101 | try { |
| 102 | return implementation.getDeclaredConstructor(); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 103 | } |
| 104 | catch (NoSuchMethodException e) { |
| 105 | container.errorHandler.handle( |
crazyboblee | 9a3861b | 2007-02-20 03:37:31 +0000 | [diff] [blame] | 106 | StackTraceElements.forMember( |
| 107 | implementation.getDeclaredConstructors()[0]), |
| 108 | ErrorMessages.MISSING_CONSTRUCTOR, |
| 109 | implementation); |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 110 | return ContainerImpl.invalidConstructor(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 115 | * Construct an instance. Returns {@code Object} instead of {@code T} because |
| 116 | * it may return a proxy. |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 117 | */ |
kevinb9n | 48d1307 | 2007-02-12 18:21:26 +0000 | [diff] [blame] | 118 | T construct(InternalContext context, Class<T> expectedType) { |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 119 | ConstructionContext<T> constructionContext |
| 120 | = context.getConstructionContext(this); |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 121 | |
| 122 | // We have a circular reference between constructors. Return a proxy. |
| 123 | if (constructionContext.isConstructing()) { |
| 124 | // TODO (crazybob): if we can't proxy this object, can we proxy the |
| 125 | // other object? |
| 126 | return constructionContext.createProxy(expectedType); |
| 127 | } |
| 128 | |
| 129 | // If we're re-entering this factory while injecting fields or methods, |
| 130 | // return the same instance. This prevents infinite loops. |
| 131 | T t = constructionContext.getCurrentReference(); |
| 132 | if (t != null) { |
| 133 | return t; |
| 134 | } |
| 135 | |
| 136 | try { |
| 137 | // First time through... |
| 138 | constructionContext.startConstruction(); |
| 139 | try { |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 140 | Object[] parameters |
| 141 | = ContainerImpl.getParameters(context, parameterInjectors); |
kevinb9n | 48d1307 | 2007-02-12 18:21:26 +0000 | [diff] [blame] | 142 | t = constructionProxy.newInstance(parameters); |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 143 | constructionContext.setProxyDelegates(t); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 144 | } |
| 145 | finally { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 146 | constructionContext.finishConstruction(); |
| 147 | } |
| 148 | |
| 149 | // Store reference. If an injector re-enters this factory, they'll |
| 150 | // get the same reference. |
| 151 | constructionContext.setCurrentReference(t); |
| 152 | |
| 153 | // Inject fields and methods. |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 154 | for (ContainerImpl.Injector injector : injectors) { |
| 155 | injector.inject(context, t); |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | return t; |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 159 | } |
| 160 | catch (InvocationTargetException e) { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 161 | throw new RuntimeException(e); |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 162 | } |
| 163 | finally { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 164 | constructionContext.removeCurrentReference(); |
| 165 | } |
| 166 | } |
| 167 | |
crazyboblee | 0789b19 | 2007-02-13 02:43:28 +0000 | [diff] [blame] | 168 | /** |
| 169 | * Returns an invalid constructor. This enables us to keep running and |
| 170 | * reporting legitimate errors. |
| 171 | */ |
| 172 | static <T> ConstructorInjector<T> invalidConstructor() { |
| 173 | return new ConstructorInjector<T>() { |
| 174 | T construct(InternalContext context, Class<T> expectedType) { |
| 175 | throw new UnsupportedOperationException(); |
| 176 | } |
| 177 | public T get() { |
| 178 | throw new UnsupportedOperationException(); |
| 179 | } |
| 180 | }; |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 181 | } |
| 182 | } |