blob: f1a1b287ef02e8bfc57a98a718f880cde07b665b [file] [log] [blame]
limpbizkita67e39f2008-02-18 20:10:59 +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.commands;
18
19import com.google.inject.Binder;
20import com.google.inject.Key;
21import com.google.inject.Module;
22import com.google.inject.binder.AnnotatedConstantBindingBuilder;
23import com.google.inject.binder.ConstantBindingBuilder;
24import com.google.inject.binder.LinkedBindingBuilder;
25import com.google.inject.binder.ScopedBindingBuilder;
26import com.google.inject.internal.Objects;
limpbizkit5a72c092008-05-18 02:25:11 +000027import com.google.inject.spi.SourceProviders;
limpbizkita67e39f2008-02-18 20:10:59 +000028import org.aopalliance.intercept.MethodInterceptor;
29
30import java.util.List;
31
32/**
33 * Executes commands against a binder.
34 *
35 * @author jessewilson@google.com (Jesse Wilson)
36 */
37public class CommandReplayer {
38
39 /**
40 * Returns a module that executes the specified commands
41 * using this executing visitor.
42 */
limpbizkit5a72c092008-05-18 02:25:11 +000043 public final Module createModule(final Iterable<Command> commands) {
limpbizkita67e39f2008-02-18 20:10:59 +000044 return new Module() {
45 public void configure(Binder binder) {
46 replay(binder, commands);
47 }
48 };
49 }
50
51 /**
52 * Replays {@code commands} against {@code binder}.
53 */
limpbizkit5a72c092008-05-18 02:25:11 +000054 public final void replay(final Binder binder, Iterable<Command> commands) {
limpbizkita67e39f2008-02-18 20:10:59 +000055 Objects.nonNull(binder, "binder");
56 Objects.nonNull(commands, "commands");
57
58 Command.Visitor<Void> visitor = new Command.Visitor<Void>() {
59 public Void visitAddMessageError(AddMessageErrorCommand command) {
60 replayAddMessageError(binder, command);
61 return null;
62 }
63
64 public Void visitAddError(AddThrowableErrorCommand command) {
65 replayAddError(binder, command);
66 return null;
67 }
68
69 public Void visitBindInterceptor(BindInterceptorCommand command) {
70 replayBindInterceptor(binder, command);
71 return null;
72 }
73
74 public Void visitBindScope(BindScopeCommand command) {
75 replayBindScope(binder, command);
76 return null;
77 }
78
79 public Void visitRequestStaticInjection(RequestStaticInjectionCommand command) {
80 replayRequestStaticInjection(binder, command);
81 return null;
82 }
83
84 public Void visitBindConstant(BindConstantCommand command) {
85 replayBindConstant(binder, command);
86 return null;
87 }
88
89 public Void visitConvertToTypes(ConvertToTypesCommand command) {
90 replayConvertToTypes(binder, command);
91 return null;
92 }
93
94 public <T> Void visitBind(BindCommand<T> command) {
95 replayBind(binder, command);
96 return null;
97 }
98
99 public <T> Void visitGetProvider(GetProviderCommand<T> command) {
100 replayGetProvider(binder, command);
101 return null;
102 }
103 };
104
105 for (Command command : commands) {
106 command.acceptVisitor(visitor);
107 }
108 }
109
limpbizkit3d58d6b2008-03-08 16:11:47 +0000110 public void replayAddMessageError(final Binder binder, final AddMessageErrorCommand command) {
111 SourceProviders.withDefault(command.getSource(), new Runnable() {
112 public void run() {
113 binder.addError(command.getMessage(), command.getArguments().toArray());
114 }
115 });
limpbizkita67e39f2008-02-18 20:10:59 +0000116 }
117
limpbizkit3d58d6b2008-03-08 16:11:47 +0000118 public void replayAddError(final Binder binder, final AddThrowableErrorCommand command) {
119 SourceProviders.withDefault(command.getSource(), new Runnable() {
120 public void run() {
121 binder.addError(command.getThrowable());
122 }
123 });
limpbizkita67e39f2008-02-18 20:10:59 +0000124 }
125
limpbizkit3d58d6b2008-03-08 16:11:47 +0000126 public void replayBindInterceptor(final Binder binder, final BindInterceptorCommand command) {
127 SourceProviders.withDefault(command.getSource(), new Runnable() {
128 public void run() {
129 List<MethodInterceptor> interceptors = command.getInterceptors();
130 binder.bindInterceptor(command.getClassMatcher(), command.getMethodMatcher(),
131 interceptors.toArray(new MethodInterceptor[interceptors.size()]));
132 }
133 });
limpbizkita67e39f2008-02-18 20:10:59 +0000134 }
135
limpbizkit3d58d6b2008-03-08 16:11:47 +0000136 public void replayBindScope(final Binder binder, final BindScopeCommand command) {
137 SourceProviders.withDefault(command.getSource(), new Runnable() {
138 public void run() {
139 binder.bindScope(command.getAnnotationType(), command.getScope());
140 }
141 });
limpbizkita67e39f2008-02-18 20:10:59 +0000142 }
143
limpbizkit3d58d6b2008-03-08 16:11:47 +0000144 public void replayRequestStaticInjection(final Binder binder,
145 final RequestStaticInjectionCommand command) {
146 SourceProviders.withDefault(command.getSource(), new Runnable() {
147 public void run() {
148 List<Class> types = command.getTypes();
149 binder.requestStaticInjection(types.toArray(new Class[types.size()]));
150 }
151 });
limpbizkita67e39f2008-02-18 20:10:59 +0000152 }
153
limpbizkit3d58d6b2008-03-08 16:11:47 +0000154 public void replayBindConstant(final Binder binder, final BindConstantCommand command) {
155 SourceProviders.withDefault(command.getSource(), new Runnable() {
156 public void run() {
157 AnnotatedConstantBindingBuilder constantBindingBuilder = binder.bindConstant();
limpbizkita67e39f2008-02-18 20:10:59 +0000158
limpbizkit3d58d6b2008-03-08 16:11:47 +0000159 Key<Object> key = command.getKey();
160 ConstantBindingBuilder builder = key.getAnnotation() != null
161 ? constantBindingBuilder.annotatedWith(key.getAnnotation())
162 : constantBindingBuilder.annotatedWith(key.getAnnotationType());
limpbizkita67e39f2008-02-18 20:10:59 +0000163
limpbizkit3d58d6b2008-03-08 16:11:47 +0000164 command.getTarget().execute(builder);
165 }
166 });
limpbizkita67e39f2008-02-18 20:10:59 +0000167 }
168
limpbizkit3d58d6b2008-03-08 16:11:47 +0000169 public void replayConvertToTypes(final Binder binder, final ConvertToTypesCommand command) {
170 SourceProviders.withDefault(command.getSource(), new Runnable() {
171 public void run() {
172 binder.convertToTypes(command.getTypeMatcher(), command.getTypeConverter());
173 }
174 });
limpbizkita67e39f2008-02-18 20:10:59 +0000175 }
176
limpbizkit3d58d6b2008-03-08 16:11:47 +0000177 public <T> void replayBind(final Binder binder, final BindCommand<T> command) {
178 SourceProviders.withDefault(command.getSource(), new Runnable() {
179 public void run() {
180 LinkedBindingBuilder<T> lbb = binder.bind(command.getKey());
limpbizkita67e39f2008-02-18 20:10:59 +0000181
limpbizkit3d58d6b2008-03-08 16:11:47 +0000182 BindTarget<T> bindTarget = command.getTarget();
183 ScopedBindingBuilder sbb = bindTarget != null
184 ? bindTarget.execute(lbb)
185 : lbb;
limpbizkita67e39f2008-02-18 20:10:59 +0000186
limpbizkit3d58d6b2008-03-08 16:11:47 +0000187 BindScoping scoping = command.getScoping();
188 if (scoping != null) {
189 scoping.execute(sbb);
190 }
191 }
192 });
limpbizkita67e39f2008-02-18 20:10:59 +0000193 }
194
limpbizkit3d58d6b2008-03-08 16:11:47 +0000195 public <T> void replayGetProvider(final Binder binder, final GetProviderCommand<T> command) {
196 SourceProviders.withDefault(command.getSource(), new Runnable() {
197 public void run() {
198 binder.getProvider(command.getKey());
199 }
200 });
limpbizkita67e39f2008-02-18 20:10:59 +0000201 }
limpbizkita67e39f2008-02-18 20:10:59 +0000202}