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 | |
sberlin | b7a02b0 | 2011-07-08 00:34:16 +0000 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkArgument; |
Christian Edward Gruber | ba5acdf | 2013-10-05 14:05:39 -0700 | [diff] [blame] | 20 | import static com.google.inject.internal.InternalFlags.getIncludeStackTraceOption; |
sberlin | b7a02b0 | 2011-07-08 00:34:16 +0000 | [diff] [blame] | 21 | |
| 22 | import com.google.common.collect.ImmutableList; |
| 23 | import com.google.common.collect.Lists; |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 24 | import com.google.common.collect.Maps; |
sberlin | b7a02b0 | 2011-07-08 00:34:16 +0000 | [diff] [blame] | 25 | import com.google.common.collect.Sets; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 26 | import com.google.inject.AbstractModule; |
| 27 | import com.google.inject.Binder; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 28 | import com.google.inject.Binding; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 29 | import com.google.inject.Key; |
limpbizkit | 97eac0f | 2009-03-28 18:25:35 +0000 | [diff] [blame] | 30 | import com.google.inject.MembersInjector; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 31 | import com.google.inject.Module; |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 32 | import com.google.inject.PrivateBinder; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 33 | import com.google.inject.PrivateModule; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 34 | import com.google.inject.Provider; |
| 35 | import com.google.inject.Scope; |
| 36 | import com.google.inject.Stage; |
| 37 | import com.google.inject.TypeLiteral; |
| 38 | import com.google.inject.binder.AnnotatedBindingBuilder; |
| 39 | import com.google.inject.binder.AnnotatedConstantBindingBuilder; |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 40 | import com.google.inject.binder.AnnotatedElementBuilder; |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 41 | import com.google.inject.internal.AbstractBindingBuilder; |
| 42 | import com.google.inject.internal.BindingBuilder; |
| 43 | import com.google.inject.internal.ConstantBindingBuilderImpl; |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 44 | import com.google.inject.internal.Errors; |
limpbizkit@gmail.com | 9a227be | 2010-07-03 15:51:31 +0000 | [diff] [blame] | 45 | import com.google.inject.internal.ExposureBuilder; |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 46 | import com.google.inject.internal.InternalFlags.IncludeStackTraceOption; |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 47 | import com.google.inject.internal.PrivateElementsImpl; |
limpbizkit | 86cb3bb | 2008-10-15 21:25:50 +0000 | [diff] [blame] | 48 | import com.google.inject.internal.ProviderMethodsModule; |
limpbizkit@gmail.com | 9a227be | 2010-07-03 15:51:31 +0000 | [diff] [blame] | 49 | import com.google.inject.internal.util.SourceProvider; |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 50 | import com.google.inject.internal.util.StackTraceElements; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 51 | import com.google.inject.matcher.Matcher; |
sberlin | b7a02b0 | 2011-07-08 00:34:16 +0000 | [diff] [blame] | 52 | |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 53 | import java.lang.annotation.Annotation; |
| 54 | import java.lang.reflect.Method; |
| 55 | import java.util.Arrays; |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 56 | import java.util.Collection; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 57 | import java.util.Collections; |
| 58 | import java.util.List; |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 59 | import java.util.Map; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 60 | import java.util.Set; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 61 | |
| 62 | /** |
limpbizkit | aa07ab0 | 2009-05-15 07:10:43 +0000 | [diff] [blame] | 63 | * Exposes elements of a module so they can be inspected, validated or {@link |
| 64 | * Element#applyTo(Binder) rewritten}. |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 65 | * |
| 66 | * @author jessewilson@google.com (Jesse Wilson) |
limpbizkit | c489adf | 2008-11-18 07:01:33 +0000 | [diff] [blame] | 67 | * @since 2.0 |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 68 | */ |
| 69 | public final class Elements { |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 70 | |
limpbizkit | afa4b5d | 2008-08-02 18:40:47 +0000 | [diff] [blame] | 71 | private static final BindingTargetVisitor<Object, Object> GET_INSTANCE_VISITOR |
| 72 | = new DefaultBindingTargetVisitor<Object, Object>() { |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 73 | @Override public Object visit(InstanceBinding<?> binding) { |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 74 | return binding.getInstance(); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 75 | } |
| 76 | |
limpbizkit | 8996e80 | 2008-12-28 01:44:29 +0000 | [diff] [blame] | 77 | @Override protected Object visitOther(Binding<?> binding) { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 78 | throw new IllegalArgumentException(); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | /** |
| 83 | * Records the elements executed by {@code modules}. |
| 84 | */ |
| 85 | public static List<Element> getElements(Module... modules) { |
| 86 | return getElements(Stage.DEVELOPMENT, Arrays.asList(modules)); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Records the elements executed by {@code modules}. |
| 91 | */ |
| 92 | public static List<Element> getElements(Stage stage, Module... modules) { |
| 93 | return getElements(stage, Arrays.asList(modules)); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Records the elements executed by {@code modules}. |
| 98 | */ |
| 99 | public static List<Element> getElements(Iterable<? extends Module> modules) { |
| 100 | return getElements(Stage.DEVELOPMENT, modules); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Records the elements executed by {@code modules}. |
| 105 | */ |
| 106 | public static List<Element> getElements(Stage stage, Iterable<? extends Module> modules) { |
| 107 | RecordingBinder binder = new RecordingBinder(stage); |
| 108 | for (Module module : modules) { |
| 109 | binder.install(module); |
| 110 | } |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 111 | binder.scanForAnnotatedMethods(); |
| 112 | for (RecordingBinder child : binder.privateBinders) { |
| 113 | child.scanForAnnotatedMethods(); |
| 114 | } |
Christian Edward Gruber | ba5acdf | 2013-10-05 14:05:39 -0700 | [diff] [blame] | 115 | // Free the memory consumed by the stack trace elements cache |
| 116 | StackTraceElements.clearCache(); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 117 | return Collections.unmodifiableList(binder.elements); |
| 118 | } |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 119 | |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 120 | private static class ElementsAsModule implements Module { |
| 121 | private final Iterable<? extends Element> elements; |
| 122 | |
| 123 | ElementsAsModule(Iterable<? extends Element> elements) { |
| 124 | this.elements = elements; |
| 125 | } |
| 126 | |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 127 | @Override |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 128 | public void configure(Binder binder) { |
| 129 | for (Element element : elements) { |
| 130 | element.applyTo(binder); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
limpbizkit | aa07ab0 | 2009-05-15 07:10:43 +0000 | [diff] [blame] | 135 | /** |
| 136 | * Returns the module composed of {@code elements}. |
| 137 | */ |
| 138 | public static Module getModule(final Iterable<? extends Element> elements) { |
Christian Edward Gruber | 2e39ef7 | 2013-10-05 14:04:53 -0700 | [diff] [blame] | 139 | return new ElementsAsModule(elements); |
limpbizkit | aa07ab0 | 2009-05-15 07:10:43 +0000 | [diff] [blame] | 140 | } |
| 141 | |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 142 | @SuppressWarnings("unchecked") |
limpbizkit | 7e1e4f7 | 2008-08-02 21:21:32 +0000 | [diff] [blame] | 143 | static <T> BindingTargetVisitor<T, T> getInstanceVisitor() { |
limpbizkit | afa4b5d | 2008-08-02 18:40:47 +0000 | [diff] [blame] | 144 | return (BindingTargetVisitor<T, T>) GET_INSTANCE_VISITOR; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 145 | } |
| 146 | |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 147 | private static class ModuleInfo { |
| 148 | private final Binder binder; |
| 149 | private final ModuleSource moduleSource; |
sameb | 02c489e | 2015-03-20 12:29:45 -0700 | [diff] [blame^] | 150 | private final boolean skipScanning; |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 151 | |
sameb | 02c489e | 2015-03-20 12:29:45 -0700 | [diff] [blame^] | 152 | private ModuleInfo(Binder binder, ModuleSource moduleSource, boolean skipScanning) { |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 153 | this.binder = binder; |
| 154 | this.moduleSource = moduleSource; |
sameb | 02c489e | 2015-03-20 12:29:45 -0700 | [diff] [blame^] | 155 | this.skipScanning = skipScanning; |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Sam Berlin | c34e018 | 2014-08-06 11:56:26 -0400 | [diff] [blame] | 159 | private static class RecordingBinder implements Binder, PrivateBinder { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 160 | private final Stage stage; |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 161 | private final Map<Module, ModuleInfo> modules; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 162 | private final List<Element> elements; |
| 163 | private final Object source; |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 164 | /** The current modules stack */ |
| 165 | private ModuleSource moduleSource = null; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 166 | private final SourceProvider sourceProvider; |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 167 | private final Set<ModuleAnnotatedMethodScanner> scanners; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 168 | |
| 169 | /** The binder where exposed bindings will be created */ |
| 170 | private final RecordingBinder parent; |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 171 | private final PrivateElementsImpl privateElements; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 172 | |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 173 | /** All children private binders, so we can scan through them. */ |
| 174 | private final List<RecordingBinder> privateBinders; |
| 175 | |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 176 | private RecordingBinder(Stage stage) { |
| 177 | this.stage = stage; |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 178 | this.modules = Maps.newLinkedHashMap(); |
| 179 | this.scanners = Sets.newLinkedHashSet(); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 180 | this.elements = Lists.newArrayList(); |
| 181 | this.source = null; |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame] | 182 | this.sourceProvider = SourceProvider.DEFAULT_INSTANCE.plusSkippedClasses( |
limpbizkit | 76c24b1 | 2008-12-25 04:32:41 +0000 | [diff] [blame] | 183 | Elements.class, RecordingBinder.class, AbstractModule.class, |
| 184 | ConstantBindingBuilderImpl.class, AbstractBindingBuilder.class, BindingBuilder.class); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 185 | this.parent = null; |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 186 | this.privateElements = null; |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 187 | this.privateBinders = Lists.newArrayList(); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 188 | } |
| 189 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 190 | /** Creates a recording binder that's backed by {@code prototype}. */ |
| 191 | private RecordingBinder( |
| 192 | RecordingBinder prototype, Object source, SourceProvider sourceProvider) { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 193 | checkArgument(source == null ^ sourceProvider == null); |
| 194 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 195 | this.stage = prototype.stage; |
| 196 | this.modules = prototype.modules; |
| 197 | this.elements = prototype.elements; |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 198 | this.scanners = prototype.scanners; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 199 | this.source = source; |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 200 | this.moduleSource = prototype.moduleSource; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 201 | this.sourceProvider = sourceProvider; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 202 | this.parent = prototype.parent; |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 203 | this.privateElements = prototype.privateElements; |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 204 | this.privateBinders = prototype.privateBinders; |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /** Creates a private recording binder. */ |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 208 | private RecordingBinder(RecordingBinder parent, PrivateElementsImpl privateElements) { |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 209 | this.stage = parent.stage; |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 210 | this.modules = Maps.newLinkedHashMap(); |
| 211 | this.scanners = Sets.newLinkedHashSet(parent.scanners); |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 212 | this.elements = privateElements.getElementsMutable(); |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 213 | this.source = parent.source; |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 214 | this.moduleSource = parent.moduleSource; |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 215 | this.sourceProvider = parent.sourceProvider; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 216 | this.parent = parent; |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 217 | this.privateElements = privateElements; |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 218 | this.privateBinders = parent.privateBinders; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 219 | } |
| 220 | |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 221 | /*if[AOP]*/ |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 222 | @Override |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 223 | public void bindInterceptor( |
| 224 | Matcher<? super Class<?>> classMatcher, |
| 225 | Matcher<? super Method> methodMatcher, |
limpbizkit | 4f6274a | 2009-02-19 21:57:55 +0000 | [diff] [blame] | 226 | org.aopalliance.intercept.MethodInterceptor... interceptors) { |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 227 | elements.add(new InterceptorBinding( |
| 228 | getElementSource(), classMatcher, methodMatcher, interceptors)); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 229 | } |
limpbizkit | bf0d876 | 2009-02-19 09:06:22 +0000 | [diff] [blame] | 230 | /*end[AOP]*/ |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 231 | |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 232 | @Override |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 233 | public void bindScope(Class<? extends Annotation> annotationType, Scope scope) { |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 234 | elements.add(new ScopeBinding(getElementSource(), annotationType, scope)); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 235 | } |
| 236 | |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 237 | @Override |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 238 | @SuppressWarnings("unchecked") // it is safe to use the type literal for the raw type |
| 239 | public void requestInjection(Object instance) { |
Christian Edward Gruber | 96e81ba | 2013-09-18 10:55:21 -0700 | [diff] [blame] | 240 | requestInjection((TypeLiteral<Object>) TypeLiteral.get(instance.getClass()), instance); |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 241 | } |
| 242 | |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 243 | @Override |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 244 | public <T> void requestInjection(TypeLiteral<T> type, T instance) { |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 245 | elements.add(new InjectionRequest<T>(getElementSource(), type, instance)); |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 246 | } |
| 247 | |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 248 | @Override |
limpbizkit | 97eac0f | 2009-03-28 18:25:35 +0000 | [diff] [blame] | 249 | public <T> MembersInjector<T> getMembersInjector(final TypeLiteral<T> typeLiteral) { |
| 250 | final MembersInjectorLookup<T> element |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 251 | = new MembersInjectorLookup<T>(getElementSource(), typeLiteral); |
limpbizkit | 97eac0f | 2009-03-28 18:25:35 +0000 | [diff] [blame] | 252 | elements.add(element); |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 253 | return element.getMembersInjector(); |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | public <T> MembersInjector<T> getMembersInjector(Class<T> type) { |
limpbizkit | 97eac0f | 2009-03-28 18:25:35 +0000 | [diff] [blame] | 257 | return getMembersInjector(TypeLiteral.get(type)); |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 258 | } |
| 259 | |
limpbizkit | a843a95 | 2009-04-08 22:24:55 +0000 | [diff] [blame] | 260 | public void bindListener(Matcher<? super TypeLiteral<?>> typeMatcher, TypeListener listener) { |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 261 | elements.add(new TypeListenerBinding(getElementSource(), listener, typeMatcher)); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 262 | } |
Christian Edward Gruber | ba5acdf | 2013-10-05 14:05:39 -0700 | [diff] [blame] | 263 | |
Sam Berlin | d51292d | 2012-02-26 21:23:19 -0500 | [diff] [blame] | 264 | public void bindListener(Matcher<? super Binding<?>> bindingMatcher, |
| 265 | ProvisionListener... listeners) { |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 266 | elements.add(new ProvisionListenerBinding(getElementSource(), bindingMatcher, listeners)); |
sberlin | 132a5db | 2011-06-05 18:32:05 +0000 | [diff] [blame] | 267 | } |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 268 | |
| 269 | public void requestStaticInjection(Class<?>... types) { |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 270 | for (Class<?> type : types) { |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 271 | elements.add(new StaticInjectionRequest(getElementSource(), type)); |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 272 | } |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 273 | } |
| 274 | |
sameb | 02c489e | 2015-03-20 12:29:45 -0700 | [diff] [blame^] | 275 | /** |
| 276 | * Applies all scanners to the modules we've installed. We skip certain |
| 277 | * PrivateModules because store them in more than one Modules map and only |
| 278 | * want to process them through one of the maps. (They're stored in both |
| 279 | * maps to prevent a module from being installed more than once.) |
| 280 | */ |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 281 | void scanForAnnotatedMethods() { |
| 282 | for (ModuleAnnotatedMethodScanner scanner : scanners) { |
| 283 | // Note: we must iterate over a copy of the modules because calling install(..) |
| 284 | // will mutate modules, otherwise causing a ConcurrentModificationException. |
| 285 | for (Map.Entry<Module, ModuleInfo> entry : Maps.newLinkedHashMap(modules).entrySet()) { |
| 286 | Module module = entry.getKey(); |
sameb | 02c489e | 2015-03-20 12:29:45 -0700 | [diff] [blame^] | 287 | ModuleInfo info = entry.getValue(); |
| 288 | if (info.skipScanning) { |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 289 | continue; |
| 290 | } |
| 291 | moduleSource = entry.getValue().moduleSource; |
| 292 | try { |
sameb | 02c489e | 2015-03-20 12:29:45 -0700 | [diff] [blame^] | 293 | info.binder.install(ProviderMethodsModule.forModule(module, scanner)); |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 294 | } catch(RuntimeException e) { |
| 295 | Collection<Message> messages = Errors.getMessagesFromThrowable(e); |
| 296 | if (!messages.isEmpty()) { |
| 297 | elements.addAll(messages); |
| 298 | } else { |
| 299 | addError(e); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | moduleSource = null; |
| 305 | } |
| 306 | |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 307 | public void install(Module module) { |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 308 | if (!modules.containsKey(module)) { |
sameb | 02c489e | 2015-03-20 12:29:45 -0700 | [diff] [blame^] | 309 | RecordingBinder binder = this; |
sameb | 7445e44 | 2015-02-10 15:37:44 -0800 | [diff] [blame] | 310 | boolean unwrapModuleSource = false; |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 311 | // Update the module source for the new module |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 312 | if (module instanceof ProviderMethodsModule) { |
sameb | 7445e44 | 2015-02-10 15:37:44 -0800 | [diff] [blame] | 313 | // There are two reason's we'd want to get the module source in a ProviderMethodsModule. |
| 314 | // ModuleAnnotatedMethodScanner lets users scan their own modules for @Provides-like |
| 315 | // bindings. If they install the module at a top-level, then moduleSource can be null. |
| 316 | // Also, if they pass something other than 'this' to it, we'd have the wrong source. |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 317 | Object delegate = ((ProviderMethodsModule) module).getDelegateModule(); |
| 318 | if (moduleSource == null |
| 319 | || !moduleSource.getModuleClassName().equals(delegate.getClass().getName())) { |
sameb | 7445e44 | 2015-02-10 15:37:44 -0800 | [diff] [blame] | 320 | moduleSource = getModuleSource(delegate); |
| 321 | unwrapModuleSource = true; |
| 322 | } |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 323 | } else { |
| 324 | moduleSource = getModuleSource(module); |
| 325 | unwrapModuleSource = true; |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 326 | } |
sameb | 02c489e | 2015-03-20 12:29:45 -0700 | [diff] [blame^] | 327 | boolean skipScanning = false; |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 328 | if (module instanceof PrivateModule) { |
sameb | 02c489e | 2015-03-20 12:29:45 -0700 | [diff] [blame^] | 329 | binder = (RecordingBinder) binder.newPrivateBinder(); |
| 330 | // Store the module in the private binder too so we scan for it. |
| 331 | binder.modules.put(module, new ModuleInfo(binder, moduleSource, false)); |
| 332 | skipScanning = true; // don't scan this module in the parent's module set. |
sameb | 7445e44 | 2015-02-10 15:37:44 -0800 | [diff] [blame] | 333 | } |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 334 | // Always store this in the parent binder (even if it was a private module) |
| 335 | // so that we know not to process it again, and so that scanners inherit down. |
sameb | 02c489e | 2015-03-20 12:29:45 -0700 | [diff] [blame^] | 336 | modules.put(module, new ModuleInfo(binder, moduleSource, skipScanning)); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 337 | try { |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 338 | module.configure(binder); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 339 | } catch (RuntimeException e) { |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 340 | Collection<Message> messages = Errors.getMessagesFromThrowable(e); |
| 341 | if (!messages.isEmpty()) { |
| 342 | elements.addAll(messages); |
| 343 | } else { |
| 344 | addError(e); |
| 345 | } |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 346 | } |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 347 | binder.install(ProviderMethodsModule.forModule(module)); |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 348 | // We are done with this module, so undo module source change |
sameb | 7445e44 | 2015-02-10 15:37:44 -0800 | [diff] [blame] | 349 | if (unwrapModuleSource) { |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 350 | moduleSource = moduleSource.getParent(); |
| 351 | } |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
| 355 | public Stage currentStage() { |
| 356 | return stage; |
| 357 | } |
| 358 | |
| 359 | public void addError(String message, Object... arguments) { |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 360 | elements.add(new Message(getElementSource(), Errors.format(message, arguments))); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | public void addError(Throwable t) { |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 364 | String message = "An exception was caught and reported. Message: " + t.getMessage(); |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 365 | elements.add(new Message(ImmutableList.of((Object) getElementSource()), message, t)); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | public void addError(Message message) { |
| 369 | elements.add(message); |
| 370 | } |
| 371 | |
limpbizkit | d1fe130 | 2008-08-01 06:40:18 +0000 | [diff] [blame] | 372 | public <T> AnnotatedBindingBuilder<T> bind(Key<T> key) { |
Christian Edward Gruber | 96e81ba | 2013-09-18 10:55:21 -0700 | [diff] [blame] | 373 | BindingBuilder<T> builder = new BindingBuilder<T>(this, elements, getElementSource(), key); |
Christian Edward Gruber | 96e81ba | 2013-09-18 10:55:21 -0700 | [diff] [blame] | 374 | return builder; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | public <T> AnnotatedBindingBuilder<T> bind(TypeLiteral<T> typeLiteral) { |
| 378 | return bind(Key.get(typeLiteral)); |
| 379 | } |
| 380 | |
| 381 | public <T> AnnotatedBindingBuilder<T> bind(Class<T> type) { |
| 382 | return bind(Key.get(type)); |
| 383 | } |
| 384 | |
| 385 | public AnnotatedConstantBindingBuilder bindConstant() { |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 386 | return new ConstantBindingBuilderImpl<Void>(this, elements, getElementSource()); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | public <T> Provider<T> getProvider(final Key<T> key) { |
sameb | 9867f9c | 2015-02-02 12:45:25 -0800 | [diff] [blame] | 390 | return getProvider(Dependency.get(key)); |
| 391 | } |
| 392 | |
| 393 | public <T> Provider<T> getProvider(final Dependency<T> dependency) { |
| 394 | final ProviderLookup<T> element = new ProviderLookup<T>(getElementSource(), dependency); |
limpbizkit | 97eac0f | 2009-03-28 18:25:35 +0000 | [diff] [blame] | 395 | elements.add(element); |
limpbizkit | 8d62075 | 2009-03-31 22:37:26 +0000 | [diff] [blame] | 396 | return element.getProvider(); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | public <T> Provider<T> getProvider(Class<T> type) { |
| 400 | return getProvider(Key.get(type)); |
| 401 | } |
| 402 | |
| 403 | public void convertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, |
| 404 | TypeConverter converter) { |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 405 | elements.add(new TypeConverterBinding(getElementSource(), typeMatcher, converter)); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Christian Edward Gruber | ba5acdf | 2013-10-05 14:05:39 -0700 | [diff] [blame] | 408 | public RecordingBinder withSource(final Object source) { |
Sam Berlin | c5848c0 | 2013-12-06 17:05:44 -0500 | [diff] [blame] | 409 | return source == this.source ? this : new RecordingBinder(this, source, null); |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 410 | } |
| 411 | |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 412 | public RecordingBinder skipSources(Class... classesToSkip) { |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 413 | // if a source is specified explicitly, we don't need to skip sources |
| 414 | if (source != null) { |
| 415 | return this; |
| 416 | } |
| 417 | |
| 418 | SourceProvider newSourceProvider = sourceProvider.plusSkippedClasses(classesToSkip); |
| 419 | return new RecordingBinder(this, null, newSourceProvider); |
| 420 | } |
| 421 | |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 422 | @Override |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 423 | public PrivateBinder newPrivateBinder() { |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 424 | PrivateElementsImpl privateElements = new PrivateElementsImpl(getElementSource()); |
Christian Edward Gruber | 96e81ba | 2013-09-18 10:55:21 -0700 | [diff] [blame] | 425 | RecordingBinder binder = new RecordingBinder(this, privateElements); |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 426 | privateBinders.add(binder); |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 427 | elements.add(privateElements); |
Christian Edward Gruber | 96e81ba | 2013-09-18 10:55:21 -0700 | [diff] [blame] | 428 | return binder; |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 429 | } |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 430 | |
| 431 | @Override |
sberlin | 8b64d45 | 2010-12-13 02:44:36 +0000 | [diff] [blame] | 432 | public void disableCircularProxies() { |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 433 | elements.add(new DisableCircularProxiesOption(getElementSource())); |
sberlin | 8b64d45 | 2010-12-13 02:44:36 +0000 | [diff] [blame] | 434 | } |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 435 | |
| 436 | @Override |
sberlin | 8b64d45 | 2010-12-13 02:44:36 +0000 | [diff] [blame] | 437 | public void requireExplicitBindings() { |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 438 | elements.add(new RequireExplicitBindingsOption(getElementSource())); |
sberlin | 8b64d45 | 2010-12-13 02:44:36 +0000 | [diff] [blame] | 439 | } |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 440 | |
| 441 | @Override |
Sam Berlin | c756777 | 2012-05-31 19:54:04 -0400 | [diff] [blame] | 442 | public void requireAtInjectOnConstructors() { |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 443 | elements.add(new RequireAtInjectOnConstructorsOption(getElementSource())); |
Sam Berlin | c756777 | 2012-05-31 19:54:04 -0400 | [diff] [blame] | 444 | } |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 445 | |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 446 | @Override |
Christian Edward Gruber | e391585 | 2013-05-16 11:00:54 -0700 | [diff] [blame] | 447 | public void requireExactBindingAnnotations() { |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 448 | elements.add(new RequireExactBindingAnnotationsOption(getElementSource())); |
Christian Edward Gruber | e391585 | 2013-05-16 11:00:54 -0700 | [diff] [blame] | 449 | } |
| 450 | |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 451 | @Override |
sameb | 54da0e3 | 2015-02-19 18:36:52 -0800 | [diff] [blame] | 452 | public void scanModulesForAnnotatedMethods(ModuleAnnotatedMethodScanner scanner) { |
| 453 | scanners.add(scanner); |
| 454 | elements.add(new ModuleAnnotatedMethodScannerBinding(getElementSource(), scanner)); |
| 455 | } |
| 456 | |
limpbizkit | 4994bf6 | 2008-12-27 02:54:59 +0000 | [diff] [blame] | 457 | public void expose(Key<?> key) { |
| 458 | exposeInternal(key); |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 459 | } |
| 460 | |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 461 | @Override |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 462 | public AnnotatedElementBuilder expose(Class<?> type) { |
| 463 | return exposeInternal(Key.get(type)); |
| 464 | } |
| 465 | |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 466 | @Override |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 467 | public AnnotatedElementBuilder expose(TypeLiteral<?> type) { |
| 468 | return exposeInternal(Key.get(type)); |
| 469 | } |
| 470 | |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 471 | private <T> AnnotatedElementBuilder exposeInternal(Key<T> key) { |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 472 | if (privateElements == null) { |
limpbizkit | b1f42f5 | 2008-11-26 07:44:53 +0000 | [diff] [blame] | 473 | addError("Cannot expose %s on a standard binder. " |
| 474 | + "Exposed bindings are only applicable to private binders.", key); |
| 475 | return new AnnotatedElementBuilder() { |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 476 | @Override |
limpbizkit | b1f42f5 | 2008-11-26 07:44:53 +0000 | [diff] [blame] | 477 | public void annotatedWith(Class<? extends Annotation> annotationType) {} |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 478 | @Override |
limpbizkit | b1f42f5 | 2008-11-26 07:44:53 +0000 | [diff] [blame] | 479 | public void annotatedWith(Annotation annotation) {} |
| 480 | }; |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 483 | ExposureBuilder<T> builder = new ExposureBuilder<T>(this, getElementSource(), key); |
limpbizkit | c3f9284 | 2008-12-30 19:43:47 +0000 | [diff] [blame] | 484 | privateElements.addExposureBuilder(builder); |
limpbizkit | fcbdf99 | 2008-11-26 02:37:35 +0000 | [diff] [blame] | 485 | return builder; |
limpbizkit | 5ea4ab2 | 2008-11-25 08:43:49 +0000 | [diff] [blame] | 486 | } |
| 487 | |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 488 | private ModuleSource getModuleSource(Object module) { |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 489 | StackTraceElement[] partialCallStack; |
Christian Edward Gruber | ba5acdf | 2013-10-05 14:05:39 -0700 | [diff] [blame] | 490 | if (getIncludeStackTraceOption() == IncludeStackTraceOption.COMPLETE) { |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 491 | partialCallStack = getPartialCallStack(new Throwable().getStackTrace()); |
Christian Edward Gruber | ba5acdf | 2013-10-05 14:05:39 -0700 | [diff] [blame] | 492 | } else { |
| 493 | partialCallStack = new StackTraceElement[0]; |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 494 | } |
| 495 | if (moduleSource == null) { |
| 496 | return new ModuleSource(module, partialCallStack); |
| 497 | } |
| 498 | return moduleSource.createChild(module, partialCallStack); |
| 499 | } |
| 500 | |
| 501 | private ElementSource getElementSource() { |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 502 | // Full call stack |
Christian Edward Gruber | ba5acdf | 2013-10-05 14:05:39 -0700 | [diff] [blame] | 503 | StackTraceElement[] callStack = null; |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 504 | // The call stack starts from current top module configure and ends at this method caller |
Christian Edward Gruber | ba5acdf | 2013-10-05 14:05:39 -0700 | [diff] [blame] | 505 | StackTraceElement[] partialCallStack = new StackTraceElement[0]; |
| 506 | // The element original source |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 507 | ElementSource originalSource = null; |
Christian Edward Gruber | ba5acdf | 2013-10-05 14:05:39 -0700 | [diff] [blame] | 508 | // The element declaring source |
| 509 | Object declaringSource = source; |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 510 | if (declaringSource instanceof ElementSource) { |
| 511 | originalSource = (ElementSource) declaringSource; |
| 512 | declaringSource = originalSource.getDeclaringSource(); |
| 513 | } |
Christian Edward Gruber | ba5acdf | 2013-10-05 14:05:39 -0700 | [diff] [blame] | 514 | IncludeStackTraceOption stackTraceOption = getIncludeStackTraceOption(); |
| 515 | if (stackTraceOption == IncludeStackTraceOption.COMPLETE || |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 516 | (stackTraceOption == IncludeStackTraceOption.ONLY_FOR_DECLARING_SOURCE |
Christian Edward Gruber | ba5acdf | 2013-10-05 14:05:39 -0700 | [diff] [blame] | 517 | && declaringSource == null)) { |
| 518 | callStack = new Throwable().getStackTrace(); |
| 519 | } |
| 520 | if (stackTraceOption == IncludeStackTraceOption.COMPLETE) { |
| 521 | partialCallStack = getPartialCallStack(callStack); |
| 522 | } |
| 523 | if (declaringSource == null) { |
| 524 | // So 'source' and 'originalSource' are null otherwise declaringSource has some value |
| 525 | if (stackTraceOption == IncludeStackTraceOption.COMPLETE || |
| 526 | stackTraceOption == IncludeStackTraceOption.ONLY_FOR_DECLARING_SOURCE) { |
| 527 | // With the above conditions and assignments 'callStack' is non-null |
| 528 | declaringSource = sourceProvider.get(callStack); |
| 529 | } else { // or if (stackTraceOption == IncludeStackTraceOptions.OFF) |
| 530 | // As neither 'declaring source' nor 'call stack' is available use 'module source' |
| 531 | declaringSource = sourceProvider.getFromClassNames(moduleSource.getModuleClassNames()); |
| 532 | } |
| 533 | } |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 534 | // Build the binding call stack |
| 535 | return new ElementSource( |
| 536 | originalSource, declaringSource, moduleSource, partialCallStack); |
| 537 | } |
| 538 | |
| 539 | /** |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 540 | * Removes the {@link #moduleSource} call stack from the beginning of current call stack. It |
| 541 | * also removes the last two elements in order to make {@link #install(Module)} the last call |
| 542 | * in the call stack. |
Christian Edward Gruber | 605bd08 | 2013-08-09 11:04:00 -0700 | [diff] [blame] | 543 | */ |
| 544 | private StackTraceElement[] getPartialCallStack(StackTraceElement[] callStack) { |
| 545 | int toSkip = 0; |
| 546 | if (moduleSource != null) { |
| 547 | toSkip = moduleSource.getStackTraceSize(); |
| 548 | } |
| 549 | // -1 for skipping 'getModuleSource' and 'getElementSource' calls |
| 550 | int chunkSize = callStack.length - toSkip - 1; |
| 551 | |
| 552 | StackTraceElement[] partialCallStack = new StackTraceElement[chunkSize]; |
| 553 | System.arraycopy(callStack, 1, partialCallStack, 0, chunkSize); |
| 554 | return partialCallStack; |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 555 | } |
cgruber | 0910c1e | 2015-02-18 12:07:13 -0800 | [diff] [blame] | 556 | |
limpbizkit | 477f9f9 | 2008-07-28 07:05:14 +0000 | [diff] [blame] | 557 | @Override public String toString() { |
| 558 | return "Binder"; |
| 559 | } |
| 560 | } |
| 561 | } |