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
17package com.google.inject.internal;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Lists;
21import com.google.inject.ConfigurationException;
22import com.google.inject.Stage;
23import com.google.inject.spi.InjectionPoint;
24import com.google.inject.spi.InjectionRequest;
25import com.google.inject.spi.StaticInjectionRequest;
26
27import java.util.List;
28import java.util.Set;
29
30/**
31 * Handles {@code Binder.requestInjection} and {@code 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 */
37final class InjectionRequestProcessor extends AbstractProcessor {
38
39  private final List<StaticInjection> staticInjections = Lists.newArrayList();
40  private final Initializer initializer;
41
42  InjectionRequestProcessor(Errors errors, Initializer initializer) {
43    super(errors);
44    this.initializer = initializer;
45  }
46
47  @Override public Boolean visit(StaticInjectionRequest request) {
48    staticInjections.add(new StaticInjection(injector, request));
49    return true;
50  }
51
52  @Override public Boolean visit(InjectionRequest<?> request) {
53    Set<InjectionPoint> injectionPoints;
54    try {
55      injectionPoints = request.getInjectionPoints();
56    } catch (ConfigurationException e) {
57      errors.merge(e.getErrorMessages());
58      injectionPoints = e.getPartialValue();
59    }
60
61    initializer.requestInjection(
62        injector, request.getInstance(), null, request.getSource(), injectionPoints);
63    return true;
64  }
65
66  void validate() {
67    for (StaticInjection staticInjection : staticInjections) {
68      staticInjection.validate();
69    }
70  }
71
72  void injectMembers() {
73    /*
74     * TODO: If you request both a parent class and one of its
75     * subclasses, the parent class's static members will be
76     * injected twice.
77     */
78    for (StaticInjection staticInjection : staticInjections) {
79      staticInjection.injectMembers();
80    }
81  }
82
83  /** A requested static injection. */
84  private class StaticInjection {
85    final InjectorImpl injector;
86    final Object source;
87    final StaticInjectionRequest request;
88    ImmutableList<SingleMemberInjector> memberInjectors;
89
90    public StaticInjection(InjectorImpl injector, StaticInjectionRequest request) {
91      this.injector = injector;
92      this.source = request.getSource();
93      this.request = request;
94    }
95
96    void validate() {
97      Errors errorsForMember = errors.withSource(source);
98      Set<InjectionPoint> injectionPoints;
99      try {
100        injectionPoints = request.getInjectionPoints();
101      } catch (ConfigurationException e) {
102        errorsForMember.merge(e.getErrorMessages());
103        injectionPoints = e.getPartialValue();
104      }
105      if (injectionPoints != null) {
106        memberInjectors = injector.membersInjectorStore.getInjectors(
107            injectionPoints, errorsForMember);
108      } else {
109        memberInjectors = ImmutableList.of();
110      }
111
112      errors.merge(errorsForMember);
113    }
114
115    void injectMembers() {
116      try {
117        injector.callInContext(new ContextualCallable<Void>() {
118          public Void call(InternalContext context) {
119            for (SingleMemberInjector memberInjector : memberInjectors) {
120              // Run injections if we're not in tool stage (ie, PRODUCTION or DEV),
121              // or if we are in tool stage and the injection point is toolable.
122              if(injector.options.stage != Stage.TOOL || memberInjector.getInjectionPoint().isToolable()) {
123                memberInjector.inject(errors, context, null);
124              }
125            }
126            return null;
127          }
128        });
129      } catch (ErrorsException e) {
130        throw new AssertionError();
131      }
132    }
133  }
134}
135