blob: 8fce69659a74499aa1b3a9e2403113c55e03bc6d [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
limpbizkit5ae41eb2009-06-06 17:51:27 +000019import com.google.inject.Key;
20import com.google.inject.MembersInjector;
21import com.google.inject.Provider;
22import com.google.inject.TypeLiteral;
limpbizkitccb15e42009-04-01 16:33:29 +000023import static com.google.inject.internal.Preconditions.checkState;
limpbizkit7cef5b02009-03-29 21:16:51 +000024import com.google.inject.matcher.Matcher;
25import com.google.inject.matcher.Matchers;
limpbizkit8d620752009-03-31 22:37:26 +000026import com.google.inject.spi.InjectionListener;
27import com.google.inject.spi.Message;
limpbizkita843a952009-04-08 22:24:55 +000028import com.google.inject.spi.TypeEncounter;
limpbizkit7cef5b02009-03-29 21:16:51 +000029import java.lang.reflect.Method;
limpbizkit8d620752009-03-31 22:37:26 +000030import java.util.List;
limpbizkit7cef5b02009-03-29 21:16:51 +000031
32/**
33 * @author jessewilson@google.com (Jesse Wilson)
34 */
limpbizkita843a952009-04-08 22:24:55 +000035final class EncounterImpl<T> implements TypeEncounter<T> {
limpbizkit8d620752009-03-31 22:37:26 +000036
37 private final Errors errors;
38 private final Lookups lookups;
limpbizkitee792462009-04-08 23:48:49 +000039 private List<MembersInjector<? super T>> membersInjectors; // lazy
limpbizkit7cef5b02009-03-29 21:16:51 +000040 private List<InjectionListener<? super T>> injectionListeners; // lazy
limpbizkiteb405132009-04-14 00:50:25 +000041 /*if[AOP]*/
limpbizkit7cef5b02009-03-29 21:16:51 +000042 private List<MethodAspect> aspects; // lazy
limpbizkiteb405132009-04-14 00:50:25 +000043 /*end[AOP]*/
limpbizkitccb15e42009-04-01 16:33:29 +000044 private boolean valid = true;
limpbizkit7cef5b02009-03-29 21:16:51 +000045
limpbizkit5ae41eb2009-06-06 17:51:27 +000046 EncounterImpl(Errors errors, Lookups lookups) {
limpbizkit8d620752009-03-31 22:37:26 +000047 this.errors = errors;
48 this.lookups = lookups;
49 }
50
limpbizkit5ae41eb2009-06-06 17:51:27 +000051 void invalidate() {
limpbizkitccb15e42009-04-01 16:33:29 +000052 valid = false;
53 }
54
limpbizkiteb405132009-04-14 00:50:25 +000055 /*if[AOP]*/
limpbizkit5ae41eb2009-06-06 17:51:27 +000056 ImmutableList<MethodAspect> getAspects() {
limpbizkita843a952009-04-08 22:24:55 +000057 return aspects == null
58 ? ImmutableList.<MethodAspect>of()
59 : ImmutableList.copyOf(aspects);
limpbizkit7cef5b02009-03-29 21:16:51 +000060 }
61
limpbizkiteb405132009-04-14 00:50:25 +000062 public void bindInterceptor(Matcher<? super Method> methodMatcher,
63 org.aopalliance.intercept.MethodInterceptor... interceptors) {
64 checkState(valid, "Encounters may not be used after hear() returns.");
65
66 // make sure the applicable aspects is mutable
67 if (aspects == null) {
68 aspects = Lists.newArrayList();
69 }
70
71 aspects.add(new MethodAspect(Matchers.any(), methodMatcher, interceptors));
72 }
73 /*end[AOP]*/
74
limpbizkit5ae41eb2009-06-06 17:51:27 +000075 ImmutableList<MembersInjector<? super T>> getMembersInjectors() {
limpbizkitee792462009-04-08 23:48:49 +000076 return membersInjectors == null
77 ? ImmutableList.<MembersInjector<? super T>>of()
78 : ImmutableList.copyOf(membersInjectors);
79 }
80
limpbizkit5ae41eb2009-06-06 17:51:27 +000081 ImmutableList<InjectionListener<? super T>> getInjectionListeners() {
limpbizkit7cef5b02009-03-29 21:16:51 +000082 return injectionListeners == null
83 ? ImmutableList.<InjectionListener<? super T>>of()
84 : ImmutableList.copyOf(injectionListeners);
85 }
86
limpbizkitee792462009-04-08 23:48:49 +000087 public void register(MembersInjector<? super T> membersInjector) {
88 checkState(valid, "Encounters may not be used after hear() returns.");
89
90 if (membersInjectors == null) {
91 membersInjectors = Lists.newArrayList();
92 }
93
94 membersInjectors.add(membersInjector);
95 }
96
limpbizkit7cef5b02009-03-29 21:16:51 +000097 public void register(InjectionListener<? super T> injectionListener) {
limpbizkitccb15e42009-04-01 16:33:29 +000098 checkState(valid, "Encounters may not be used after hear() returns.");
99
limpbizkit7cef5b02009-03-29 21:16:51 +0000100 if (injectionListeners == null) {
101 injectionListeners = Lists.newArrayList();
102 }
103
limpbizkit8d620752009-03-31 22:37:26 +0000104 injectionListeners.add(injectionListener);
limpbizkit7cef5b02009-03-29 21:16:51 +0000105 }
106
limpbizkit7cef5b02009-03-29 21:16:51 +0000107 public void addError(String message, Object... arguments) {
limpbizkitccb15e42009-04-01 16:33:29 +0000108 checkState(valid, "Encounters may not be used after hear() returns.");
limpbizkit8d620752009-03-31 22:37:26 +0000109 errors.addMessage(message, arguments);
limpbizkit7cef5b02009-03-29 21:16:51 +0000110 }
111
112 public void addError(Throwable t) {
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.errorInUserCode(t, "An exception was caught and reported. Message: %s", t.getMessage());
limpbizkit7cef5b02009-03-29 21:16:51 +0000115 }
116
117 public void addError(Message message) {
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.addMessage(message);
limpbizkit7cef5b02009-03-29 21:16:51 +0000120 }
121
122 public <T> Provider<T> getProvider(Key<T> key) {
limpbizkitccb15e42009-04-01 16:33:29 +0000123 checkState(valid, "Encounters may not be used after hear() returns.");
limpbizkit8d620752009-03-31 22:37:26 +0000124 return lookups.getProvider(key);
limpbizkit7cef5b02009-03-29 21:16:51 +0000125 }
126
127 public <T> Provider<T> getProvider(Class<T> type) {
limpbizkit8d620752009-03-31 22:37:26 +0000128 return getProvider(Key.get(type));
limpbizkit7cef5b02009-03-29 21:16:51 +0000129 }
130
131 public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> typeLiteral) {
limpbizkitccb15e42009-04-01 16:33:29 +0000132 checkState(valid, "Encounters may not be used after hear() returns.");
limpbizkit8d620752009-03-31 22:37:26 +0000133 return lookups.getMembersInjector(typeLiteral);
limpbizkit7cef5b02009-03-29 21:16:51 +0000134 }
135
136 public <T> MembersInjector<T> getMembersInjector(Class<T> type) {
limpbizkit8d620752009-03-31 22:37:26 +0000137 return getMembersInjector(TypeLiteral.get(type));
limpbizkit7cef5b02009-03-29 21:16:51 +0000138 }
limpbizkit8d620752009-03-31 22:37:26 +0000139}