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 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame] | 17 | package com.google.inject.internal; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 18 | |
sberlin | ba75f35 | 2011-06-12 21:54:43 +0000 | [diff] [blame] | 19 | import com.google.inject.Key; |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame] | 20 | import com.google.inject.MembersInjector; |
| 21 | import com.google.inject.TypeLiteral; |
sberlin | d9c913a | 2011-06-26 21:02:54 +0000 | [diff] [blame^] | 22 | import com.google.common.collect.ImmutableList; |
| 23 | import com.google.common.collect.ImmutableSet; |
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 | */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame] | 32 | final class MembersInjectorImpl<T> implements MembersInjector<T> { |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 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 { |
sberlin | ba75f35 | 2011-06-12 21:54:43 +0000 | [diff] [blame] | 61 | injectAndNotify(instance, errors, null, typeLiteral, false); |
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 | |
sberlin | ba75f35 | 2011-06-12 21:54:43 +0000 | [diff] [blame] | 69 | void injectAndNotify(final T instance, final Errors errors, |
| 70 | final Key<T> key, final Object source, final boolean toolableOnly) |
| 71 | throws ErrorsException { |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 72 | if (instance == null) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | injector.callInContext(new ContextualCallable<Void>() { |
| 77 | public Void call(InternalContext context) throws ErrorsException { |
sberlin | ba75f35 | 2011-06-12 21:54:43 +0000 | [diff] [blame] | 78 | context.pushState(key, source); |
| 79 | try { |
| 80 | injectMembers(instance, errors, context, toolableOnly); |
| 81 | } finally { |
| 82 | context.popState(); |
| 83 | } |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 84 | return null; |
| 85 | } |
| 86 | }); |
| 87 | |
sberlin | 97c2271 | 2010-02-05 21:12:05 +0000 | [diff] [blame] | 88 | // TODO: We *could* notify listeners too here, |
| 89 | // but it's not clear if we want to. There's no way to know |
| 90 | // if a MembersInjector from the usersMemberInjector list wants |
| 91 | // toolable injections, so do we really want to notify |
| 92 | // about injection? (We could take a strategy of only notifying |
| 93 | // if atleast one InjectionPoint was toolable, in which case |
| 94 | // the above callInContext could return 'true' if it injected |
| 95 | // anything.) |
| 96 | if(!toolableOnly) { |
| 97 | notifyListeners(instance, errors); |
| 98 | } |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void notifyListeners(T instance, Errors errors) throws ErrorsException { |
| 102 | int numErrorsBefore = errors.size(); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 103 | for (InjectionListener<? super T> injectionListener : injectionListeners) { |
| 104 | try { |
| 105 | injectionListener.afterInjection(instance); |
| 106 | } catch (RuntimeException e) { |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 107 | errors.errorNotifyingInjectionListener(injectionListener, typeLiteral, e); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 110 | errors.throwIfNewErrors(numErrorsBefore); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 111 | } |
| 112 | |
sberlin | 97c2271 | 2010-02-05 21:12:05 +0000 | [diff] [blame] | 113 | void injectMembers(T t, Errors errors, InternalContext context, boolean toolableOnly) { |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 114 | // optimization: use manual for/each to save allocating an iterator here |
| 115 | for (int i = 0, size = memberInjectors.size(); i < size; i++) { |
sberlin | 97c2271 | 2010-02-05 21:12:05 +0000 | [diff] [blame] | 116 | SingleMemberInjector injector = memberInjectors.get(i); |
| 117 | if(!toolableOnly || injector.getInjectionPoint().isToolable()) { |
| 118 | injector.inject(errors, context, t); |
| 119 | } |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 120 | } |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 121 | |
sberlin | 97c2271 | 2010-02-05 21:12:05 +0000 | [diff] [blame] | 122 | // TODO: There's no way to know if a user's MembersInjector wants toolable injections. |
| 123 | if(!toolableOnly) { |
limpbizkit | e89c49e | 2009-05-06 01:02:14 +0000 | [diff] [blame] | 124 | // optimization: use manual for/each to save allocating an iterator here |
sberlin | 97c2271 | 2010-02-05 21:12:05 +0000 | [diff] [blame] | 125 | for (int i = 0, size = userMembersInjectors.size(); i < size; i++) { |
| 126 | MembersInjector<? super T> userMembersInjector = userMembersInjectors.get(i); |
| 127 | try { |
| 128 | userMembersInjector.injectMembers(t); |
| 129 | } catch (RuntimeException e) { |
| 130 | errors.errorInUserInjector(userMembersInjector, typeLiteral, e); |
| 131 | } |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override public String toString() { |
| 137 | return "MembersInjector<" + typeLiteral + ">"; |
| 138 | } |
| 139 | |
| 140 | public ImmutableSet<InjectionPoint> getInjectionPoints() { |
| 141 | ImmutableSet.Builder<InjectionPoint> builder = ImmutableSet.builder(); |
| 142 | for (SingleMemberInjector memberInjector : memberInjectors) { |
| 143 | builder.add(memberInjector.getInjectionPoint()); |
| 144 | } |
| 145 | return builder.build(); |
| 146 | } |
| 147 | |
limpbizkit | eb40513 | 2009-04-14 00:50:25 +0000 | [diff] [blame] | 148 | /*if[AOP]*/ |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 149 | public ImmutableList<MethodAspect> getAddedAspects() { |
| 150 | return addedAspects; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 151 | } |
limpbizkit | eb40513 | 2009-04-14 00:50:25 +0000 | [diff] [blame] | 152 | /*end[AOP]*/ |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 153 | } |