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.FailableCache; |
| 22 | import com.google.inject.internal.ImmutableList; |
| 23 | import com.google.inject.internal.Lists; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 24 | import com.google.inject.spi.InjectionPoint; |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 25 | import com.google.inject.spi.TypeListenerBinding; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 26 | import java.lang.reflect.Field; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 27 | import java.util.List; |
| 28 | import java.util.Set; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Members injectors by type. |
| 32 | * |
| 33 | * @author jessewilson@google.com (Jesse Wilson) |
| 34 | */ |
| 35 | class MembersInjectorStore { |
| 36 | private final InjectorImpl injector; |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 37 | private final ImmutableList<TypeListenerBinding> typeListenerBindings; |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 38 | |
| 39 | private final FailableCache<TypeLiteral<?>, MembersInjectorImpl<?>> cache |
| 40 | = new FailableCache<TypeLiteral<?>, MembersInjectorImpl<?>>() { |
| 41 | @Override protected MembersInjectorImpl<?> create(TypeLiteral<?> type, Errors errors) |
| 42 | throws ErrorsException { |
| 43 | return createWithListeners(type, errors); |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | MembersInjectorStore(InjectorImpl injector, |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 48 | List<TypeListenerBinding> typeListenerBindings) { |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 49 | this.injector = injector; |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 50 | this.typeListenerBindings = ImmutableList.copyOf(typeListenerBindings); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /** |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 54 | * Returns true if any type listeners are installed. Other code may take shortcuts when there |
| 55 | * aren't any type listeners. |
| 56 | */ |
| 57 | public boolean hasTypeListeners() { |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 58 | return !typeListenerBindings.isEmpty(); |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 62 | * Returns a new complete members injector with injection listeners registered. |
| 63 | */ |
| 64 | @SuppressWarnings("unchecked") // the MembersInjector type always agrees with the passed type |
| 65 | public <T> MembersInjectorImpl<T> get(TypeLiteral<T> key, Errors errors) throws ErrorsException { |
| 66 | return (MembersInjectorImpl<T>) cache.get(key, errors); |
| 67 | } |
| 68 | |
| 69 | /** |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 70 | * Creates a new members injector and attaches both injection listeners and method aspects. |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 71 | */ |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 72 | private <T> MembersInjectorImpl<T> createWithListeners(TypeLiteral<T> type, Errors errors) |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 73 | throws ErrorsException { |
| 74 | int numErrorsBefore = errors.size(); |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 75 | |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 76 | Set<InjectionPoint> injectionPoints; |
| 77 | try { |
| 78 | injectionPoints = InjectionPoint.forInstanceMethodsAndFields(type); |
| 79 | } catch (ConfigurationException e) { |
| 80 | errors.merge(e.getErrorMessages()); |
| 81 | injectionPoints = e.getPartialValue(); |
| 82 | } |
| 83 | ImmutableList<SingleMemberInjector> injectors = getInjectors(injectionPoints, errors); |
| 84 | errors.throwIfNewErrors(numErrorsBefore); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 85 | |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 86 | EncounterImpl<T> encounter = new EncounterImpl<T>(errors, injector.lookups); |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 87 | for (TypeListenerBinding typeListener : typeListenerBindings) { |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 88 | if (typeListener.getTypeMatcher().matches(type)) { |
| 89 | try { |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 90 | typeListener.getListener().hear(type, encounter); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 91 | } catch (RuntimeException e) { |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 92 | errors.errorNotifyingTypeListener(typeListener, type, e); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
limpbizkit | ccb15e4 | 2009-04-01 16:33:29 +0000 | [diff] [blame] | 96 | encounter.invalidate(); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 97 | errors.throwIfNewErrors(numErrorsBefore); |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 98 | |
limpbizkit | eb40513 | 2009-04-14 00:50:25 +0000 | [diff] [blame] | 99 | return new MembersInjectorImpl<T>(injector, type, encounter, injectors); |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns the injectors for the specified injection points. |
| 104 | */ |
| 105 | ImmutableList<SingleMemberInjector> getInjectors( |
| 106 | Set<InjectionPoint> injectionPoints, Errors errors) { |
| 107 | List<SingleMemberInjector> injectors = Lists.newArrayList(); |
| 108 | for (InjectionPoint injectionPoint : injectionPoints) { |
| 109 | try { |
| 110 | Errors errorsForMember = injectionPoint.isOptional() |
| 111 | ? new Errors(injectionPoint) |
| 112 | : errors.withSource(injectionPoint); |
| 113 | SingleMemberInjector injector = injectionPoint.getMember() instanceof Field |
| 114 | ? new SingleFieldInjector(this.injector, injectionPoint, errorsForMember) |
| 115 | : new SingleMethodInjector(this.injector, injectionPoint, errorsForMember); |
| 116 | injectors.add(injector); |
| 117 | } catch (ErrorsException ignoredForNow) { |
| 118 | // ignored for now |
| 119 | } |
| 120 | } |
| 121 | return ImmutableList.copyOf(injectors); |
| 122 | } |
| 123 | } |