limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 1 | /** |
| 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 | |
| 17 | package com.google.inject; |
| 18 | |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 19 | import com.google.inject.internal.Errors; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 20 | import com.google.inject.internal.ImmutableList; |
| 21 | import com.google.inject.internal.Lists; |
limpbizkit | ccb15e4 | 2009-04-01 16:33:29 +0000 | [diff] [blame^] | 22 | import static com.google.inject.internal.Preconditions.checkState; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 23 | import com.google.inject.matcher.Matcher; |
| 24 | import com.google.inject.matcher.Matchers; |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 25 | import com.google.inject.spi.InjectableType; |
| 26 | import com.google.inject.spi.InjectionListener; |
| 27 | import com.google.inject.spi.Message; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 28 | import java.lang.reflect.Method; |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 29 | import java.util.List; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 30 | import org.aopalliance.intercept.MethodInterceptor; |
| 31 | |
| 32 | /** |
| 33 | * @author jessewilson@google.com (Jesse Wilson) |
| 34 | */ |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 35 | public final class EncounterImpl<T> implements InjectableType.Encounter<T> { |
| 36 | |
| 37 | private final Errors errors; |
| 38 | private final Lookups lookups; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 39 | private List<InjectionListener<? super T>> injectionListeners; // lazy |
| 40 | private List<MethodAspect> aspects; // lazy |
limpbizkit | ccb15e4 | 2009-04-01 16:33:29 +0000 | [diff] [blame^] | 41 | private boolean valid = true; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 42 | |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 43 | public EncounterImpl(Errors errors, Lookups lookups) { |
| 44 | this.errors = errors; |
| 45 | this.lookups = lookups; |
| 46 | } |
| 47 | |
limpbizkit | ccb15e4 | 2009-04-01 16:33:29 +0000 | [diff] [blame^] | 48 | public void invalidate() { |
| 49 | valid = false; |
| 50 | } |
| 51 | |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 52 | public boolean hasAddedAspects() { |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 53 | 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 | |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 64 | public ImmutableList<InjectionListener<? super T>> getInjectionListeners() { |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 65 | 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) { |
limpbizkit | ccb15e4 | 2009-04-01 16:33:29 +0000 | [diff] [blame^] | 72 | checkState(valid, "Encounters may not be used after hear() returns."); |
| 73 | |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 74 | if (injectionListeners == null) { |
| 75 | injectionListeners = Lists.newArrayList(); |
| 76 | } |
| 77 | |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 78 | injectionListeners.add(injectionListener); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | public void bindInterceptor(Matcher<? super Method> methodMatcher, |
| 82 | MethodInterceptor... interceptors) { |
limpbizkit | ccb15e4 | 2009-04-01 16:33:29 +0000 | [diff] [blame^] | 83 | checkState(valid, "Encounters may not be used after hear() returns."); |
| 84 | |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 85 | // 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) { |
limpbizkit | ccb15e4 | 2009-04-01 16:33:29 +0000 | [diff] [blame^] | 94 | checkState(valid, "Encounters may not be used after hear() returns."); |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 95 | errors.addMessage(message, arguments); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | public void addError(Throwable t) { |
limpbizkit | ccb15e4 | 2009-04-01 16:33:29 +0000 | [diff] [blame^] | 99 | checkState(valid, "Encounters may not be used after hear() returns."); |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 100 | errors.errorInUserCode(t, "An exception was caught and reported. Message: %s", t.getMessage()); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | public void addError(Message message) { |
limpbizkit | ccb15e4 | 2009-04-01 16:33:29 +0000 | [diff] [blame^] | 104 | checkState(valid, "Encounters may not be used after hear() returns."); |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 105 | errors.addMessage(message); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | public <T> Provider<T> getProvider(Key<T> key) { |
limpbizkit | ccb15e4 | 2009-04-01 16:33:29 +0000 | [diff] [blame^] | 109 | checkState(valid, "Encounters may not be used after hear() returns."); |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 110 | return lookups.getProvider(key); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | public <T> Provider<T> getProvider(Class<T> type) { |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 114 | return getProvider(Key.get(type)); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> typeLiteral) { |
limpbizkit | ccb15e4 | 2009-04-01 16:33:29 +0000 | [diff] [blame^] | 118 | checkState(valid, "Encounters may not be used after hear() returns."); |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 119 | return lookups.getMembersInjector(typeLiteral); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | public <T> MembersInjector<T> getMembersInjector(Class<T> type) { |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 123 | return getMembersInjector(TypeLiteral.get(type)); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 124 | } |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 125 | } |