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 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 17 | package com.google.inject.internal; |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 18 | |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 19 | import com.google.inject.spi.InjectionPoint; |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 20 | import java.lang.reflect.InvocationTargetException; |
| 21 | |
| 22 | /** |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 23 | * Creates instances using an injectable constructor. After construction, all injectable fields and |
| 24 | * methods are injected. |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 25 | * |
| 26 | * @author crazybob@google.com (Bob Lee) |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 27 | */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 28 | final class ConstructorInjector<T> { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 29 | |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 30 | private final ImmutableSet<InjectionPoint> injectableMembers; |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 31 | private final SingleParameterInjector<?>[] parameterInjectors; |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 32 | private final ConstructionProxy<T> constructionProxy; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 33 | private final MembersInjectorImpl<T> membersInjector; |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 34 | |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 35 | ConstructorInjector(ImmutableSet<InjectionPoint> injectableMembers, |
| 36 | ConstructionProxy<T> constructionProxy, |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 37 | SingleParameterInjector<?>[] parameterInjectors, |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 38 | MembersInjectorImpl<T> membersInjector) |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 39 | throws ErrorsException { |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 40 | this.injectableMembers = injectableMembers; |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 41 | this.constructionProxy = constructionProxy; |
| 42 | this.parameterInjectors = parameterInjectors; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 43 | this.membersInjector = membersInjector; |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | public ImmutableSet<InjectionPoint> getInjectableMembers() { |
| 47 | return injectableMembers; |
| 48 | } |
| 49 | |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 50 | ConstructionProxy<T> getConstructionProxy() { |
| 51 | return constructionProxy; |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 52 | } |
| 53 | |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 54 | /** |
kevinb9n | a99dca7 | 2007-02-11 04:48:57 +0000 | [diff] [blame] | 55 | * Construct an instance. Returns {@code Object} instead of {@code T} because |
| 56 | * it may return a proxy. |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 57 | */ |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 58 | Object construct(Errors errors, InternalContext context, Class<?> expectedType) |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 59 | throws ErrorsException { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 60 | ConstructionContext<T> constructionContext = context.getConstructionContext(this); |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 61 | |
| 62 | // We have a circular reference between constructors. Return a proxy. |
| 63 | if (constructionContext.isConstructing()) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 64 | // TODO (crazybob): if we can't proxy this object, can we proxy the other object? |
| 65 | return constructionContext.createProxy(errors, expectedType); |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // If we're re-entering this factory while injecting fields or methods, |
| 69 | // return the same instance. This prevents infinite loops. |
| 70 | T t = constructionContext.getCurrentReference(); |
| 71 | if (t != null) { |
| 72 | return t; |
| 73 | } |
| 74 | |
| 75 | try { |
| 76 | // First time through... |
| 77 | constructionContext.startConstruction(); |
| 78 | try { |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 79 | Object[] parameters = SingleParameterInjector.getAll(errors, context, parameterInjectors); |
kevinb9n | 48d1307 | 2007-02-12 18:21:26 +0000 | [diff] [blame] | 80 | t = constructionProxy.newInstance(parameters); |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 81 | constructionContext.setProxyDelegates(t); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 82 | } finally { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 83 | constructionContext.finishConstruction(); |
| 84 | } |
| 85 | |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 86 | // Store reference. If an injector re-enters this factory, they'll get the same reference. |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 87 | constructionContext.setCurrentReference(t); |
| 88 | |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 89 | membersInjector.injectMembers(t, errors, context); |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 90 | membersInjector.notifyListeners(t, errors); |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 91 | |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 92 | return t; |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 93 | } catch (InvocationTargetException userException) { |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 94 | Throwable cause = userException.getCause() != null |
| 95 | ? userException.getCause() |
| 96 | : userException; |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 97 | throw errors.withSource(constructionProxy.getInjectionPoint()) |
| 98 | .errorInjectingConstructor(cause).toException(); |
| 99 | } finally { |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 100 | constructionContext.removeCurrentReference(); |
| 101 | } |
| 102 | } |
crazyboblee | e3adfd6 | 2007-02-02 21:30:08 +0000 | [diff] [blame] | 103 | } |