Latest API tweaks on Recorder, Replayer
git-svn-id: https://google-guice.googlecode.com/svn/trunk@402 d779f126-a31b-0410-b53b-1d3aecad763e
diff --git a/extensions/visitable/src/com/google/inject/visitable/CommandReplayer.java b/extensions/visitable/src/com/google/inject/visitable/CommandReplayer.java
index f74cb2f..a943779 100644
--- a/extensions/visitable/src/com/google/inject/visitable/CommandReplayer.java
+++ b/extensions/visitable/src/com/google/inject/visitable/CommandReplayer.java
@@ -39,7 +39,7 @@
/**
* Returns the current binder in use.
*/
- protected Binder getBinder() {
+ protected Binder binder() {
if (binder == null) {
throw new IllegalStateException("No binder exists at this time!");
}
@@ -78,35 +78,35 @@
}
public Void visitAddMessageError(AddMessageErrorCommand command) {
- getBinder().addError(command.getMessage(), command.getArguments().toArray());
+ binder().addError(command.getMessage(), command.getArguments().toArray());
return null;
}
public Void visitAddError(AddThrowableErrorCommand command) {
- getBinder().addError(command.getThrowable());
+ binder().addError(command.getThrowable());
return null;
}
public Void visitBindInterceptor(BindInterceptorCommand command) {
List<MethodInterceptor> interceptors = command.getInterceptors();
- getBinder().bindInterceptor(command.getClassMatcher(), command.getMethodMatcher(),
+ binder().bindInterceptor(command.getClassMatcher(), command.getMethodMatcher(),
interceptors.toArray(new MethodInterceptor[interceptors.size()]));
return null;
}
public Void visitBindScope(BindScopeCommand command) {
- getBinder().bindScope(command.getAnnotationType(), command.getScope());
+ binder().bindScope(command.getAnnotationType(), command.getScope());
return null;
}
public Void visitRequestStaticInjection(RequestStaticInjectionCommand command) {
List<Class> types = command.getTypes();
- getBinder().requestStaticInjection(types.toArray(new Class[types.size()]));
+ binder().requestStaticInjection(types.toArray(new Class[types.size()]));
return null;
}
public Void visitConstantBinding(BindConstantCommand command) {
- AnnotatedConstantBindingBuilder constantBindingBuilder = getBinder().bindConstant();
+ AnnotatedConstantBindingBuilder constantBindingBuilder = binder().bindConstant();
Key<Object> key = command.getKey();
ConstantBindingBuilder builder = key.getAnnotation() != null
@@ -118,12 +118,12 @@
}
public Void visitConvertToTypes(ConvertToTypesCommand command) {
- getBinder().convertToTypes(command.getTypeMatcher(), command.getTypeConverter());
+ binder().convertToTypes(command.getTypeMatcher(), command.getTypeConverter());
return null;
}
public <T> Void visitBinding(BindCommand<T> command) {
- LinkedBindingBuilder<T> lbb = getBinder().bind(command.getKey());
+ LinkedBindingBuilder<T> lbb = binder().bind(command.getKey());
BindTarget<T> bindTarget = command.getTarget();
ScopedBindingBuilder sbb = bindTarget != null
@@ -139,7 +139,7 @@
}
public <T> Void visitGetProviderCommand(GetProviderCommand<T> command) {
- getBinder().getProvider(command.getKey());
+ binder().getProvider(command.getKey());
return null;
}
}
diff --git a/extensions/visitable/src/com/google/inject/visitable/intercepting/InterceptingInjectorBuilder.java b/extensions/visitable/src/com/google/inject/visitable/intercepting/InterceptingInjectorBuilder.java
index 6d95102..eb63977 100644
--- a/extensions/visitable/src/com/google/inject/visitable/intercepting/InterceptingInjectorBuilder.java
+++ b/extensions/visitable/src/com/google/inject/visitable/intercepting/InterceptingInjectorBuilder.java
@@ -109,9 +109,9 @@
}
Key<T> anonymousKey = Key.get(key.getTypeLiteral(), uniqueAnnotation());
- getBinder().bind(key).toProvider(new InterceptingProvider<T>(key, anonymousKey));
+ binder().bind(key).toProvider(new InterceptingProvider<T>(key, anonymousKey));
- LinkedBindingBuilder<T> linkedBindingBuilder = getBinder().bind(anonymousKey);
+ LinkedBindingBuilder<T> linkedBindingBuilder = binder().bind(anonymousKey);
ScopedBindingBuilder scopedBindingBuilder = command.getTarget().execute(linkedBindingBuilder);
BindScoping scoping = command.getScoping();
diff --git a/extensions/visitable/test/com/google/inject/visitable/CommandRewriteTest.java b/extensions/visitable/test/com/google/inject/visitable/CommandRewriteTest.java
new file mode 100644
index 0000000..ea86c50
--- /dev/null
+++ b/extensions/visitable/test/com/google/inject/visitable/CommandRewriteTest.java
@@ -0,0 +1,66 @@
+/**
+ * Copyright (C) 2008 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.inject.visitable;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.Module;
+import junit.framework.TestCase;
+
+import java.util.List;
+
+
+/**
+ * @author jessewilson@google.com (Jesse Wilson)
+ */
+public class CommandRewriteTest extends TestCase {
+
+ public void testRewriteBindings() {
+ // create a module the binds String.class and CharSequence.class
+ Module module = new AbstractModule() {
+ protected void configure() {
+ bind(String.class).toInstance("Pizza");
+ bind(CharSequence.class).toInstance("Wine");
+ }
+ };
+
+ // record the commands from that module
+ CommandRecorder commandRecorder = new CommandRecorder(new FutureInjector());
+ List<Command> commands = commandRecorder.recordCommands(module);
+
+ // create a rewriter that rewrites the binding to 'Wine' with a binding to 'Beer'
+ CommandReplayer rewriter = new CommandReplayer() {
+ @Override public <T> Void visitBinding(BindCommand<T> command) {
+ if ("Wine".equals(command.getTarget().get(null))) {
+ binder().bind(CharSequence.class).toInstance("Beer");
+ } else {
+ super.visitBinding(command);
+ }
+ return null;
+ }
+ };
+
+ // create a module from the original list of commands and the rewriter
+ Module rewrittenModule = rewriter.createModule(commands);
+
+ // it all works
+ Injector injector = Guice.createInjector(rewrittenModule);
+ assertEquals("Pizza", injector.getInstance(String.class));
+ assertEquals("Beer", injector.getInstance(CharSequence.class));
+ }
+}