blob: af0b07d38ba2dc163d03a19e70cd02214472ebac [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
limpbizkit5ae41eb2009-06-06 17:51:27 +000017package com.google.inject.internal;
limpbizkit03b81a62009-03-18 05:34:39 +000018
limpbizkit5ae41eb2009-06-06 17:51:27 +000019import com.google.inject.ConfigurationException;
20import com.google.inject.TypeLiteral;
limpbizkit03b81a62009-03-18 05:34:39 +000021import static com.google.inject.internal.Iterables.concat;
limpbizkit03b81a62009-03-18 05:34:39 +000022import com.google.inject.spi.InjectionPoint;
limpbizkit03b81a62009-03-18 05:34:39 +000023
24/**
25 * Constructor injectors by type.
26 *
27 * @author jessewilson@google.com (Jesse Wilson)
28 */
limpbizkit5ae41eb2009-06-06 17:51:27 +000029final class ConstructorInjectorStore {
limpbizkit03b81a62009-03-18 05:34:39 +000030 private final InjectorImpl injector;
limpbizkit03b81a62009-03-18 05:34:39 +000031
limpbizkit7cef5b02009-03-29 21:16:51 +000032 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
limpbizkita843a952009-04-08 22:24:55 +000041 ConstructorInjectorStore(InjectorImpl injector) {
limpbizkit03b81a62009-03-18 05:34:39 +000042 this.injector = injector;
limpbizkit03b81a62009-03-18 05:34:39 +000043 }
44
limpbizkit7cef5b02009-03-29 21:16:51 +000045 /**
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);
limpbizkit03b81a62009-03-18 05:34:39 +000051 }
52
53 private <T> ConstructorInjector<T> createConstructor(TypeLiteral<T> type, Errors errors)
54 throws ErrorsException {
limpbizkit516e2ab2009-03-26 22:01:18 +000055 int numErrorsBefore = errors.size();
limpbizkit03b81a62009-03-18 05:34:39 +000056
limpbizkit516e2ab2009-03-26 22:01:18 +000057 InjectionPoint injectionPoint;
58 try {
59 injectionPoint = InjectionPoint.forConstructorOf(type);
60 } catch (ConfigurationException e) {
61 errors.merge(e.getErrorMessages());
62 throw errors.toException();
63 }
64
limpbizkite89c49e2009-05-06 01:02:14 +000065 SingleParameterInjector<?>[] constructorParameterInjectors
limpbizkit03b81a62009-03-18 05:34:39 +000066 = injector.getParametersInjectors(injectionPoint.getDependencies(), errors);
limpbizkita843a952009-04-08 22:24:55 +000067 MembersInjectorImpl<T> membersInjector = injector.membersInjectorStore.get(type, errors);
limpbizkit03b81a62009-03-18 05:34:39 +000068
limpbizkiteb405132009-04-14 00:50:25 +000069 /*if[AOP]*/
70 ImmutableList<MethodAspect> injectorAspects = injector.state.getMethodAspects();
limpbizkita843a952009-04-08 22:24:55 +000071 ImmutableList<MethodAspect> methodAspects = membersInjector.getAddedAspects().isEmpty()
limpbizkiteb405132009-04-14 00:50:25 +000072 ? 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]*/
limpbizkit03b81a62009-03-18 05:34:39 +000079
limpbizkit516e2ab2009-03-26 22:01:18 +000080 errors.throwIfNewErrors(numErrorsBefore);
81
limpbizkiteb405132009-04-14 00:50:25 +000082 return new ConstructorInjector<T>(membersInjector.getInjectionPoints(), factory.create(),
limpbizkita843a952009-04-08 22:24:55 +000083 constructorParameterInjectors, membersInjector);
limpbizkit03b81a62009-03-18 05:34:39 +000084 }
limpbizkit03b81a62009-03-18 05:34:39 +000085}