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