blob: 396300963a7e7b9b1d6d20758427c11558771f27 [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
limpbizkit8d620752009-03-31 22:37:26 +000019import com.google.inject.internal.Errors;
limpbizkit7cef5b02009-03-29 21:16:51 +000020import com.google.inject.internal.ImmutableList;
21import com.google.inject.internal.Lists;
limpbizkitccb15e42009-04-01 16:33:29 +000022import static com.google.inject.internal.Preconditions.checkState;
limpbizkit7cef5b02009-03-29 21:16:51 +000023import com.google.inject.matcher.Matcher;
24import com.google.inject.matcher.Matchers;
limpbizkit8d620752009-03-31 22:37:26 +000025import com.google.inject.spi.InjectionListener;
26import com.google.inject.spi.Message;
limpbizkita843a952009-04-08 22:24:55 +000027import com.google.inject.spi.TypeEncounter;
limpbizkit7cef5b02009-03-29 21:16:51 +000028import java.lang.reflect.Method;
limpbizkit8d620752009-03-31 22:37:26 +000029import java.util.List;
limpbizkit7cef5b02009-03-29 21:16:51 +000030
31/**
32 * @author jessewilson@google.com (Jesse Wilson)
33 */
limpbizkita843a952009-04-08 22:24:55 +000034final class EncounterImpl<T> implements TypeEncounter<T> {
limpbizkit8d620752009-03-31 22:37:26 +000035
36 private final Errors errors;
37 private final Lookups lookups;
limpbizkitee792462009-04-08 23:48:49 +000038 private List<MembersInjector<? super T>> membersInjectors; // lazy
limpbizkit7cef5b02009-03-29 21:16:51 +000039 private List<InjectionListener<? super T>> injectionListeners; // lazy
limpbizkiteb405132009-04-14 00:50:25 +000040 /*if[AOP]*/
limpbizkit7cef5b02009-03-29 21:16:51 +000041 private List<MethodAspect> aspects; // lazy
limpbizkiteb405132009-04-14 00:50:25 +000042 /*end[AOP]*/
limpbizkitccb15e42009-04-01 16:33:29 +000043 private boolean valid = true;
limpbizkit7cef5b02009-03-29 21:16:51 +000044
limpbizkit8d620752009-03-31 22:37:26 +000045 public EncounterImpl(Errors errors, Lookups lookups) {
46 this.errors = errors;
47 this.lookups = lookups;
48 }
49
limpbizkitccb15e42009-04-01 16:33:29 +000050 public void invalidate() {
51 valid = false;
52 }
53
limpbizkiteb405132009-04-14 00:50:25 +000054 /*if[AOP]*/
limpbizkita843a952009-04-08 22:24:55 +000055 public ImmutableList<MethodAspect> getAspects() {
56 return aspects == null
57 ? ImmutableList.<MethodAspect>of()
58 : ImmutableList.copyOf(aspects);
limpbizkit7cef5b02009-03-29 21:16:51 +000059 }
60
limpbizkiteb405132009-04-14 00:50:25 +000061 public void bindInterceptor(Matcher<? super Method> methodMatcher,
62 org.aopalliance.intercept.MethodInterceptor... interceptors) {
63 checkState(valid, "Encounters may not be used after hear() returns.");
64
65 // make sure the applicable aspects is mutable
66 if (aspects == null) {
67 aspects = Lists.newArrayList();
68 }
69
70 aspects.add(new MethodAspect(Matchers.any(), methodMatcher, interceptors));
71 }
72 /*end[AOP]*/
73
limpbizkitee792462009-04-08 23:48:49 +000074 public ImmutableList<MembersInjector<? super T>> getMembersInjectors() {
75 return membersInjectors == null
76 ? ImmutableList.<MembersInjector<? super T>>of()
77 : ImmutableList.copyOf(membersInjectors);
78 }
79
limpbizkit8d620752009-03-31 22:37:26 +000080 public ImmutableList<InjectionListener<? super T>> getInjectionListeners() {
limpbizkit7cef5b02009-03-29 21:16:51 +000081 return injectionListeners == null
82 ? ImmutableList.<InjectionListener<? super T>>of()
83 : ImmutableList.copyOf(injectionListeners);
84 }
85
limpbizkitee792462009-04-08 23:48:49 +000086 public void register(MembersInjector<? super T> membersInjector) {
87 checkState(valid, "Encounters may not be used after hear() returns.");
88
89 if (membersInjectors == null) {
90 membersInjectors = Lists.newArrayList();
91 }
92
93 membersInjectors.add(membersInjector);
94 }
95
limpbizkit7cef5b02009-03-29 21:16:51 +000096 public void register(InjectionListener<? super T> injectionListener) {
limpbizkitccb15e42009-04-01 16:33:29 +000097 checkState(valid, "Encounters may not be used after hear() returns.");
98
limpbizkit7cef5b02009-03-29 21:16:51 +000099 if (injectionListeners == null) {
100 injectionListeners = Lists.newArrayList();
101 }
102
limpbizkit8d620752009-03-31 22:37:26 +0000103 injectionListeners.add(injectionListener);
limpbizkit7cef5b02009-03-29 21:16:51 +0000104 }
105
limpbizkit7cef5b02009-03-29 21:16:51 +0000106 public void addError(String message, Object... arguments) {
limpbizkitccb15e42009-04-01 16:33:29 +0000107 checkState(valid, "Encounters may not be used after hear() returns.");
limpbizkit8d620752009-03-31 22:37:26 +0000108 errors.addMessage(message, arguments);
limpbizkit7cef5b02009-03-29 21:16:51 +0000109 }
110
111 public void addError(Throwable t) {
limpbizkitccb15e42009-04-01 16:33:29 +0000112 checkState(valid, "Encounters may not be used after hear() returns.");
limpbizkit8d620752009-03-31 22:37:26 +0000113 errors.errorInUserCode(t, "An exception was caught and reported. Message: %s", t.getMessage());
limpbizkit7cef5b02009-03-29 21:16:51 +0000114 }
115
116 public void addError(Message message) {
limpbizkitccb15e42009-04-01 16:33:29 +0000117 checkState(valid, "Encounters may not be used after hear() returns.");
limpbizkit8d620752009-03-31 22:37:26 +0000118 errors.addMessage(message);
limpbizkit7cef5b02009-03-29 21:16:51 +0000119 }
120
121 public <T> Provider<T> getProvider(Key<T> key) {
limpbizkitccb15e42009-04-01 16:33:29 +0000122 checkState(valid, "Encounters may not be used after hear() returns.");
limpbizkit8d620752009-03-31 22:37:26 +0000123 return lookups.getProvider(key);
limpbizkit7cef5b02009-03-29 21:16:51 +0000124 }
125
126 public <T> Provider<T> getProvider(Class<T> type) {
limpbizkit8d620752009-03-31 22:37:26 +0000127 return getProvider(Key.get(type));
limpbizkit7cef5b02009-03-29 21:16:51 +0000128 }
129
130 public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> typeLiteral) {
limpbizkitccb15e42009-04-01 16:33:29 +0000131 checkState(valid, "Encounters may not be used after hear() returns.");
limpbizkit8d620752009-03-31 22:37:26 +0000132 return lookups.getMembersInjector(typeLiteral);
limpbizkit7cef5b02009-03-29 21:16:51 +0000133 }
134
135 public <T> MembersInjector<T> getMembersInjector(Class<T> type) {
limpbizkit8d620752009-03-31 22:37:26 +0000136 return getMembersInjector(TypeLiteral.get(type));
limpbizkit7cef5b02009-03-29 21:16:51 +0000137 }
limpbizkit8d620752009-03-31 22:37:26 +0000138}