limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +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.spi; |
| 18 | |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 19 | import com.google.inject.AbstractModule; |
| 20 | import com.google.inject.Binder; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 21 | import com.google.inject.Binding; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 22 | import com.google.inject.Key; |
limpbizkit | 97eac0f | 2009-03-28 18:25:35 +0000 | [diff] [blame] | 23 | import com.google.inject.MembersInjector; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 24 | import com.google.inject.Module; |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 25 | import com.google.inject.PrivateBinder; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 26 | import com.google.inject.PrivateModule; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 27 | import com.google.inject.Provider; |
| 28 | import com.google.inject.Scope; |
| 29 | import com.google.inject.Stage; |
| 30 | import com.google.inject.TypeLiteral; |
| 31 | import com.google.inject.binder.AnnotatedBindingBuilder; |
| 32 | import com.google.inject.binder.AnnotatedConstantBindingBuilder; |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 33 | import com.google.inject.binder.AnnotatedElementBuilder; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 34 | import com.google.inject.internal.AbstractBindingBuilder; |
| 35 | import com.google.inject.internal.BindingBuilder; |
| 36 | import com.google.inject.internal.ConstantBindingBuilderImpl; |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 37 | import com.google.inject.internal.Errors; |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 38 | import com.google.inject.internal.ImmutableList; |
| 39 | import com.google.inject.internal.Lists; |
| 40 | import static com.google.inject.internal.Preconditions.checkArgument; |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 41 | import com.google.inject.internal.PrivateElementsImpl; |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 42 | import com.google.inject.internal.ProviderMethodsModule; |
limpbizkit | 53664a7 | 2009-02-21 00:25:27 +0000 | [diff] [blame] | 43 | import com.google.inject.internal.Sets; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 44 | import com.google.inject.internal.SourceProvider; |
limpbizkit | aa07ab0 | 2009-05-15 07:10:43 +0000 | [diff] [blame] | 45 | import com.google.inject.internal.ExposureBuilder; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 46 | import com.google.inject.matcher.Matcher; |
| 47 | import java.lang.annotation.Annotation; |
| 48 | import java.lang.reflect.Method; |
| 49 | import java.util.Arrays; |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 50 | import java.util.Collection; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 51 | import java.util.Collections; |
| 52 | import java.util.List; |
| 53 | import java.util.Set; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 54 | |
| 55 | /** |
limpbizkit | aa07ab0 | 2009-05-15 07:10:43 +0000 | [diff] [blame] | 56 | * Exposes elements of a module so they can be inspected, validated or {@link |
| 57 | * Element#applyTo(Binder) rewritten}. |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 58 | * |
| 59 | * @author jessewilson@google.com (Jesse Wilson) |
limpbizkit | c489adf | 2008-11-18 07:01:33 +0000 | [diff] [blame] | 60 | * @since 2.0 |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 61 | */ |
| 62 | public final class Elements { |
limpbizkit | afa4b5d | 2008-08-02 18:40:47 +0000 | [diff] [blame] | 63 | private static final BindingTargetVisitor<Object, Object> GET_INSTANCE_VISITOR |
| 64 | = new DefaultBindingTargetVisitor<Object, Object>() { |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 65 | @Override public Object visit(InstanceBinding<?> binding) { |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 66 | return binding.getInstance(); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 67 | } |
| 68 | |
limpbizkit | 8996e80 | 2008-12-28 01:44:29 +0000 | [diff] [blame] | 69 | @Override protected Object visitOther(Binding<?> binding) { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 70 | throw new IllegalArgumentException(); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | /** |
| 75 | * Records the elements executed by {@code modules}. |
| 76 | */ |
| 77 | public static List<Element> getElements(Module... modules) { |
| 78 | return getElements(Stage.DEVELOPMENT, Arrays.asList(modules)); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Records the elements executed by {@code modules}. |
| 83 | */ |
| 84 | public static List<Element> getElements(Stage stage, Module... modules) { |
| 85 | return getElements(stage, Arrays.asList(modules)); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Records the elements executed by {@code modules}. |
| 90 | */ |
| 91 | public static List<Element> getElements(Iterable<? extends Module> modules) { |
| 92 | return getElements(Stage.DEVELOPMENT, modules); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Records the elements executed by {@code modules}. |
| 97 | */ |
| 98 | public static List<Element> getElements(Stage stage, Iterable<? extends Module> modules) { |
| 99 | RecordingBinder binder = new RecordingBinder(stage); |
| 100 | for (Module module : modules) { |
| 101 | binder.install(module); |
| 102 | } |
| 103 | return Collections.unmodifiableList(binder.elements); |
| 104 | } |
| 105 | |
limpbizkit | aa07ab0 | 2009-05-15 07:10:43 +0000 | [diff] [blame] | 106 | /** |
| 107 | * Returns the module composed of {@code elements}. |
| 108 | */ |
| 109 | public static Module getModule(final Iterable<? extends Element> elements) { |
| 110 | return new Module() { |
| 111 | public void configure(Binder binder) { |
| 112 | for (Element element : elements) { |
| 113 | element.applyTo(binder); |
| 114 | } |
| 115 | } |
| 116 | }; |
| 117 | } |
| 118 | |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 119 | @SuppressWarnings("unchecked") |
limpbizkit | 7e1e4f7 | 2008-08-02 21:21:32 +0000 | [diff] [blame] | 120 | static <T> BindingTargetVisitor<T, T> getInstanceVisitor() { |
limpbizkit | afa4b5d | 2008-08-02 18:40:47 +0000 | [diff] [blame] | 121 | return (BindingTargetVisitor<T, T>) GET_INSTANCE_VISITOR; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 122 | } |
| 123 | |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 124 | private static class RecordingBinder implements Binder, PrivateBinder { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 125 | private final Stage stage; |
| 126 | private final Set<Module> modules; |
| 127 | private final List<Element> elements; |
| 128 | private final Object source; |
| 129 | private final SourceProvider sourceProvider; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 130 | |
| 131 | /** The binder where exposed bindings will be created */ |
| 132 | private final RecordingBinder parent; |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 133 | private final PrivateElementsImpl privateElements; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 134 | |
| 135 | private RecordingBinder(Stage stage) { |
| 136 | this.stage = stage; |
| 137 | this.modules = Sets.newHashSet(); |
| 138 | this.elements = Lists.newArrayList(); |
| 139 | this.source = null; |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 140 | this.sourceProvider = SourceProvider.DEFAULT_INSTANCE.plusSkippedClasses( |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 141 | Elements.class, RecordingBinder.class, AbstractModule.class, |
| 142 | ConstantBindingBuilderImpl.class, AbstractBindingBuilder.class, BindingBuilder.class); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 143 | this.parent = null; |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 144 | this.privateElements = null; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 145 | } |
| 146 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 147 | /** Creates a recording binder that's backed by {@code prototype}. */ |
| 148 | private RecordingBinder( |
| 149 | RecordingBinder prototype, Object source, SourceProvider sourceProvider) { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 150 | checkArgument(source == null ^ sourceProvider == null); |
| 151 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 152 | this.stage = prototype.stage; |
| 153 | this.modules = prototype.modules; |
| 154 | this.elements = prototype.elements; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 155 | this.source = source; |
| 156 | this.sourceProvider = sourceProvider; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 157 | this.parent = prototype.parent; |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 158 | this.privateElements = prototype.privateElements; |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** Creates a private recording binder. */ |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 162 | private RecordingBinder(RecordingBinder parent, PrivateElementsImpl privateElements) { |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 163 | this.stage = parent.stage; |
| 164 | this.modules = Sets.newHashSet(); |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 165 | this.elements = privateElements.getElementsMutable(); |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 166 | this.source = parent.source; |
| 167 | this.sourceProvider = parent.sourceProvider; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 168 | this.parent = parent; |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 169 | this.privateElements = privateElements; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 170 | } |
| 171 | |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 172 | /*if[AOP]*/ |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 173 | public void bindInterceptor( |
| 174 | Matcher<? super Class<?>> classMatcher, |
| 175 | Matcher<? super Method> methodMatcher, |
limpbizkit | 4f6274a | 2009-02-19 21:57:55 +0000 | [diff] [blame] | 176 | org.aopalliance.intercept.MethodInterceptor... interceptors) { |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 177 | elements.add(new InterceptorBinding(getSource(), classMatcher, methodMatcher, interceptors)); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 178 | } |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 179 | /*end[AOP]*/ |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 180 | |
| 181 | public void bindScope(Class<? extends Annotation> annotationType, Scope scope) { |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 182 | elements.add(new ScopeBinding(getSource(), annotationType, scope)); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 183 | } |
| 184 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 185 | @SuppressWarnings("unchecked") // it is safe to use the type literal for the raw type |
| 186 | public void requestInjection(Object instance) { |
| 187 | requestInjection((TypeLiteral) TypeLiteral.get(instance.getClass()), instance); |
| 188 | } |
| 189 | |
| 190 | public <T> void requestInjection(TypeLiteral<T> type, T instance) { |
| 191 | elements.add(new InjectionRequest<T>(getSource(), type, instance)); |
| 192 | } |
| 193 | |
limpbizkit | 97eac0f | 2009-03-28 18:25:35 +0000 | [diff] [blame] | 194 | public <T> MembersInjector<T> getMembersInjector(final TypeLiteral<T> typeLiteral) { |
| 195 | final MembersInjectorLookup<T> element |
| 196 | = new MembersInjectorLookup<T>(getSource(), typeLiteral); |
| 197 | elements.add(element); |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 198 | return element.getMembersInjector(); |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | public <T> MembersInjector<T> getMembersInjector(Class<T> type) { |
limpbizkit | 97eac0f | 2009-03-28 18:25:35 +0000 | [diff] [blame] | 202 | return getMembersInjector(TypeLiteral.get(type)); |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 203 | } |
| 204 | |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 205 | public void bindListener(Matcher<? super TypeLiteral<?>> typeMatcher, TypeListener listener) { |
limpbizkit | ee79246 | 2009-04-08 23:48:49 +0000 | [diff] [blame] | 206 | elements.add(new TypeListenerBinding(getSource(), listener, typeMatcher)); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | public void requestStaticInjection(Class<?>... types) { |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 210 | for (Class<?> type : types) { |
| 211 | elements.add(new StaticInjectionRequest(getSource(), type)); |
| 212 | } |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | public void install(Module module) { |
| 216 | if (modules.add(module)) { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 217 | Binder binder = this; |
| 218 | if (module instanceof PrivateModule) { |
| 219 | binder = binder.newPrivateBinder(); |
| 220 | } |
| 221 | |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 222 | try { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 223 | module.configure(binder); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 224 | } catch (RuntimeException e) { |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 225 | Collection<Message> messages = Errors.getMessagesFromThrowable(e); |
| 226 | if (!messages.isEmpty()) { |
| 227 | elements.addAll(messages); |
| 228 | } else { |
| 229 | addError(e); |
| 230 | } |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 231 | } |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 232 | binder.install(ProviderMethodsModule.forModule(module)); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
| 236 | public Stage currentStage() { |
| 237 | return stage; |
| 238 | } |
| 239 | |
| 240 | public void addError(String message, Object... arguments) { |
limpbizkit | b1f42f5 | 2008-11-26 07:44:53 +0000 | [diff] [blame] | 241 | elements.add(new Message(getSource(), Errors.format(message, arguments))); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | public void addError(Throwable t) { |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 245 | String message = "An exception was caught and reported. Message: " + t.getMessage(); |
| 246 | elements.add(new Message(ImmutableList.of(getSource()), message, t)); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | public void addError(Message message) { |
| 250 | elements.add(message); |
| 251 | } |
| 252 | |
limpbizkit | d1fe130 | 2008-08-01 06:40:18 +0000 | [diff] [blame] | 253 | public <T> AnnotatedBindingBuilder<T> bind(Key<T> key) { |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 254 | return new BindingBuilder<T>(this, elements, getSource(), key); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | public <T> AnnotatedBindingBuilder<T> bind(TypeLiteral<T> typeLiteral) { |
| 258 | return bind(Key.get(typeLiteral)); |
| 259 | } |
| 260 | |
| 261 | public <T> AnnotatedBindingBuilder<T> bind(Class<T> type) { |
| 262 | return bind(Key.get(type)); |
| 263 | } |
| 264 | |
| 265 | public AnnotatedConstantBindingBuilder bindConstant() { |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 266 | return new ConstantBindingBuilderImpl<Void>(this, elements, getSource()); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | public <T> Provider<T> getProvider(final Key<T> key) { |
limpbizkit | 97eac0f | 2009-03-28 18:25:35 +0000 | [diff] [blame] | 270 | final ProviderLookup<T> element = new ProviderLookup<T>(getSource(), key); |
| 271 | elements.add(element); |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 272 | return element.getProvider(); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | public <T> Provider<T> getProvider(Class<T> type) { |
| 276 | return getProvider(Key.get(type)); |
| 277 | } |
| 278 | |
| 279 | public void convertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, |
| 280 | TypeConverter converter) { |
limpbizkit | 00ca9f7 | 2008-08-02 17:56:17 +0000 | [diff] [blame] | 281 | elements.add(new TypeConverterBinding(getSource(), typeMatcher, converter)); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 282 | } |
| 283 | |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 284 | public RecordingBinder withSource(final Object source) { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 285 | return new RecordingBinder(this, source, null); |
| 286 | } |
| 287 | |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 288 | public RecordingBinder skipSources(Class... classesToSkip) { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 289 | // if a source is specified explicitly, we don't need to skip sources |
| 290 | if (source != null) { |
| 291 | return this; |
| 292 | } |
| 293 | |
| 294 | SourceProvider newSourceProvider = sourceProvider.plusSkippedClasses(classesToSkip); |
| 295 | return new RecordingBinder(this, null, newSourceProvider); |
| 296 | } |
| 297 | |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 298 | public PrivateBinder newPrivateBinder() { |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 299 | PrivateElementsImpl privateElements = new PrivateElementsImpl(getSource()); |
| 300 | elements.add(privateElements); |
| 301 | return new RecordingBinder(this, privateElements); |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 302 | } |
| 303 | |
limpbizkit | 4994bf6 | 2008-12-27 02:54:59 +0000 | [diff] [blame] | 304 | public void expose(Key<?> key) { |
| 305 | exposeInternal(key); |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | public AnnotatedElementBuilder expose(Class<?> type) { |
| 309 | return exposeInternal(Key.get(type)); |
| 310 | } |
| 311 | |
| 312 | public AnnotatedElementBuilder expose(TypeLiteral<?> type) { |
| 313 | return exposeInternal(Key.get(type)); |
| 314 | } |
| 315 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 316 | private <T> AnnotatedElementBuilder exposeInternal(Key<T> key) { |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 317 | if (privateElements == null) { |
limpbizkit | b1f42f5 | 2008-11-26 07:44:53 +0000 | [diff] [blame] | 318 | addError("Cannot expose %s on a standard binder. " |
| 319 | + "Exposed bindings are only applicable to private binders.", key); |
| 320 | return new AnnotatedElementBuilder() { |
| 321 | public void annotatedWith(Class<? extends Annotation> annotationType) {} |
| 322 | public void annotatedWith(Annotation annotation) {} |
| 323 | }; |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 324 | } |
| 325 | |
limpbizkit | aa07ab0 | 2009-05-15 07:10:43 +0000 | [diff] [blame] | 326 | ExposureBuilder<T> builder = new ExposureBuilder<T>(this, getSource(), key); |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 327 | privateElements.addExposureBuilder(builder); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 328 | return builder; |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 329 | } |
| 330 | |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 331 | protected Object getSource() { |
| 332 | return sourceProvider != null |
| 333 | ? sourceProvider.get() |
| 334 | : source; |
| 335 | } |
| 336 | |
| 337 | @Override public String toString() { |
| 338 | return "Binder"; |
| 339 | } |
| 340 | } |
| 341 | } |