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