forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathModuleDescriptor.java
More file actions
292 lines (262 loc) · 13.3 KB
/
ModuleDescriptor.java
File metadata and controls
292 lines (262 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* Copyright (C) 2015 The Dagger Authors.
*
* 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 dagger.internal.codegen.binding;
import static com.google.common.base.CaseFormat.LOWER_CAMEL;
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
import static com.google.common.base.Verify.verify;
import static com.google.common.collect.Collections2.transform;
import static dagger.internal.codegen.base.ModuleAnnotation.moduleAnnotation;
import static dagger.internal.codegen.base.Util.reentrantComputeIfAbsent;
import static dagger.internal.codegen.binding.SourceFiles.classFileName;
import static dagger.internal.codegen.extension.DaggerCollectors.toOptional;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import static dagger.internal.codegen.xprocessing.XElements.asMethod;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
import static dagger.internal.codegen.xprocessing.XTypes.isDeclared;
import androidx.room3.compiler.codegen.XClassName;
import androidx.room3.compiler.codegen.XTypeName;
import androidx.room3.compiler.processing.XMethodElement;
import androidx.room3.compiler.processing.XProcessingEnv;
import androidx.room3.compiler.processing.XType;
import androidx.room3.compiler.processing.XTypeElement;
import com.google.auto.value.AutoValue;
import com.google.auto.value.extension.memoized.Memoized;
import com.google.common.collect.ImmutableSet;
import com.google.common.graph.Traverser;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import dagger.Binds;
import dagger.BindsOptionalOf;
import dagger.Module;
import dagger.internal.codegen.base.ClearableCache;
import dagger.internal.codegen.base.DaggerSuperficialValidation;
import dagger.internal.codegen.base.ModuleKind;
import dagger.internal.codegen.model.Key;
import dagger.internal.codegen.xprocessing.XTypeElements;
import dagger.internal.codegen.xprocessing.XTypeNames;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
/** Contains metadata that describes a module. */
@AutoValue
public abstract class ModuleDescriptor {
public abstract XTypeElement moduleElement();
public abstract ImmutableSet<ContributionBinding> bindings();
/** The multibinding declarations contained in this module. */
abstract ImmutableSet<MultibindingDeclaration> multibindingDeclarations();
/** The {@link Module#subcomponents() subcomponent declarations} contained in this module. */
abstract ImmutableSet<SubcomponentDeclaration> subcomponentDeclarations();
/** The {@link Binds} method declarations that define delegate bindings. */
abstract ImmutableSet<DelegateDeclaration> delegateDeclarations();
/** The {@link BindsOptionalOf} method declarations that define optional bindings. */
abstract ImmutableSet<OptionalBindingDeclaration> optionalDeclarations();
/** The kind of the module. */
public abstract ModuleKind kind();
/** Whether the module is implicitly included rather than directly referenced in annotation. */
public abstract boolean isImplicitlyIncluded();
/** Returns all of the bindings declared in this module. */
@Memoized
public ImmutableSet<Declaration> allBindingDeclarations() {
return ImmutableSet.<Declaration>builder()
.addAll(bindings())
.addAll(delegateDeclarations())
.addAll(multibindingDeclarations())
.addAll(optionalDeclarations())
.addAll(subcomponentDeclarations())
.build();
}
/** Returns the keys of all bindings declared by this module. */
ImmutableSet<Key> allBindingKeys() {
return allBindingDeclarations().stream().map(Declaration::key).collect(toImmutableSet());
}
/** A {@link ModuleDescriptor} factory. */
@Singleton
public static final class Factory implements ClearableCache {
private final XProcessingEnv processingEnv;
private final BindingFactory bindingFactory;
private final MultibindingDeclaration.Factory multibindingDeclarationFactory;
private final DelegateDeclaration.Factory bindingDelegateDeclarationFactory;
private final SubcomponentDeclaration.Factory subcomponentDeclarationFactory;
private final OptionalBindingDeclaration.Factory optionalBindingDeclarationFactory;
private final DaggerSuperficialValidation superficialValidation;
private final Map<XTypeElement, ModuleDescriptor> cache = new HashMap<>();
private final Set<XTypeElement> implicitlyIncludedModules = new LinkedHashSet<>();
@Inject
Factory(
XProcessingEnv processingEnv,
BindingFactory bindingFactory,
MultibindingDeclaration.Factory multibindingDeclarationFactory,
DelegateDeclaration.Factory bindingDelegateDeclarationFactory,
SubcomponentDeclaration.Factory subcomponentDeclarationFactory,
OptionalBindingDeclaration.Factory optionalBindingDeclarationFactory,
DaggerSuperficialValidation superficialValidation) {
this.processingEnv = processingEnv;
this.bindingFactory = bindingFactory;
this.multibindingDeclarationFactory = multibindingDeclarationFactory;
this.bindingDelegateDeclarationFactory = bindingDelegateDeclarationFactory;
this.subcomponentDeclarationFactory = subcomponentDeclarationFactory;
this.optionalBindingDeclarationFactory = optionalBindingDeclarationFactory;
this.superficialValidation = superficialValidation;
}
public ModuleDescriptor create(XTypeElement moduleElement) {
return reentrantComputeIfAbsent(cache, moduleElement, this::createUncached);
}
public ModuleDescriptor createUncached(XTypeElement moduleElement) {
ImmutableSet.Builder<ContributionBinding> bindings = ImmutableSet.builder();
ImmutableSet.Builder<DelegateDeclaration> delegates = ImmutableSet.builder();
ImmutableSet.Builder<MultibindingDeclaration> multibindingDeclarations =
ImmutableSet.builder();
ImmutableSet.Builder<OptionalBindingDeclaration> optionalDeclarations =
ImmutableSet.builder();
XTypeElements.getAllMethods(moduleElement)
.forEach(
moduleMethod -> {
if (moduleMethod.hasAnnotation(XTypeNames.PROVIDES)) {
bindings.add(bindingFactory.providesMethodBinding(moduleMethod, moduleElement));
}
if (moduleMethod.hasAnnotation(XTypeNames.PRODUCES)) {
bindings.add(bindingFactory.producesMethodBinding(moduleMethod, moduleElement));
}
if (DelegateBinding.hasDelegateAnnotation(moduleMethod)) {
if (moduleMethod.hasAnnotation(XTypeNames.BINDS)
&& moduleMethod.getParameters().isEmpty()) {
// Parameterless @Binds methods are treated as explicit InjectionBindings
// to avoid duplicate binding errors and cyclical dependencies that arise
// if they were modeled as DelegateBindings on the same key as the @Inject
// constructor.
bindingFactory
.explicitInjectionBinding(moduleMethod, moduleElement)
.ifPresent(bindings::add);
} else {
delegates.add(
bindingDelegateDeclarationFactory.create(moduleMethod, moduleElement));
}
}
if (moduleMethod.hasAnnotation(XTypeNames.MULTIBINDS)) {
multibindingDeclarations.add(
multibindingDeclarationFactory.forMultibindsMethod(
moduleMethod, moduleElement));
}
if (moduleMethod.hasAnnotation(XTypeNames.BINDS_OPTIONAL_OF)) {
optionalDeclarations.add(
optionalBindingDeclarationFactory.forMethod(moduleMethod, moduleElement));
}
});
moduleElement.getEnclosedTypeElements().stream()
.filter(XTypeElement::isCompanionObject)
.collect(toOptional())
.ifPresent(companionModule -> collectCompanionModuleBindings(companionModule, bindings));
return new AutoValue_ModuleDescriptor(
moduleElement,
bindings.build(),
multibindingDeclarations.build(),
subcomponentDeclarationFactory.forModule(moduleElement),
delegates.build(),
optionalDeclarations.build(),
ModuleKind.forAnnotatedElement(moduleElement).get(),
implicitlyIncludedModules.contains(moduleElement));
}
private void collectCompanionModuleBindings(
XTypeElement companionModule, ImmutableSet.Builder<ContributionBinding> bindings) {
ImmutableSet<String> bindingElementDescriptors =
bindings.build().stream()
.map(binding -> asMethod(binding.bindingElement().get()).getJvmDescriptor())
.collect(toImmutableSet());
XTypeElements.getAllMethods(companionModule).stream()
// Binding methods in companion objects with @JvmStatic are mirrored in the enclosing
// class, therefore we should ignore it or else it'll be a duplicate binding.
.filter(method -> !method.hasAnnotation(XTypeNames.JVM_STATIC))
// Fallback strategy for de-duping contributing bindings in the companion module with
// @JvmStatic by comparing descriptors. Contributing bindings are the only valid bindings
// a companion module can declare. See: https://youtrack.jetbrains.com/issue/KT-35104
// TODO(danysantiago): Checks qualifiers too.
.filter(method -> !bindingElementDescriptors.contains(method.getJvmDescriptor()))
.forEach(
method -> {
if (method.hasAnnotation(XTypeNames.PROVIDES)) {
bindings.add(bindingFactory.providesMethodBinding(method, companionModule));
}
if (method.hasAnnotation(XTypeNames.PRODUCES)) {
bindings.add(bindingFactory.producesMethodBinding(method, companionModule));
}
});
}
/** Returns all the modules transitively included by given modules, including the arguments. */
ImmutableSet<ModuleDescriptor> transitiveModules(Collection<XTypeElement> modules) {
// Traverse as a graph to automatically handle modules with cyclic includes.
return ImmutableSet.copyOf(
Traverser.forGraph(
(ModuleDescriptor module) -> transform(includedModules(module), this::create))
.depthFirstPreOrder(transform(modules, this::create)));
}
private ImmutableSet<XTypeElement> includedModules(ModuleDescriptor moduleDescriptor) {
return ImmutableSet.copyOf(
collectIncludedModules(new LinkedHashSet<>(), moduleDescriptor.moduleElement()));
}
@CanIgnoreReturnValue
private Set<XTypeElement> collectIncludedModules(
Set<XTypeElement> includedModules, XTypeElement moduleElement) {
XType superclass = moduleElement.getSuperType();
if (superclass != null) {
verify(isDeclared(superclass));
if (!superclass.asTypeName().equals(XTypeName.ANY_OBJECT)) {
collectIncludedModules(includedModules, superclass.getTypeElement());
}
}
moduleAnnotation(moduleElement, superficialValidation)
.ifPresent(
moduleAnnotation -> {
includedModules.addAll(moduleAnnotation.includes());
ImmutableSet<XTypeElement> daggerAndroidModules =
implicitlyIncludedModules(moduleElement);
includedModules.addAll(daggerAndroidModules);
implicitlyIncludedModules.addAll(daggerAndroidModules);
});
return includedModules;
}
private static final XClassName CONTRIBUTES_ANDROID_INJECTOR =
XClassName.get("dagger.android", "ContributesAndroidInjector");
// @ContributesAndroidInjector generates a module that is implicitly included in the enclosing
// module
private ImmutableSet<XTypeElement> implicitlyIncludedModules(XTypeElement module) {
if (processingEnv.findTypeElement(CONTRIBUTES_ANDROID_INJECTOR) == null) {
return ImmutableSet.of();
}
return module.getDeclaredMethods().stream()
.filter(method -> method.hasAnnotation(CONTRIBUTES_ANDROID_INJECTOR))
.map(
method ->
DaggerSuperficialValidation.requireTypeElement(
processingEnv, implicitlyIncludedModuleName(module, method)))
.collect(toImmutableSet());
}
private XClassName implicitlyIncludedModuleName(XTypeElement module, XMethodElement method) {
return XClassName.get(
module.getPackageName(),
String.format(
"%s_%s",
classFileName(module.asClassName()),
LOWER_CAMEL.to(UPPER_CAMEL, getSimpleName(method))));
}
@Override
public void clearCache() {
cache.clear();
}
}
}