blob: 132ff544424995c00d5f401144804b3d2548fa24 [file] [log] [blame]
limpbizkit3d58d6b2008-03-08 16:11:47 +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
limpbizkit9dc32d42008-06-15 11:29:10 +000019import com.google.common.collect.Lists;
20import com.google.inject.InjectorImpl.SingleMemberInjector;
limpbizkit3d58d6b2008-03-08 16:11:47 +000021import com.google.inject.commands.RequestStaticInjectionCommand;
limpbizkit9dc32d42008-06-15 11:29:10 +000022import com.google.inject.internal.Errors;
limpbizkit163c48a2008-06-16 02:58:08 +000023import com.google.inject.internal.ErrorsException;
limpbizkit3d58d6b2008-03-08 16:11:47 +000024import java.util.List;
25
26/**
27 * Handles {@link Binder#requestStaticInjection} commands.
28 *
29 * @author crazybob@google.com (Bob Lee)
30 * @author jessewilson@google.com (Jesse Wilson)
31 */
32class RequestStaticInjectionCommandProcessor extends CommandProcessor {
33
limpbizkit9dc32d42008-06-15 11:29:10 +000034 private final List<StaticInjection> staticInjections = Lists.newArrayList();
limpbizkit3d58d6b2008-03-08 16:11:47 +000035
limpbizkit9dc32d42008-06-15 11:29:10 +000036 RequestStaticInjectionCommandProcessor(Errors errors) {
37 super(errors);
limpbizkitad94bcb2008-04-28 00:22:43 +000038 }
39
limpbizkit3d58d6b2008-03-08 16:11:47 +000040 @Override public Boolean visitRequestStaticInjection(RequestStaticInjectionCommand command) {
41 for (Class<?> type : command.getTypes()) {
42 staticInjections.add(new StaticInjection(command.getSource(), type));
43 }
44 return true;
45 }
46
47 public void validate(InjectorImpl injector) {
48 for (StaticInjection staticInjection : staticInjections) {
49 staticInjection.validate(injector);
50 }
51 }
52
53 public void injectMembers(InjectorImpl injector) {
54 for (StaticInjection staticInjection : staticInjections) {
55 staticInjection.injectMembers(injector);
56 }
57 }
58
limpbizkit9dc32d42008-06-15 11:29:10 +000059 /** A requested static injection. */
limpbizkit3d58d6b2008-03-08 16:11:47 +000060 private class StaticInjection {
61 final Object source;
62 final Class<?> type;
limpbizkit9dc32d42008-06-15 11:29:10 +000063 final List<SingleMemberInjector> memberInjectors = Lists.newArrayList();
limpbizkit3d58d6b2008-03-08 16:11:47 +000064
65 public StaticInjection(Object source, Class type) {
66 this.source = source;
67 this.type = type;
68 }
69
70 void validate(final InjectorImpl injector) {
limpbizkit2c2c6102008-06-20 13:34:36 +000071 Errors errorsForMember = errors.withSource(source);
72 injector.addSingleInjectorsForFields(
73 type.getDeclaredFields(), true, memberInjectors, errorsForMember);
74 injector.addSingleInjectorsForMethods(
75 type.getDeclaredMethods(), true, memberInjectors, errorsForMember);
limpbizkit3d58d6b2008-03-08 16:11:47 +000076 }
limpbizkit9dc32d42008-06-15 11:29:10 +000077
78 void injectMembers(InjectorImpl injector) {
79 try {
80 injector.callInContext(new ContextualCallable<Void>() {
81 public Void call(InternalContext context) {
82 for (SingleMemberInjector injector : memberInjectors) {
83 injector.inject(errors, context, null);
84 }
85 return null;
86 }
87 });
limpbizkit163c48a2008-06-16 02:58:08 +000088 } catch (ErrorsException e) {
limpbizkit9dc32d42008-06-15 11:29:10 +000089 throw new AssertionError();
90 }
91 }
limpbizkit3d58d6b2008-03-08 16:11:47 +000092 }
limpbizkit9dc32d42008-06-15 11:29:10 +000093}