blob: 8aa3e948bd7670f80eccf96fed1b717b7375c77d [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
limpbizkit5ae41eb2009-06-06 17:51:27 +000017package com.google.inject.internal;
limpbizkit7cef5b02009-03-29 21:16:51 +000018
sberlinb7a02b02011-07-08 00:34:16 +000019import static com.google.common.base.Preconditions.checkState;
20
21import com.google.common.collect.ImmutableList;
Sam Berlin35023b82013-12-06 17:08:01 -050022import com.google.common.collect.ImmutableSet;
sberlinb7a02b02011-07-08 00:34:16 +000023import com.google.common.collect.Lists;
limpbizkit5ae41eb2009-06-06 17:51:27 +000024import com.google.inject.Key;
25import com.google.inject.MembersInjector;
26import com.google.inject.Provider;
27import com.google.inject.TypeLiteral;
limpbizkit7cef5b02009-03-29 21:16:51 +000028import com.google.inject.matcher.Matcher;
29import com.google.inject.matcher.Matchers;
limpbizkit8d620752009-03-31 22:37:26 +000030import com.google.inject.spi.InjectionListener;
31import com.google.inject.spi.Message;
limpbizkita843a952009-04-08 22:24:55 +000032import com.google.inject.spi.TypeEncounter;
sberlinb7a02b02011-07-08 00:34:16 +000033
limpbizkit7cef5b02009-03-29 21:16:51 +000034import java.lang.reflect.Method;
limpbizkit8d620752009-03-31 22:37:26 +000035import java.util.List;
limpbizkit7cef5b02009-03-29 21:16:51 +000036
37/**
38 * @author jessewilson@google.com (Jesse Wilson)
39 */
limpbizkita843a952009-04-08 22:24:55 +000040final class EncounterImpl<T> implements TypeEncounter<T> {
limpbizkit8d620752009-03-31 22:37:26 +000041
42 private final Errors errors;
43 private final Lookups lookups;
limpbizkitee792462009-04-08 23:48:49 +000044 private List<MembersInjector<? super T>> membersInjectors; // lazy
limpbizkit7cef5b02009-03-29 21:16:51 +000045 private List<InjectionListener<? super T>> injectionListeners; // lazy
limpbizkiteb405132009-04-14 00:50:25 +000046 /*if[AOP]*/
limpbizkit7cef5b02009-03-29 21:16:51 +000047 private List<MethodAspect> aspects; // lazy
limpbizkiteb405132009-04-14 00:50:25 +000048 /*end[AOP]*/
limpbizkitccb15e42009-04-01 16:33:29 +000049 private boolean valid = true;
limpbizkit7cef5b02009-03-29 21:16:51 +000050
limpbizkit5ae41eb2009-06-06 17:51:27 +000051 EncounterImpl(Errors errors, Lookups lookups) {
limpbizkit8d620752009-03-31 22:37:26 +000052 this.errors = errors;
53 this.lookups = lookups;
54 }
55
limpbizkit5ae41eb2009-06-06 17:51:27 +000056 void invalidate() {
limpbizkitccb15e42009-04-01 16:33:29 +000057 valid = false;
58 }
59
limpbizkiteb405132009-04-14 00:50:25 +000060 /*if[AOP]*/
limpbizkit5ae41eb2009-06-06 17:51:27 +000061 ImmutableList<MethodAspect> getAspects() {
limpbizkita843a952009-04-08 22:24:55 +000062 return aspects == null
63 ? ImmutableList.<MethodAspect>of()
64 : ImmutableList.copyOf(aspects);
limpbizkit7cef5b02009-03-29 21:16:51 +000065 }
66
limpbizkiteb405132009-04-14 00:50:25 +000067 public void bindInterceptor(Matcher<? super Method> methodMatcher,
68 org.aopalliance.intercept.MethodInterceptor... interceptors) {
69 checkState(valid, "Encounters may not be used after hear() returns.");
70
71 // make sure the applicable aspects is mutable
72 if (aspects == null) {
73 aspects = Lists.newArrayList();
74 }
75
76 aspects.add(new MethodAspect(Matchers.any(), methodMatcher, interceptors));
77 }
78 /*end[AOP]*/
79
Sam Berlin35023b82013-12-06 17:08:01 -050080 ImmutableSet<MembersInjector<? super T>> getMembersInjectors() {
limpbizkitee792462009-04-08 23:48:49 +000081 return membersInjectors == null
Sam Berlin35023b82013-12-06 17:08:01 -050082 ? ImmutableSet.<MembersInjector<? super T>>of()
83 : ImmutableSet.copyOf(membersInjectors);
limpbizkitee792462009-04-08 23:48:49 +000084 }
85
Sam Berlin35023b82013-12-06 17:08:01 -050086 ImmutableSet<InjectionListener<? super T>> getInjectionListeners() {
limpbizkit7cef5b02009-03-29 21:16:51 +000087 return injectionListeners == null
Sam Berlin35023b82013-12-06 17:08:01 -050088 ? ImmutableSet.<InjectionListener<? super T>>of()
89 : ImmutableSet.copyOf(injectionListeners);
limpbizkit7cef5b02009-03-29 21:16:51 +000090 }
91
limpbizkitee792462009-04-08 23:48:49 +000092 public void register(MembersInjector<? super T> membersInjector) {
93 checkState(valid, "Encounters may not be used after hear() returns.");
94
95 if (membersInjectors == null) {
96 membersInjectors = Lists.newArrayList();
97 }
98
99 membersInjectors.add(membersInjector);
100 }
101
limpbizkit7cef5b02009-03-29 21:16:51 +0000102 public void register(InjectionListener<? super T> injectionListener) {
limpbizkitccb15e42009-04-01 16:33:29 +0000103 checkState(valid, "Encounters may not be used after hear() returns.");
104
limpbizkit7cef5b02009-03-29 21:16:51 +0000105 if (injectionListeners == null) {
106 injectionListeners = Lists.newArrayList();
107 }
108
limpbizkit8d620752009-03-31 22:37:26 +0000109 injectionListeners.add(injectionListener);
limpbizkit7cef5b02009-03-29 21:16:51 +0000110 }
111
limpbizkit7cef5b02009-03-29 21:16:51 +0000112 public void addError(String message, Object... arguments) {
limpbizkitccb15e42009-04-01 16:33:29 +0000113 checkState(valid, "Encounters may not be used after hear() returns.");
limpbizkit8d620752009-03-31 22:37:26 +0000114 errors.addMessage(message, arguments);
limpbizkit7cef5b02009-03-29 21:16:51 +0000115 }
116
117 public void addError(Throwable t) {
limpbizkitccb15e42009-04-01 16:33:29 +0000118 checkState(valid, "Encounters may not be used after hear() returns.");
limpbizkit8d620752009-03-31 22:37:26 +0000119 errors.errorInUserCode(t, "An exception was caught and reported. Message: %s", t.getMessage());
limpbizkit7cef5b02009-03-29 21:16:51 +0000120 }
121
122 public void addError(Message message) {
limpbizkitccb15e42009-04-01 16:33:29 +0000123 checkState(valid, "Encounters may not be used after hear() returns.");
limpbizkit8d620752009-03-31 22:37:26 +0000124 errors.addMessage(message);
limpbizkit7cef5b02009-03-29 21:16:51 +0000125 }
126
127 public <T> Provider<T> getProvider(Key<T> key) {
limpbizkitccb15e42009-04-01 16:33:29 +0000128 checkState(valid, "Encounters may not be used after hear() returns.");
limpbizkit8d620752009-03-31 22:37:26 +0000129 return lookups.getProvider(key);
limpbizkit7cef5b02009-03-29 21:16:51 +0000130 }
131
132 public <T> Provider<T> getProvider(Class<T> type) {
limpbizkit8d620752009-03-31 22:37:26 +0000133 return getProvider(Key.get(type));
limpbizkit7cef5b02009-03-29 21:16:51 +0000134 }
135
136 public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> typeLiteral) {
limpbizkitccb15e42009-04-01 16:33:29 +0000137 checkState(valid, "Encounters may not be used after hear() returns.");
limpbizkit8d620752009-03-31 22:37:26 +0000138 return lookups.getMembersInjector(typeLiteral);
limpbizkit7cef5b02009-03-29 21:16:51 +0000139 }
140
141 public <T> MembersInjector<T> getMembersInjector(Class<T> type) {
limpbizkit8d620752009-03-31 22:37:26 +0000142 return getMembersInjector(TypeLiteral.get(type));
limpbizkit7cef5b02009-03-29 21:16:51 +0000143 }
limpbizkit8d620752009-03-31 22:37:26 +0000144}