limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2009 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 com.google.inject.internal.Errors; |
| 20 | import com.google.inject.internal.ErrorsException; |
| 21 | import com.google.inject.internal.FailableCache; |
| 22 | import com.google.inject.internal.ImmutableList; |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 23 | import static com.google.inject.internal.Iterables.concat; |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 24 | import com.google.inject.spi.InjectionPoint; |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Constructor injectors by type. |
| 28 | * |
| 29 | * @author jessewilson@google.com (Jesse Wilson) |
| 30 | */ |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 31 | class ConstructorInjectorStore { |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 32 | private final InjectorImpl injector; |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 33 | |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 34 | private final FailableCache<TypeLiteral<?>, ConstructorInjector<?>> cache |
| 35 | = new FailableCache<TypeLiteral<?>, ConstructorInjector<?>> () { |
| 36 | @SuppressWarnings("unchecked") |
| 37 | protected ConstructorInjector<?> create(TypeLiteral<?> type, Errors errors) |
| 38 | throws ErrorsException { |
| 39 | return createConstructor(type, errors); |
| 40 | } |
| 41 | }; |
| 42 | |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 43 | ConstructorInjectorStore(InjectorImpl injector) { |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 44 | this.injector = injector; |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 45 | } |
| 46 | |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 47 | /** |
| 48 | * Returns a new complete constructor injector with injection listeners registered. |
| 49 | */ |
| 50 | @SuppressWarnings("unchecked") // the ConstructorInjector type always agrees with the passed type |
| 51 | public <T> ConstructorInjector<T> get(TypeLiteral<T> key, Errors errors) throws ErrorsException { |
| 52 | return (ConstructorInjector<T>) cache.get(key, errors); |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | private <T> ConstructorInjector<T> createConstructor(TypeLiteral<T> type, Errors errors) |
| 56 | throws ErrorsException { |
limpbizkit | 516e2ab | 2009-03-26 22:01:18 +0000 | [diff] [blame] | 57 | int numErrorsBefore = errors.size(); |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 58 | |
limpbizkit | 516e2ab | 2009-03-26 22:01:18 +0000 | [diff] [blame] | 59 | InjectionPoint injectionPoint; |
| 60 | try { |
| 61 | injectionPoint = InjectionPoint.forConstructorOf(type); |
| 62 | } catch (ConfigurationException e) { |
| 63 | errors.merge(e.getErrorMessages()); |
| 64 | throw errors.toException(); |
| 65 | } |
| 66 | |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 67 | SingleParameterInjector<?>[] constructorParameterInjectors |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 68 | = injector.getParametersInjectors(injectionPoint.getDependencies(), errors); |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 69 | MembersInjectorImpl<T> membersInjector = injector.membersInjectorStore.get(type, errors); |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 70 | |
limpbizkit | eb40513 | 2009-04-14 00:50:25 +0000 | [diff] [blame] | 71 | /*if[AOP]*/ |
| 72 | ImmutableList<MethodAspect> injectorAspects = injector.state.getMethodAspects(); |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 73 | ImmutableList<MethodAspect> methodAspects = membersInjector.getAddedAspects().isEmpty() |
limpbizkit | eb40513 | 2009-04-14 00:50:25 +0000 | [diff] [blame] | 74 | ? injectorAspects |
| 75 | : ImmutableList.copyOf(concat(injectorAspects, membersInjector.getAddedAspects())); |
| 76 | ConstructionProxyFactory<T> factory = new ProxyFactory<T>(injectionPoint, methodAspects); |
| 77 | /*end[AOP]*/ |
| 78 | /*if[NO_AOP] |
| 79 | ConstructionProxyFactory<T> factory = new DefaultConstructionProxyFactory<T>(injectionPoint); |
| 80 | end[NO_AOP]*/ |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 81 | |
limpbizkit | 516e2ab | 2009-03-26 22:01:18 +0000 | [diff] [blame] | 82 | errors.throwIfNewErrors(numErrorsBefore); |
| 83 | |
limpbizkit | eb40513 | 2009-04-14 00:50:25 +0000 | [diff] [blame] | 84 | return new ConstructorInjector<T>(membersInjector.getInjectionPoints(), factory.create(), |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 85 | constructorParameterInjectors, membersInjector); |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 86 | } |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 87 | } |