blob: e883cd6ac90810f7b8285ed781927a2433c2a443 [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;
23import com.google.inject.internal.ResolveFailedException;
24import com.google.inject.spi.SourceProviders;
limpbizkit3d58d6b2008-03-08 16:11:47 +000025import java.util.List;
26
27/**
28 * Handles {@link Binder#requestStaticInjection} commands.
29 *
30 * @author crazybob@google.com (Bob Lee)
31 * @author jessewilson@google.com (Jesse Wilson)
32 */
33class RequestStaticInjectionCommandProcessor extends CommandProcessor {
34
limpbizkit9dc32d42008-06-15 11:29:10 +000035 private final List<StaticInjection> staticInjections = Lists.newArrayList();
limpbizkit3d58d6b2008-03-08 16:11:47 +000036
limpbizkit9dc32d42008-06-15 11:29:10 +000037 RequestStaticInjectionCommandProcessor(Errors errors) {
38 super(errors);
limpbizkitad94bcb2008-04-28 00:22:43 +000039 }
40
limpbizkit3d58d6b2008-03-08 16:11:47 +000041 @Override public Boolean visitRequestStaticInjection(RequestStaticInjectionCommand command) {
42 for (Class<?> type : command.getTypes()) {
43 staticInjections.add(new StaticInjection(command.getSource(), type));
44 }
45 return true;
46 }
47
48 public void validate(InjectorImpl injector) {
49 for (StaticInjection staticInjection : staticInjections) {
50 staticInjection.validate(injector);
51 }
52 }
53
54 public void injectMembers(InjectorImpl injector) {
55 for (StaticInjection staticInjection : staticInjections) {
56 staticInjection.injectMembers(injector);
57 }
58 }
59
limpbizkit9dc32d42008-06-15 11:29:10 +000060 /** A requested static injection. */
limpbizkit3d58d6b2008-03-08 16:11:47 +000061 private class StaticInjection {
62 final Object source;
63 final Class<?> type;
limpbizkit9dc32d42008-06-15 11:29:10 +000064 final List<SingleMemberInjector> memberInjectors = Lists.newArrayList();
limpbizkit3d58d6b2008-03-08 16:11:47 +000065
66 public StaticInjection(Object source, Class type) {
67 this.source = source;
68 this.type = type;
69 }
70
71 void validate(final InjectorImpl injector) {
limpbizkit9dc32d42008-06-15 11:29:10 +000072 SourceProviders.withDefault(source, new Runnable() {
73 public void run() {
74 injector.addSingleInjectorsForFields(
75 type.getDeclaredFields(), true, memberInjectors, errors);
76 injector.addSingleInjectorsForMethods(
77 type.getDeclaredMethods(), true, memberInjectors, errors);
limpbizkit3d58d6b2008-03-08 16:11:47 +000078 }
79 });
80 }
limpbizkit9dc32d42008-06-15 11:29:10 +000081
82 void injectMembers(InjectorImpl injector) {
83 try {
84 injector.callInContext(new ContextualCallable<Void>() {
85 public Void call(InternalContext context) {
86 for (SingleMemberInjector injector : memberInjectors) {
87 injector.inject(errors, context, null);
88 }
89 return null;
90 }
91 });
92 } catch (ResolveFailedException e) {
93 throw new AssertionError();
94 }
95 }
limpbizkit3d58d6b2008-03-08 16:11:47 +000096 }
limpbizkit9dc32d42008-06-15 11:29:10 +000097}