blob: aa98fbcf26e71cf501e8c78752f1e705126114fd [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
limpbizkit5ae41eb2009-06-06 17:51:27 +000017package com.google.inject.internal;
limpbizkit477f9f92008-07-28 07:05:14 +000018
limpbizkit5ae41eb2009-06-06 17:51:27 +000019import com.google.inject.ConfigurationException;
limpbizkit18cb1912008-09-02 18:20:45 +000020import com.google.inject.spi.InjectionPoint;
limpbizkit00ca9f72008-08-02 17:56:17 +000021import com.google.inject.spi.InjectionRequest;
22import com.google.inject.spi.StaticInjectionRequest;
limpbizkit477f9f92008-07-28 07:05:14 +000023import java.util.List;
limpbizkit3a8c1552008-11-16 21:43:01 +000024import java.util.Set;
limpbizkit477f9f92008-07-28 07:05:14 +000025
26/**
limpbizkit5ae41eb2009-06-06 17:51:27 +000027 * Handles {@code Binder.requestInjection} and {@code Binder.requestStaticInjection} commands.
limpbizkit477f9f92008-07-28 07:05:14 +000028 *
29 * @author crazybob@google.com (Bob Lee)
30 * @author jessewilson@google.com (Jesse Wilson)
31 * @author mikeward@google.com (Mike Ward)
32 */
limpbizkit5ae41eb2009-06-06 17:51:27 +000033final class InjectionRequestProcessor extends AbstractProcessor {
limpbizkit477f9f92008-07-28 07:05:14 +000034
35 private final List<StaticInjection> staticInjections = Lists.newArrayList();
limpbizkitfcbdf992008-11-26 02:37:35 +000036 private final Initializer initializer;
limpbizkit477f9f92008-07-28 07:05:14 +000037
limpbizkitfcbdf992008-11-26 02:37:35 +000038 InjectionRequestProcessor(Errors errors, Initializer initializer) {
limpbizkit477f9f92008-07-28 07:05:14 +000039 super(errors);
limpbizkitfcbdf992008-11-26 02:37:35 +000040 this.initializer = initializer;
limpbizkit477f9f92008-07-28 07:05:14 +000041 }
42
limpbizkit03b81a62009-03-18 05:34:39 +000043 @Override public Boolean visit(StaticInjectionRequest request) {
limpbizkitc45600e2008-12-27 02:57:04 +000044 staticInjections.add(new StaticInjection(injector, request));
limpbizkit477f9f92008-07-28 07:05:14 +000045 return true;
46 }
47
limpbizkit5ae41eb2009-06-06 17:51:27 +000048 @Override public Boolean visit(InjectionRequest<?> request) {
limpbizkit3a8c1552008-11-16 21:43:01 +000049 Set<InjectionPoint> injectionPoints;
limpbizkitb3a8f0b2008-09-05 22:30:40 +000050 try {
limpbizkitc45600e2008-12-27 02:57:04 +000051 injectionPoints = request.getInjectionPoints();
limpbizkitb3a8f0b2008-09-05 22:30:40 +000052 } catch (ConfigurationException e) {
53 errors.merge(e.getErrorMessages());
limpbizkit3a8c1552008-11-16 21:43:01 +000054 injectionPoints = e.getPartialValue();
limpbizkit477f9f92008-07-28 07:05:14 +000055 }
limpbizkitb3a8f0b2008-09-05 22:30:40 +000056
limpbizkitc45600e2008-12-27 02:57:04 +000057 initializer.requestInjection(
58 injector, request.getInstance(), request.getSource(), injectionPoints);
limpbizkit477f9f92008-07-28 07:05:14 +000059 return true;
60 }
61
limpbizkit5ae41eb2009-06-06 17:51:27 +000062 void validate() {
limpbizkit477f9f92008-07-28 07:05:14 +000063 for (StaticInjection staticInjection : staticInjections) {
limpbizkitfcbdf992008-11-26 02:37:35 +000064 staticInjection.validate();
limpbizkit477f9f92008-07-28 07:05:14 +000065 }
66 }
67
limpbizkit5ae41eb2009-06-06 17:51:27 +000068 void injectMembers() {
limpbizkit477f9f92008-07-28 07:05:14 +000069 for (StaticInjection staticInjection : staticInjections) {
limpbizkitfcbdf992008-11-26 02:37:35 +000070 staticInjection.injectMembers();
limpbizkit477f9f92008-07-28 07:05:14 +000071 }
72 }
73
74 /** A requested static injection. */
75 private class StaticInjection {
limpbizkitfcbdf992008-11-26 02:37:35 +000076 final InjectorImpl injector;
limpbizkit477f9f92008-07-28 07:05:14 +000077 final Object source;
limpbizkitc45600e2008-12-27 02:57:04 +000078 final StaticInjectionRequest request;
limpbizkit18cb1912008-09-02 18:20:45 +000079 ImmutableList<SingleMemberInjector> memberInjectors;
limpbizkit477f9f92008-07-28 07:05:14 +000080
limpbizkitc45600e2008-12-27 02:57:04 +000081 public StaticInjection(InjectorImpl injector, StaticInjectionRequest request) {
limpbizkitfcbdf992008-11-26 02:37:35 +000082 this.injector = injector;
limpbizkitc45600e2008-12-27 02:57:04 +000083 this.source = request.getSource();
84 this.request = request;
limpbizkit477f9f92008-07-28 07:05:14 +000085 }
86
limpbizkitfcbdf992008-11-26 02:37:35 +000087 void validate() {
limpbizkit477f9f92008-07-28 07:05:14 +000088 Errors errorsForMember = errors.withSource(source);
limpbizkit3a8c1552008-11-16 21:43:01 +000089 Set<InjectionPoint> injectionPoints;
limpbizkit18cb1912008-09-02 18:20:45 +000090 try {
limpbizkitc45600e2008-12-27 02:57:04 +000091 injectionPoints = request.getInjectionPoints();
limpbizkit18cb1912008-09-02 18:20:45 +000092 } catch (ConfigurationException e) {
93 errors.merge(e.getErrorMessages());
limpbizkit3a8c1552008-11-16 21:43:01 +000094 injectionPoints = e.getPartialValue();
limpbizkit18cb1912008-09-02 18:20:45 +000095 }
limpbizkit7cef5b02009-03-29 21:16:51 +000096 memberInjectors = injector.membersInjectorStore.getInjectors(
97 injectionPoints, errorsForMember);
limpbizkit477f9f92008-07-28 07:05:14 +000098 }
99
limpbizkitfcbdf992008-11-26 02:37:35 +0000100 void injectMembers() {
limpbizkit477f9f92008-07-28 07:05:14 +0000101 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}