limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 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 | |
| 17 | package com.google.inject.commands; |
| 18 | |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame^] | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
limpbizkit | 185d2a2 | 2008-06-16 07:22:47 +0000 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
| 21 | import com.google.common.collect.Sets; |
| 22 | import com.google.inject.Binder; |
| 23 | import com.google.inject.Key; |
| 24 | import com.google.inject.Module; |
| 25 | import com.google.inject.Provider; |
| 26 | import com.google.inject.Scope; |
| 27 | import com.google.inject.Stage; |
| 28 | import com.google.inject.TypeLiteral; |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 29 | import com.google.inject.binder.AnnotatedBindingBuilder; |
| 30 | import com.google.inject.binder.AnnotatedConstantBindingBuilder; |
| 31 | import com.google.inject.matcher.Matcher; |
limpbizkit | 185d2a2 | 2008-06-16 07:22:47 +0000 | [diff] [blame] | 32 | import com.google.inject.spi.Message; |
limpbizkit | d6967b9 | 2008-05-16 15:28:51 +0000 | [diff] [blame] | 33 | import com.google.inject.spi.SourceProviders; |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 34 | import com.google.inject.spi.TypeConverter; |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 35 | import java.lang.annotation.Annotation; |
| 36 | import java.lang.reflect.Method; |
limpbizkit | 185d2a2 | 2008-06-16 07:22:47 +0000 | [diff] [blame] | 37 | import java.util.Arrays; |
| 38 | import java.util.Collections; |
| 39 | import java.util.List; |
| 40 | import java.util.Set; |
| 41 | import org.aopalliance.intercept.MethodInterceptor; |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Records commands executed by a module so they can be inspected or |
| 45 | * {@link CommandReplayer replayed}. |
| 46 | * |
| 47 | * @author jessewilson@google.com (Jesse Wilson) |
| 48 | */ |
| 49 | public final class CommandRecorder { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 50 | private Stage currentStage = Stage.DEVELOPMENT; |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 51 | private final EarlyRequestsProvider earlyRequestsProvider; |
| 52 | |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 53 | static { |
| 54 | SourceProviders.skip(RecordingBinder.class); |
| 55 | } |
| 56 | |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 57 | /** |
| 58 | * @param earlyRequestsProvider satisfies requests to |
| 59 | * {@link Binder#getProvider} at module execution time. For modules that |
| 60 | * will be used to create an injector, use {@link FutureInjector}. |
| 61 | */ |
| 62 | public CommandRecorder(EarlyRequestsProvider earlyRequestsProvider) { |
| 63 | this.earlyRequestsProvider = earlyRequestsProvider; |
| 64 | } |
| 65 | |
| 66 | /** |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 67 | * Sets the stage reported by the binder. |
| 68 | */ |
| 69 | public void setCurrentStage(Stage currentStage) { |
| 70 | this.currentStage = currentStage; |
| 71 | } |
| 72 | |
| 73 | /** |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 74 | * Records the commands executed by {@code modules}. |
| 75 | */ |
| 76 | public List<Command> recordCommands(Module... modules) { |
| 77 | return recordCommands(Arrays.asList(modules)); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Records the commands executed by {@code modules}. |
| 82 | */ |
| 83 | public List<Command> recordCommands(Iterable<Module> modules) { |
| 84 | RecordingBinder binder = new RecordingBinder(); |
| 85 | for (Module module : modules) { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 86 | binder.install(module); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 87 | } |
| 88 | return Collections.unmodifiableList(binder.commands); |
| 89 | } |
| 90 | |
| 91 | private class RecordingBinder implements Binder { |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame^] | 92 | private final Set<Module> modules; |
| 93 | private final List<Command> commands; |
| 94 | |
| 95 | private RecordingBinder() { |
| 96 | modules = Sets.newHashSet(); |
| 97 | commands = Lists.newArrayList(); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Creates a recording binder that's backed by the same configuration as |
| 102 | * {@code backingBinder}. |
| 103 | */ |
| 104 | private RecordingBinder(RecordingBinder backingBinder) { |
| 105 | modules = backingBinder.modules; |
| 106 | commands = backingBinder.commands; |
| 107 | } |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 108 | |
| 109 | public void bindInterceptor( |
| 110 | Matcher<? super Class<?>> classMatcher, |
| 111 | Matcher<? super Method> methodMatcher, |
| 112 | MethodInterceptor... interceptors) { |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame^] | 113 | commands.add(new BindInterceptorCommand(getSource(), classMatcher, methodMatcher, interceptors)); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | public void bindScope(Class<? extends Annotation> annotationType, Scope scope) { |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame^] | 117 | commands.add(new BindScopeCommand(getSource(), annotationType, scope)); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | public void requestStaticInjection(Class<?>... types) { |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame^] | 121 | commands.add(new RequestStaticInjectionCommand(getSource(), types)); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | public void install(Module module) { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 125 | if (modules.add(module)) { |
| 126 | module.configure(this); |
| 127 | } |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | public Stage currentStage() { |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 131 | return currentStage; |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | public void addError(String message, Object... arguments) { |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame^] | 135 | commands.add(new AddMessageErrorCommand(getSource(), message, arguments)); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | public void addError(Throwable t) { |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame^] | 139 | commands.add(new AddThrowableErrorCommand(getSource(), t)); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 140 | } |
| 141 | |
limpbizkit | 185d2a2 | 2008-06-16 07:22:47 +0000 | [diff] [blame] | 142 | public void addError(Message message) { |
| 143 | throw new UnsupportedOperationException("TODO"); |
| 144 | } |
| 145 | |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 146 | public <T> BindCommand<T>.BindingBuilder bind(Key<T> key) { |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame^] | 147 | BindCommand<T> bindCommand = new BindCommand<T>(getSource(), key); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 148 | commands.add(bindCommand); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 149 | return bindCommand.bindingBuilder(RecordingBinder.this); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | public <T> AnnotatedBindingBuilder<T> bind(TypeLiteral<T> typeLiteral) { |
| 153 | return bind(Key.get(typeLiteral)); |
| 154 | } |
| 155 | |
| 156 | public <T> AnnotatedBindingBuilder<T> bind(Class<T> type) { |
| 157 | return bind(Key.get(type)); |
| 158 | } |
| 159 | |
| 160 | public AnnotatedConstantBindingBuilder bindConstant() { |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame^] | 161 | BindConstantCommand bindConstantCommand = new BindConstantCommand(getSource()); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 162 | commands.add(bindConstantCommand); |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 163 | return bindConstantCommand.bindingBuilder(RecordingBinder.this); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | public <T> Provider<T> getProvider(final Key<T> key) { |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame^] | 167 | commands.add(new GetProviderCommand<T>(getSource(), key, earlyRequestsProvider)); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 168 | return new Provider<T>() { |
| 169 | public T get() { |
| 170 | return earlyRequestsProvider.get(key); |
| 171 | } |
limpbizkit | d6967b9 | 2008-05-16 15:28:51 +0000 | [diff] [blame] | 172 | |
| 173 | @Override public String toString() { |
| 174 | return "Provider<" + key.getTypeLiteral() + ">"; |
| 175 | } |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 176 | }; |
| 177 | } |
| 178 | |
| 179 | public <T> Provider<T> getProvider(Class<T> type) { |
| 180 | return getProvider(Key.get(type)); |
| 181 | } |
| 182 | |
| 183 | public void convertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, |
limpbizkit | 3d58d6b | 2008-03-08 16:11:47 +0000 | [diff] [blame] | 184 | TypeConverter converter) { |
limpbizkit | 7d9991e | 2008-06-17 02:49:18 +0000 | [diff] [blame^] | 185 | commands.add(new ConvertToTypesCommand(getSource(), typeMatcher, converter)); |
| 186 | } |
| 187 | |
| 188 | public Binder withSource(final Object source) { |
| 189 | checkNotNull(source, "source"); |
| 190 | |
| 191 | return new RecordingBinder(this) { |
| 192 | @Override protected Object getSource() { |
| 193 | return source; |
| 194 | } |
| 195 | }; |
| 196 | } |
| 197 | |
| 198 | protected Object getSource() { |
| 199 | return SourceProviders.defaultSource(); |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 200 | } |
limpbizkit | d6967b9 | 2008-05-16 15:28:51 +0000 | [diff] [blame] | 201 | |
| 202 | @Override public String toString() { |
| 203 | return "Binder"; |
| 204 | } |
limpbizkit | a67e39f | 2008-02-18 20:10:59 +0000 | [diff] [blame] | 205 | } |
| 206 | } |