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