blob: 069318f164ac933c08d0549e7c4e1185b5f54ce8 [file] [log] [blame]
limpbizkit03b81a62009-03-18 05:34:39 +00001/**
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
17package com.google.inject;
18
19import com.google.inject.internal.Errors;
20import com.google.inject.internal.ErrorsException;
21import com.google.inject.internal.FailableCache;
22import com.google.inject.internal.ImmutableList;
limpbizkit03b81a62009-03-18 05:34:39 +000023import static com.google.inject.internal.Iterables.concat;
limpbizkit03b81a62009-03-18 05:34:39 +000024import com.google.inject.spi.InjectionPoint;
limpbizkit03b81a62009-03-18 05:34:39 +000025
26/**
27 * Constructor injectors by type.
28 *
29 * @author jessewilson@google.com (Jesse Wilson)
30 */
limpbizkit7cef5b02009-03-29 21:16:51 +000031class ConstructorInjectorStore {
limpbizkit03b81a62009-03-18 05:34:39 +000032 private final InjectorImpl injector;
limpbizkit03b81a62009-03-18 05:34:39 +000033
limpbizkit7cef5b02009-03-29 21:16:51 +000034 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
limpbizkita843a952009-04-08 22:24:55 +000043 ConstructorInjectorStore(InjectorImpl injector) {
limpbizkit03b81a62009-03-18 05:34:39 +000044 this.injector = injector;
limpbizkit03b81a62009-03-18 05:34:39 +000045 }
46
limpbizkit7cef5b02009-03-29 21:16:51 +000047 /**
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);
limpbizkit03b81a62009-03-18 05:34:39 +000053 }
54
55 private <T> ConstructorInjector<T> createConstructor(TypeLiteral<T> type, Errors errors)
56 throws ErrorsException {
limpbizkit516e2ab2009-03-26 22:01:18 +000057 int numErrorsBefore = errors.size();
limpbizkit03b81a62009-03-18 05:34:39 +000058
limpbizkit516e2ab2009-03-26 22:01:18 +000059 InjectionPoint injectionPoint;
60 try {
61 injectionPoint = InjectionPoint.forConstructorOf(type);
62 } catch (ConfigurationException e) {
63 errors.merge(e.getErrorMessages());
64 throw errors.toException();
65 }
66
limpbizkite89c49e2009-05-06 01:02:14 +000067 SingleParameterInjector<?>[] constructorParameterInjectors
limpbizkit03b81a62009-03-18 05:34:39 +000068 = injector.getParametersInjectors(injectionPoint.getDependencies(), errors);
limpbizkita843a952009-04-08 22:24:55 +000069 MembersInjectorImpl<T> membersInjector = injector.membersInjectorStore.get(type, errors);
limpbizkit03b81a62009-03-18 05:34:39 +000070
limpbizkiteb405132009-04-14 00:50:25 +000071 /*if[AOP]*/
72 ImmutableList<MethodAspect> injectorAspects = injector.state.getMethodAspects();
limpbizkita843a952009-04-08 22:24:55 +000073 ImmutableList<MethodAspect> methodAspects = membersInjector.getAddedAspects().isEmpty()
limpbizkiteb405132009-04-14 00:50:25 +000074 ? 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]*/
limpbizkit03b81a62009-03-18 05:34:39 +000081
limpbizkit516e2ab2009-03-26 22:01:18 +000082 errors.throwIfNewErrors(numErrorsBefore);
83
limpbizkiteb405132009-04-14 00:50:25 +000084 return new ConstructorInjector<T>(membersInjector.getInjectionPoints(), factory.create(),
limpbizkita843a952009-04-08 22:24:55 +000085 constructorParameterInjectors, membersInjector);
limpbizkit03b81a62009-03-18 05:34:39 +000086 }
limpbizkit03b81a62009-03-18 05:34:39 +000087}