blob: edcb3893272b0c52dfc646fbb1a49bae707026c4 [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.InjectableType;
26import com.google.inject.spi.InjectionListener;
27import com.google.inject.spi.Message;
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 +000030import org.aopalliance.intercept.MethodInterceptor;
31
32/**
33 * @author jessewilson@google.com (Jesse Wilson)
34 */
limpbizkit8d620752009-03-31 22:37:26 +000035public final class EncounterImpl<T> implements InjectableType.Encounter<T> {
36
37 private final Errors errors;
38 private final Lookups lookups;
limpbizkit7cef5b02009-03-29 21:16:51 +000039 private List<InjectionListener<? super T>> injectionListeners; // lazy
40 private List<MethodAspect> aspects; // lazy
limpbizkitccb15e42009-04-01 16:33:29 +000041 private boolean valid = true;
limpbizkit7cef5b02009-03-29 21:16:51 +000042
limpbizkit8d620752009-03-31 22:37:26 +000043 public EncounterImpl(Errors errors, Lookups lookups) {
44 this.errors = errors;
45 this.lookups = lookups;
46 }
47
limpbizkitccb15e42009-04-01 16:33:29 +000048 public void invalidate() {
49 valid = false;
50 }
51
limpbizkit8d620752009-03-31 22:37:26 +000052 public boolean hasAddedAspects() {
limpbizkit7cef5b02009-03-29 21:16:51 +000053 return aspects != null;
54 }
55
56 public boolean hasAddedListeners() {
57 return injectionListeners != null;
58 }
59
60 public List<MethodAspect> getAspects() {
61 return aspects;
62 }
63
limpbizkit8d620752009-03-31 22:37:26 +000064 public ImmutableList<InjectionListener<? super T>> getInjectionListeners() {
limpbizkit7cef5b02009-03-29 21:16:51 +000065 return injectionListeners == null
66 ? ImmutableList.<InjectionListener<? super T>>of()
67 : ImmutableList.copyOf(injectionListeners);
68 }
69
70 @SuppressWarnings("unchecked") // an InjectionListener<? super T> is an InjectionListener<T>
71 public void register(InjectionListener<? super T> injectionListener) {
limpbizkitccb15e42009-04-01 16:33:29 +000072 checkState(valid, "Encounters may not be used after hear() returns.");
73
limpbizkit7cef5b02009-03-29 21:16:51 +000074 if (injectionListeners == null) {
75 injectionListeners = Lists.newArrayList();
76 }
77
limpbizkit8d620752009-03-31 22:37:26 +000078 injectionListeners.add(injectionListener);
limpbizkit7cef5b02009-03-29 21:16:51 +000079 }
80
81 public void bindInterceptor(Matcher<? super Method> methodMatcher,
82 MethodInterceptor... interceptors) {
limpbizkitccb15e42009-04-01 16:33:29 +000083 checkState(valid, "Encounters may not be used after hear() returns.");
84
limpbizkit7cef5b02009-03-29 21:16:51 +000085 // make sure the applicable aspects is mutable
86 if (aspects == null) {
87 aspects = Lists.newArrayList();
88 }
89
90 aspects.add(new MethodAspect(Matchers.any(), methodMatcher, interceptors));
91 }
92
93 public void addError(String message, Object... arguments) {
limpbizkitccb15e42009-04-01 16:33:29 +000094 checkState(valid, "Encounters may not be used after hear() returns.");
limpbizkit8d620752009-03-31 22:37:26 +000095 errors.addMessage(message, arguments);
limpbizkit7cef5b02009-03-29 21:16:51 +000096 }
97
98 public void addError(Throwable t) {
limpbizkitccb15e42009-04-01 16:33:29 +000099 checkState(valid, "Encounters may not be used after hear() returns.");
limpbizkit8d620752009-03-31 22:37:26 +0000100 errors.errorInUserCode(t, "An exception was caught and reported. Message: %s", t.getMessage());
limpbizkit7cef5b02009-03-29 21:16:51 +0000101 }
102
103 public void addError(Message message) {
limpbizkitccb15e42009-04-01 16:33:29 +0000104 checkState(valid, "Encounters may not be used after hear() returns.");
limpbizkit8d620752009-03-31 22:37:26 +0000105 errors.addMessage(message);
limpbizkit7cef5b02009-03-29 21:16:51 +0000106 }
107
108 public <T> Provider<T> getProvider(Key<T> key) {
limpbizkitccb15e42009-04-01 16:33:29 +0000109 checkState(valid, "Encounters may not be used after hear() returns.");
limpbizkit8d620752009-03-31 22:37:26 +0000110 return lookups.getProvider(key);
limpbizkit7cef5b02009-03-29 21:16:51 +0000111 }
112
113 public <T> Provider<T> getProvider(Class<T> type) {
limpbizkit8d620752009-03-31 22:37:26 +0000114 return getProvider(Key.get(type));
limpbizkit7cef5b02009-03-29 21:16:51 +0000115 }
116
117 public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> typeLiteral) {
limpbizkitccb15e42009-04-01 16:33:29 +0000118 checkState(valid, "Encounters may not be used after hear() returns.");
limpbizkit8d620752009-03-31 22:37:26 +0000119 return lookups.getMembersInjector(typeLiteral);
limpbizkit7cef5b02009-03-29 21:16:51 +0000120 }
121
122 public <T> MembersInjector<T> getMembersInjector(Class<T> type) {
limpbizkit8d620752009-03-31 22:37:26 +0000123 return getMembersInjector(TypeLiteral.get(type));
limpbizkit7cef5b02009-03-29 21:16:51 +0000124 }
limpbizkit8d620752009-03-31 22:37:26 +0000125}