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