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 | |
| 19 | import com.google.inject.internal.Errors; |
| 20 | import com.google.inject.internal.ErrorsException; |
| 21 | import com.google.inject.internal.ImmutableList; |
| 22 | import com.google.inject.internal.ImmutableSet; |
| 23 | import com.google.inject.internal.InternalContext; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 24 | import com.google.inject.spi.InjectionListener; |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 25 | import com.google.inject.spi.InjectionPoint; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Injects members of instances of a given type. |
| 29 | * |
| 30 | * @author jessewilson@google.com (Jesse Wilson) |
| 31 | */ |
| 32 | class MembersInjectorImpl<T> implements MembersInjector<T> { |
| 33 | private final TypeLiteral<T> typeLiteral; |
| 34 | private final InjectorImpl injector; |
| 35 | private final ImmutableList<SingleMemberInjector> memberInjectors; |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 36 | private final ImmutableList<MembersInjector<? super T>> userMembersInjectors; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 37 | private final ImmutableList<InjectionListener<? super T>> injectionListeners; |
limpbizkit | eb40513 | 2009-04-14 00:50:25 +0000 | [diff] [blame] | 38 | /*if[AOP]*/ |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 39 | private final ImmutableList<MethodAspect> addedAspects; |
limpbizkit | eb40513 | 2009-04-14 00:50:25 +0000 | [diff] [blame] | 40 | /*end[AOP]*/ |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 41 | |
| 42 | MembersInjectorImpl(InjectorImpl injector, TypeLiteral<T> typeLiteral, |
limpbizkit | eb40513 | 2009-04-14 00:50:25 +0000 | [diff] [blame] | 43 | EncounterImpl<T> encounter, ImmutableList<SingleMemberInjector> memberInjectors) { |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 44 | this.injector = injector; |
| 45 | this.typeLiteral = typeLiteral; |
| 46 | this.memberInjectors = memberInjectors; |
limpbizkit | eb40513 | 2009-04-14 00:50:25 +0000 | [diff] [blame] | 47 | this.userMembersInjectors = encounter.getMembersInjectors(); |
| 48 | this.injectionListeners = encounter.getInjectionListeners(); |
| 49 | /*if[AOP]*/ |
| 50 | this.addedAspects = encounter.getAspects(); |
| 51 | /*end[AOP]*/ |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | public ImmutableList<SingleMemberInjector> getMemberInjectors() { |
| 55 | return memberInjectors; |
| 56 | } |
| 57 | |
| 58 | public void injectMembers(T instance) { |
| 59 | Errors errors = new Errors(typeLiteral); |
| 60 | try { |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 61 | injectAndNotify(instance, errors); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 62 | } catch (ErrorsException e) { |
| 63 | errors.merge(e.getErrors()); |
| 64 | } |
| 65 | |
| 66 | errors.throwProvisionExceptionIfErrorsExist(); |
| 67 | } |
| 68 | |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 69 | void injectAndNotify(final T instance, final Errors errors) throws ErrorsException { |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 70 | if (instance == null) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | injector.callInContext(new ContextualCallable<Void>() { |
| 75 | public Void call(InternalContext context) throws ErrorsException { |
| 76 | injectMembers(instance, errors, context); |
| 77 | return null; |
| 78 | } |
| 79 | }); |
| 80 | |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 81 | notifyListeners(instance, errors); |
| 82 | } |
| 83 | |
| 84 | void notifyListeners(T instance, Errors errors) throws ErrorsException { |
| 85 | int numErrorsBefore = errors.size(); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 86 | for (InjectionListener<? super T> injectionListener : injectionListeners) { |
| 87 | try { |
| 88 | injectionListener.afterInjection(instance); |
| 89 | } catch (RuntimeException e) { |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 90 | errors.errorNotifyingInjectionListener(injectionListener, typeLiteral, e); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 93 | errors.throwIfNewErrors(numErrorsBefore); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void injectMembers(T t, Errors errors, InternalContext context) { |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 97 | // optimization: use manual for/each to save allocating an iterator here |
| 98 | for (int i = 0, size = memberInjectors.size(); i < size; i++) { |
| 99 | memberInjectors.get(i).inject(errors, context, t); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 100 | } |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 101 | |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 102 | // optimization: use manual for/each to save allocating an iterator here |
| 103 | for (int i = 0, size = userMembersInjectors.size(); i < size; i++) { |
| 104 | MembersInjector<? super T> userMembersInjector = userMembersInjectors.get(i); |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 105 | try { |
| 106 | userMembersInjector.injectMembers(t); |
| 107 | } catch (RuntimeException e) { |
| 108 | errors.errorInUserInjector(userMembersInjector, typeLiteral, e); |
| 109 | } |
| 110 | } |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override public String toString() { |
| 114 | return "MembersInjector<" + typeLiteral + ">"; |
| 115 | } |
| 116 | |
| 117 | public ImmutableSet<InjectionPoint> getInjectionPoints() { |
| 118 | ImmutableSet.Builder<InjectionPoint> builder = ImmutableSet.builder(); |
| 119 | for (SingleMemberInjector memberInjector : memberInjectors) { |
| 120 | builder.add(memberInjector.getInjectionPoint()); |
| 121 | } |
| 122 | return builder.build(); |
| 123 | } |
| 124 | |
limpbizkit | eb40513 | 2009-04-14 00:50:25 +0000 | [diff] [blame] | 125 | /*if[AOP]*/ |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 126 | public ImmutableList<MethodAspect> getAddedAspects() { |
| 127 | return addedAspects; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 128 | } |
limpbizkit | eb40513 | 2009-04-14 00:50:25 +0000 | [diff] [blame] | 129 | /*end[AOP]*/ |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 130 | } |