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