limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2008 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 | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 18 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 19 | import com.google.inject.ConfigurationException; |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 20 | import com.google.inject.spi.InjectionPoint; |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 21 | import com.google.inject.spi.InjectionRequest; |
| 22 | import com.google.inject.spi.StaticInjectionRequest; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 23 | import java.util.List; |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 24 | import java.util.Set; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 25 | |
| 26 | /** |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 27 | * Handles {@code Binder.requestInjection} and {@code Binder.requestStaticInjection} commands. |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 28 | * |
| 29 | * @author crazybob@google.com (Bob Lee) |
| 30 | * @author jessewilson@google.com (Jesse Wilson) |
| 31 | * @author mikeward@google.com (Mike Ward) |
| 32 | */ |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 33 | final class InjectionRequestProcessor extends AbstractProcessor { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 34 | |
| 35 | private final List<StaticInjection> staticInjections = Lists.newArrayList(); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 36 | private final Initializer initializer; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 37 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 38 | InjectionRequestProcessor(Errors errors, Initializer initializer) { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 39 | super(errors); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 40 | this.initializer = initializer; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 41 | } |
| 42 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 43 | @Override public Boolean visit(StaticInjectionRequest request) { |
limpbizkit | c45600e | 2008-12-27 02:57:04 +0000 | [diff] [blame] | 44 | staticInjections.add(new StaticInjection(injector, request)); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 45 | return true; |
| 46 | } |
| 47 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 48 | @Override public Boolean visit(InjectionRequest<?> request) { |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 49 | Set<InjectionPoint> injectionPoints; |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 50 | try { |
limpbizkit | c45600e | 2008-12-27 02:57:04 +0000 | [diff] [blame] | 51 | injectionPoints = request.getInjectionPoints(); |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 52 | } catch (ConfigurationException e) { |
| 53 | errors.merge(e.getErrorMessages()); |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 54 | injectionPoints = e.getPartialValue(); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 55 | } |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 56 | |
limpbizkit | c45600e | 2008-12-27 02:57:04 +0000 | [diff] [blame] | 57 | initializer.requestInjection( |
| 58 | injector, request.getInstance(), request.getSource(), injectionPoints); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 59 | return true; |
| 60 | } |
| 61 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 62 | void validate() { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 63 | for (StaticInjection staticInjection : staticInjections) { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 64 | staticInjection.validate(); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 68 | void injectMembers() { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 69 | for (StaticInjection staticInjection : staticInjections) { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 70 | staticInjection.injectMembers(); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | /** A requested static injection. */ |
| 75 | private class StaticInjection { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 76 | final InjectorImpl injector; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 77 | final Object source; |
limpbizkit | c45600e | 2008-12-27 02:57:04 +0000 | [diff] [blame] | 78 | final StaticInjectionRequest request; |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 79 | ImmutableList<SingleMemberInjector> memberInjectors; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 80 | |
limpbizkit | c45600e | 2008-12-27 02:57:04 +0000 | [diff] [blame] | 81 | public StaticInjection(InjectorImpl injector, StaticInjectionRequest request) { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 82 | this.injector = injector; |
limpbizkit | c45600e | 2008-12-27 02:57:04 +0000 | [diff] [blame] | 83 | this.source = request.getSource(); |
| 84 | this.request = request; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 85 | } |
| 86 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 87 | void validate() { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 88 | Errors errorsForMember = errors.withSource(source); |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 89 | Set<InjectionPoint> injectionPoints; |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 90 | try { |
limpbizkit | c45600e | 2008-12-27 02:57:04 +0000 | [diff] [blame] | 91 | injectionPoints = request.getInjectionPoints(); |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 92 | } catch (ConfigurationException e) { |
| 93 | errors.merge(e.getErrorMessages()); |
limpbizkit | 3a8c155 | 2008-11-16 21:43:01 +0000 | [diff] [blame] | 94 | injectionPoints = e.getPartialValue(); |
limpbizkit | 18cb191 | 2008-09-02 18:20:45 +0000 | [diff] [blame] | 95 | } |
limpbizkit | 7cef5b0 | 2009-03-29 21:16:51 +0000 | [diff] [blame] | 96 | memberInjectors = injector.membersInjectorStore.getInjectors( |
| 97 | injectionPoints, errorsForMember); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 98 | } |
| 99 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 100 | void injectMembers() { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 101 | try { |
| 102 | injector.callInContext(new ContextualCallable<Void>() { |
| 103 | public Void call(InternalContext context) { |
| 104 | for (SingleMemberInjector injector : memberInjectors) { |
| 105 | injector.inject(errors, context, null); |
| 106 | } |
| 107 | return null; |
| 108 | } |
| 109 | }); |
| 110 | } catch (ErrorsException e) { |
| 111 | throw new AssertionError(); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |