blob: 67af1344ed77079080d4a31bfc0103f138eb6222 [file] [log] [blame]
limpbizkit477f9f92008-07-28 07:05:14 +00001/**
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
17package com.google.inject;
18
limpbizkit477f9f92008-07-28 07:05:14 +000019import com.google.inject.internal.Errors;
20import com.google.inject.internal.ErrorsException;
limpbizkit53664a72009-02-21 00:25:27 +000021import com.google.inject.internal.ImmutableList;
limpbizkit76c24b12008-12-25 04:32:41 +000022import com.google.inject.internal.InternalContext;
limpbizkit53664a72009-02-21 00:25:27 +000023import com.google.inject.internal.Lists;
limpbizkit18cb1912008-09-02 18:20:45 +000024import com.google.inject.spi.InjectionPoint;
limpbizkit00ca9f72008-08-02 17:56:17 +000025import com.google.inject.spi.InjectionRequest;
26import com.google.inject.spi.StaticInjectionRequest;
limpbizkit477f9f92008-07-28 07:05:14 +000027import java.util.List;
limpbizkit3a8c1552008-11-16 21:43:01 +000028import java.util.Set;
limpbizkit477f9f92008-07-28 07:05:14 +000029
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 */
limpbizkit00ca9f72008-08-02 17:56:17 +000037class InjectionRequestProcessor extends AbstractProcessor {
limpbizkit477f9f92008-07-28 07:05:14 +000038
39 private final List<StaticInjection> staticInjections = Lists.newArrayList();
limpbizkitfcbdf992008-11-26 02:37:35 +000040 private final Initializer initializer;
limpbizkit477f9f92008-07-28 07:05:14 +000041
limpbizkitfcbdf992008-11-26 02:37:35 +000042 InjectionRequestProcessor(Errors errors, Initializer initializer) {
limpbizkit477f9f92008-07-28 07:05:14 +000043 super(errors);
limpbizkitfcbdf992008-11-26 02:37:35 +000044 this.initializer = initializer;
limpbizkit477f9f92008-07-28 07:05:14 +000045 }
46
limpbizkit03b81a62009-03-18 05:34:39 +000047 @Override public Boolean visit(StaticInjectionRequest request) {
limpbizkitc45600e2008-12-27 02:57:04 +000048 staticInjections.add(new StaticInjection(injector, request));
limpbizkit477f9f92008-07-28 07:05:14 +000049 return true;
50 }
51
limpbizkit03b81a62009-03-18 05:34:39 +000052 @Override public Boolean visit(InjectionRequest request) {
limpbizkit3a8c1552008-11-16 21:43:01 +000053 Set<InjectionPoint> injectionPoints;
limpbizkitb3a8f0b2008-09-05 22:30:40 +000054 try {
limpbizkitc45600e2008-12-27 02:57:04 +000055 injectionPoints = request.getInjectionPoints();
limpbizkitb3a8f0b2008-09-05 22:30:40 +000056 } catch (ConfigurationException e) {
57 errors.merge(e.getErrorMessages());
limpbizkit3a8c1552008-11-16 21:43:01 +000058 injectionPoints = e.getPartialValue();
limpbizkit477f9f92008-07-28 07:05:14 +000059 }
limpbizkitb3a8f0b2008-09-05 22:30:40 +000060
limpbizkitc45600e2008-12-27 02:57:04 +000061 initializer.requestInjection(
62 injector, request.getInstance(), request.getSource(), injectionPoints);
limpbizkit477f9f92008-07-28 07:05:14 +000063 return true;
64 }
65
limpbizkitfcbdf992008-11-26 02:37:35 +000066 public void validate() {
limpbizkit477f9f92008-07-28 07:05:14 +000067 for (StaticInjection staticInjection : staticInjections) {
limpbizkitfcbdf992008-11-26 02:37:35 +000068 staticInjection.validate();
limpbizkit477f9f92008-07-28 07:05:14 +000069 }
70 }
71
limpbizkitfcbdf992008-11-26 02:37:35 +000072 public void injectMembers() {
limpbizkit477f9f92008-07-28 07:05:14 +000073 for (StaticInjection staticInjection : staticInjections) {
limpbizkitfcbdf992008-11-26 02:37:35 +000074 staticInjection.injectMembers();
limpbizkit477f9f92008-07-28 07:05:14 +000075 }
76 }
77
78 /** A requested static injection. */
79 private class StaticInjection {
limpbizkitfcbdf992008-11-26 02:37:35 +000080 final InjectorImpl injector;
limpbizkit477f9f92008-07-28 07:05:14 +000081 final Object source;
limpbizkitc45600e2008-12-27 02:57:04 +000082 final StaticInjectionRequest request;
limpbizkit18cb1912008-09-02 18:20:45 +000083 ImmutableList<SingleMemberInjector> memberInjectors;
limpbizkit477f9f92008-07-28 07:05:14 +000084
limpbizkitc45600e2008-12-27 02:57:04 +000085 public StaticInjection(InjectorImpl injector, StaticInjectionRequest request) {
limpbizkitfcbdf992008-11-26 02:37:35 +000086 this.injector = injector;
limpbizkitc45600e2008-12-27 02:57:04 +000087 this.source = request.getSource();
88 this.request = request;
limpbizkit477f9f92008-07-28 07:05:14 +000089 }
90
limpbizkitfcbdf992008-11-26 02:37:35 +000091 void validate() {
limpbizkit477f9f92008-07-28 07:05:14 +000092 Errors errorsForMember = errors.withSource(source);
limpbizkit3a8c1552008-11-16 21:43:01 +000093 Set<InjectionPoint> injectionPoints;
limpbizkit18cb1912008-09-02 18:20:45 +000094 try {
limpbizkitc45600e2008-12-27 02:57:04 +000095 injectionPoints = request.getInjectionPoints();
limpbizkit18cb1912008-09-02 18:20:45 +000096 } catch (ConfigurationException e) {
97 errors.merge(e.getErrorMessages());
limpbizkit3a8c1552008-11-16 21:43:01 +000098 injectionPoints = e.getPartialValue();
limpbizkit18cb1912008-09-02 18:20:45 +000099 }
limpbizkit7cef5b02009-03-29 21:16:51 +0000100 memberInjectors = injector.membersInjectorStore.getInjectors(
101 injectionPoints, errorsForMember);
limpbizkit477f9f92008-07-28 07:05:14 +0000102 }
103
limpbizkitfcbdf992008-11-26 02:37:35 +0000104 void injectMembers() {
limpbizkit477f9f92008-07-28 07:05:14 +0000105 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}