blob: ce88d2c08aa5eb0ec247cc56369805a4f9503eca [file] [log] [blame]
limpbizkit7cef5b02009-03-29 21:16:51 +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;
23import com.google.inject.internal.Lists;
limpbizkit7cef5b02009-03-29 21:16:51 +000024import com.google.inject.spi.InjectionPoint;
limpbizkitee792462009-04-08 23:48:49 +000025import com.google.inject.spi.TypeListenerBinding;
limpbizkit7cef5b02009-03-29 21:16:51 +000026import java.lang.reflect.Field;
limpbizkit7cef5b02009-03-29 21:16:51 +000027import java.util.List;
28import java.util.Set;
limpbizkit7cef5b02009-03-29 21:16:51 +000029
30/**
31 * Members injectors by type.
32 *
33 * @author jessewilson@google.com (Jesse Wilson)
34 */
35class MembersInjectorStore {
36 private final InjectorImpl injector;
limpbizkitee792462009-04-08 23:48:49 +000037 private final ImmutableList<TypeListenerBinding> typeListenerBindings;
limpbizkit7cef5b02009-03-29 21:16:51 +000038
39 private final FailableCache<TypeLiteral<?>, MembersInjectorImpl<?>> cache
40 = new FailableCache<TypeLiteral<?>, MembersInjectorImpl<?>>() {
41 @Override protected MembersInjectorImpl<?> create(TypeLiteral<?> type, Errors errors)
42 throws ErrorsException {
43 return createWithListeners(type, errors);
44 }
45 };
46
47 MembersInjectorStore(InjectorImpl injector,
limpbizkitee792462009-04-08 23:48:49 +000048 List<TypeListenerBinding> typeListenerBindings) {
limpbizkit7cef5b02009-03-29 21:16:51 +000049 this.injector = injector;
limpbizkitee792462009-04-08 23:48:49 +000050 this.typeListenerBindings = ImmutableList.copyOf(typeListenerBindings);
limpbizkit7cef5b02009-03-29 21:16:51 +000051 }
52
53 /**
limpbizkita843a952009-04-08 22:24:55 +000054 * Returns true if any type listeners are installed. Other code may take shortcuts when there
55 * aren't any type listeners.
56 */
57 public boolean hasTypeListeners() {
limpbizkitee792462009-04-08 23:48:49 +000058 return !typeListenerBindings.isEmpty();
limpbizkita843a952009-04-08 22:24:55 +000059 }
60
61 /**
limpbizkit7cef5b02009-03-29 21:16:51 +000062 * Returns a new complete members injector with injection listeners registered.
63 */
64 @SuppressWarnings("unchecked") // the MembersInjector type always agrees with the passed type
65 public <T> MembersInjectorImpl<T> get(TypeLiteral<T> key, Errors errors) throws ErrorsException {
66 return (MembersInjectorImpl<T>) cache.get(key, errors);
67 }
68
69 /**
limpbizkita843a952009-04-08 22:24:55 +000070 * Creates a new members injector and attaches both injection listeners and method aspects.
limpbizkit7cef5b02009-03-29 21:16:51 +000071 */
limpbizkita843a952009-04-08 22:24:55 +000072 private <T> MembersInjectorImpl<T> createWithListeners(TypeLiteral<T> type, Errors errors)
limpbizkit7cef5b02009-03-29 21:16:51 +000073 throws ErrorsException {
74 int numErrorsBefore = errors.size();
limpbizkita843a952009-04-08 22:24:55 +000075
limpbizkit7cef5b02009-03-29 21:16:51 +000076 Set<InjectionPoint> injectionPoints;
77 try {
78 injectionPoints = InjectionPoint.forInstanceMethodsAndFields(type);
79 } catch (ConfigurationException e) {
80 errors.merge(e.getErrorMessages());
81 injectionPoints = e.getPartialValue();
82 }
83 ImmutableList<SingleMemberInjector> injectors = getInjectors(injectionPoints, errors);
84 errors.throwIfNewErrors(numErrorsBefore);
limpbizkit7cef5b02009-03-29 21:16:51 +000085
limpbizkit8d620752009-03-31 22:37:26 +000086 EncounterImpl<T> encounter = new EncounterImpl<T>(errors, injector.lookups);
limpbizkitee792462009-04-08 23:48:49 +000087 for (TypeListenerBinding typeListener : typeListenerBindings) {
limpbizkit7cef5b02009-03-29 21:16:51 +000088 if (typeListener.getTypeMatcher().matches(type)) {
89 try {
limpbizkita843a952009-04-08 22:24:55 +000090 typeListener.getListener().hear(type, encounter);
limpbizkit7cef5b02009-03-29 21:16:51 +000091 } catch (RuntimeException e) {
limpbizkita843a952009-04-08 22:24:55 +000092 errors.errorNotifyingTypeListener(typeListener, type, e);
limpbizkit7cef5b02009-03-29 21:16:51 +000093 }
94 }
95 }
limpbizkitccb15e42009-04-01 16:33:29 +000096 encounter.invalidate();
limpbizkit7cef5b02009-03-29 21:16:51 +000097 errors.throwIfNewErrors(numErrorsBefore);
limpbizkita843a952009-04-08 22:24:55 +000098
limpbizkiteb405132009-04-14 00:50:25 +000099 return new MembersInjectorImpl<T>(injector, type, encounter, injectors);
limpbizkit7cef5b02009-03-29 21:16:51 +0000100 }
101
102 /**
103 * Returns the injectors for the specified injection points.
104 */
105 ImmutableList<SingleMemberInjector> getInjectors(
106 Set<InjectionPoint> injectionPoints, Errors errors) {
107 List<SingleMemberInjector> injectors = Lists.newArrayList();
108 for (InjectionPoint injectionPoint : injectionPoints) {
109 try {
110 Errors errorsForMember = injectionPoint.isOptional()
111 ? new Errors(injectionPoint)
112 : errors.withSource(injectionPoint);
113 SingleMemberInjector injector = injectionPoint.getMember() instanceof Field
114 ? new SingleFieldInjector(this.injector, injectionPoint, errorsForMember)
115 : new SingleMethodInjector(this.injector, injectionPoint, errorsForMember);
116 injectors.add(injector);
117 } catch (ErrorsException ignoredForNow) {
118 // ignored for now
119 }
120 }
121 return ImmutableList.copyOf(injectors);
122 }
123}