forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathModuleProcessingStep.java
More file actions
161 lines (150 loc) · 7.28 KB
/
Copy pathModuleProcessingStep.java
File metadata and controls
161 lines (150 loc) · 7.28 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
/*
* Copyright (C) 2014 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.processingstep;
import static dagger.internal.codegen.extension.DaggerCollectors.toOptional;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import androidx.room3.compiler.codegen.XClassName;
import androidx.room3.compiler.processing.XElement;
import androidx.room3.compiler.processing.XMethodElement;
import androidx.room3.compiler.processing.XProcessingEnv;
import androidx.room3.compiler.processing.XTypeElement;
import com.google.common.collect.ImmutableSet;
import dagger.internal.codegen.base.SourceFileGenerator;
import dagger.internal.codegen.base.ValidationReport;
import dagger.internal.codegen.binding.BindingFactory;
import dagger.internal.codegen.binding.BindingGraph;
import dagger.internal.codegen.binding.BindingGraphFactory;
import dagger.internal.codegen.binding.ComponentDescriptor;
import dagger.internal.codegen.binding.ContributionBinding;
import dagger.internal.codegen.binding.DelegateDeclaration;
import dagger.internal.codegen.binding.ProductionBinding;
import dagger.internal.codegen.validation.BindingGraphValidator;
import dagger.internal.codegen.validation.ModuleValidator;
import dagger.internal.codegen.writing.InaccessibleMapKeyProxyGenerator;
import dagger.internal.codegen.writing.ModuleGenerator;
import dagger.internal.codegen.xprocessing.XTypeNames;
import java.util.Map;
import java.util.Set;
import javax.inject.Inject;
/**
* A {@link ProcessingStep} that validates module classes and generates factories for binding
* methods.
*/
final class ModuleProcessingStep extends TypeCheckingProcessingStep<XTypeElement> {
private final ModuleValidator moduleValidator;
private final BindingFactory bindingFactory;
private final SourceFileGenerator<ContributionBinding> factoryGenerator;
private final SourceFileGenerator<ProductionBinding> producerFactoryGenerator;
private final SourceFileGenerator<XTypeElement> moduleConstructorProxyGenerator;
private final InaccessibleMapKeyProxyGenerator inaccessibleMapKeyProxyGenerator;
private final DelegateDeclaration.Factory delegateDeclarationFactory;
private final ComponentDescriptor.Factory componentDescriptorFactory;
private final BindingGraphFactory bindingGraphFactory;
private final BindingGraphValidator bindingGraphValidator;
@Inject
ModuleProcessingStep(
ModuleValidator moduleValidator,
BindingFactory bindingFactory,
SourceFileGenerator<ContributionBinding> factoryGenerator,
SourceFileGenerator<ProductionBinding> producerFactoryGenerator,
@ModuleGenerator SourceFileGenerator<XTypeElement> moduleConstructorProxyGenerator,
InaccessibleMapKeyProxyGenerator inaccessibleMapKeyProxyGenerator,
DelegateDeclaration.Factory delegateDeclarationFactory,
ComponentDescriptor.Factory componentDescriptorFactory,
BindingGraphFactory bindingGraphFactory,
BindingGraphValidator bindingGraphValidator) {
this.moduleValidator = moduleValidator;
this.bindingFactory = bindingFactory;
this.factoryGenerator = factoryGenerator;
this.producerFactoryGenerator = producerFactoryGenerator;
this.moduleConstructorProxyGenerator = moduleConstructorProxyGenerator;
this.inaccessibleMapKeyProxyGenerator = inaccessibleMapKeyProxyGenerator;
this.delegateDeclarationFactory = delegateDeclarationFactory;
this.componentDescriptorFactory = componentDescriptorFactory;
this.bindingGraphFactory = bindingGraphFactory;
this.bindingGraphValidator = bindingGraphValidator;
}
@Override
public ImmutableSet<XClassName> annotationClassNames() {
return ImmutableSet.of(XTypeNames.MODULE, XTypeNames.PRODUCER_MODULE);
}
@Override
public ImmutableSet<XElement> process(
XProcessingEnv env, Map<String, ? extends Set<? extends XElement>> elementsByAnnotation) {
moduleValidator.addKnownModules(
elementsByAnnotation.values().stream()
.flatMap(Set::stream)
// This cast is safe because @Module has @Target(ElementType.TYPE)
.map(XTypeElement.class::cast)
.collect(toImmutableSet()));
return super.process(env, elementsByAnnotation);
}
@Override
protected void process(XTypeElement module, ImmutableSet<XClassName> annotations) {
// For backwards compatibility, we allow a companion object to be annotated with @Module even
// though it's no longer required. However, we skip processing the companion object itself
// because it will now be processed when processing the companion object's enclosing class.
if (module.isCompanionObject()) {
// TODO(danysantiago): Be strict about annotating companion objects with @Module,
// i.e. tell user to annotate parent instead.
return;
}
ValidationReport report = moduleValidator.validate(module);
report.printMessagesTo(messager);
if (!report.isClean()) {
return;
}
if (bindingGraphValidator.shouldDoFullBindingGraphValidation(module)) {
BindingGraph bindingGraph =
bindingGraphFactory.create(
componentDescriptorFactory.moduleComponentDescriptor(module),
/* createFullBindingGraph= */ true);
if (!bindingGraphValidator.isValid(bindingGraph.topLevelBindingGraph())) {
return;
}
}
generateForMethodsIn(module);
module.getEnclosedTypeElements().stream()
.filter(XTypeElement::isCompanionObject)
.collect(toOptional())
.ifPresent(this::generateForMethodsIn);
}
private void generateForMethodsIn(XTypeElement module) {
for (XMethodElement method : module.getDeclaredMethods()) {
if (method.hasAnnotation(XTypeNames.PROVIDES)) {
generate(factoryGenerator, bindingFactory.providesMethodBinding(method, module));
} else if (method.hasAnnotation(XTypeNames.PRODUCES)) {
generate(producerFactoryGenerator, bindingFactory.producesMethodBinding(method, module));
} else if (method.hasAnnotation(XTypeNames.BINDS) && !method.getParameters().isEmpty()) {
inaccessibleMapKeyProxyGenerator.generate(bindsMethodBinding(module, method), messager);
}
}
// We should never need to generate a constructor proxy for a companion object since we never
// need to call a companion object's constructor.
if (!module.isCompanionObject()) {
moduleConstructorProxyGenerator.generate(module, messager);
}
}
private <B extends ContributionBinding> void generate(
SourceFileGenerator<B> generator, B binding) {
generator.generate(binding, messager);
inaccessibleMapKeyProxyGenerator.generate(binding, messager);
}
private ContributionBinding bindsMethodBinding(XTypeElement module, XMethodElement method) {
return bindingFactory.unresolvedDelegateBinding(
delegateDeclarationFactory.create(method, module));
}
}