forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
AggregatedDepsMetadata.java
220 lines (194 loc) · 8.89 KB
/
AggregatedDepsMetadata.java
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
/*
* Copyright (C) 2021 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.hilt.processor.internal.aggregateddeps;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Iterables.getOnlyElement;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.ClassName;
import dagger.hilt.processor.internal.AggregatedElements;
import dagger.hilt.processor.internal.AnnotationValues;
import dagger.hilt.processor.internal.ClassNames;
import dagger.hilt.processor.internal.Processors;
import dagger.hilt.processor.internal.root.ir.AggregatedDepsIr;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
/**
* A class that represents the values stored in an {@link
* dagger.hilt.processor.internal.aggregateddeps.AggregatedDeps} annotation.
*/
@AutoValue
public abstract class AggregatedDepsMetadata {
private static final String AGGREGATED_DEPS_PACKAGE = "hilt_aggregated_deps";
enum DependencyType {
MODULE,
ENTRY_POINT,
COMPONENT_ENTRY_POINT
}
/** Returns the aggregating element */
public abstract TypeElement aggregatingElement();
public abstract Optional<TypeElement> testElement();
public abstract ImmutableSet<TypeElement> componentElements();
abstract DependencyType dependencyType();
public abstract TypeElement dependency();
public abstract ImmutableSet<TypeElement> replacedDependencies();
public boolean isModule() {
return dependencyType() == DependencyType.MODULE;
}
/** Returns metadata for all aggregated elements in the aggregating package. */
public static ImmutableSet<AggregatedDepsMetadata> from(Elements elements) {
return from(
AggregatedElements.from(AGGREGATED_DEPS_PACKAGE, ClassNames.AGGREGATED_DEPS, elements),
elements);
}
/** Returns metadata for each aggregated element. */
public static ImmutableSet<AggregatedDepsMetadata> from(
ImmutableSet<TypeElement> aggregatedElements, Elements elements) {
return aggregatedElements.stream()
.map(aggregatedElement -> create(aggregatedElement, elements))
.collect(toImmutableSet());
}
public static AggregatedDepsIr toIr(AggregatedDepsMetadata metadata) {
return new AggregatedDepsIr(
ClassName.get(metadata.aggregatingElement()),
metadata.componentElements().stream()
.map(ClassName::get)
.map(ClassName::canonicalName)
.collect(Collectors.toList()),
metadata.testElement()
.map(ClassName::get)
.map(ClassName::canonicalName)
.orElse(null),
metadata.replacedDependencies().stream()
.map(ClassName::get)
.map(ClassName::canonicalName)
.collect(Collectors.toList()),
metadata.dependencyType() == DependencyType.MODULE
? ClassName.get(metadata.dependency()).canonicalName()
: null,
metadata.dependencyType() == DependencyType.ENTRY_POINT
? ClassName.get(metadata.dependency()).canonicalName()
: null,
metadata.dependencyType() == DependencyType.COMPONENT_ENTRY_POINT
? ClassName.get(metadata.dependency()).canonicalName()
: null);
}
private static AggregatedDepsMetadata create(TypeElement element, Elements elements) {
AnnotationMirror annotationMirror =
Processors.getAnnotationMirror(element, ClassNames.AGGREGATED_DEPS);
ImmutableMap<String, AnnotationValue> values =
Processors.getAnnotationValues(elements, annotationMirror);
return new AutoValue_AggregatedDepsMetadata(
element,
getTestElement(values.get("test"), elements),
getComponents(values.get("components"), elements),
getDependencyType(
values.get("modules"), values.get("entryPoints"), values.get("componentEntryPoints")),
getDependency(
values.get("modules"),
values.get("entryPoints"),
values.get("componentEntryPoints"),
elements),
getReplacedDependencies(values.get("replaces"), elements));
}
private static Optional<TypeElement> getTestElement(
AnnotationValue testValue, Elements elements) {
checkNotNull(testValue);
String test = AnnotationValues.getString(testValue);
return test.isEmpty() ? Optional.empty() : Optional.of(elements.getTypeElement(test));
}
private static ImmutableSet<TypeElement> getComponents(
AnnotationValue componentsValue, Elements elements) {
checkNotNull(componentsValue);
ImmutableSet<TypeElement> componentNames =
AnnotationValues.getAnnotationValues(componentsValue).stream()
.map(AnnotationValues::getString)
.map(
// This is a temporary hack to map the old ApplicationComponent to the new
// SingletonComponent. Technically, this is only needed for backwards compatibility
// with libraries using the old processor since new processors should convert to the
// new SingletonComponent when generating the metadata class.
componentName ->
componentName.contentEquals(
"dagger.hilt.android.components.ApplicationComponent")
? ClassNames.SINGLETON_COMPONENT.canonicalName()
: componentName)
.map(elements::getTypeElement)
.collect(toImmutableSet());
checkState(!componentNames.isEmpty());
return componentNames;
}
private static DependencyType getDependencyType(
AnnotationValue modulesValue,
AnnotationValue entryPointsValue,
AnnotationValue componentEntryPointsValue) {
checkNotNull(modulesValue);
checkNotNull(entryPointsValue);
checkNotNull(componentEntryPointsValue);
ImmutableSet.Builder<DependencyType> dependencyTypes = ImmutableSet.builder();
if (!AnnotationValues.getAnnotationValues(modulesValue).isEmpty()) {
dependencyTypes.add(DependencyType.MODULE);
}
if (!AnnotationValues.getAnnotationValues(entryPointsValue).isEmpty()) {
dependencyTypes.add(DependencyType.ENTRY_POINT);
}
if (!AnnotationValues.getAnnotationValues(componentEntryPointsValue).isEmpty()) {
dependencyTypes.add(DependencyType.COMPONENT_ENTRY_POINT);
}
return getOnlyElement(dependencyTypes.build());
}
private static TypeElement getDependency(
AnnotationValue modulesValue,
AnnotationValue entryPointsValue,
AnnotationValue componentEntryPointsValue,
Elements elements) {
checkNotNull(modulesValue);
checkNotNull(entryPointsValue);
checkNotNull(componentEntryPointsValue);
return elements.getTypeElement(
AnnotationValues.getString(
getOnlyElement(
ImmutableSet.<AnnotationValue>builder()
.addAll(AnnotationValues.getAnnotationValues(modulesValue))
.addAll(AnnotationValues.getAnnotationValues(entryPointsValue))
.addAll(AnnotationValues.getAnnotationValues(componentEntryPointsValue))
.build())));
}
private static ImmutableSet<TypeElement> getReplacedDependencies(
AnnotationValue replacedDependenciesValue, Elements elements) {
// Allow null values to support libraries using a Hilt version before @TestInstallIn was added
return replacedDependenciesValue == null
? ImmutableSet.of()
: AnnotationValues.getAnnotationValues(replacedDependenciesValue).stream()
.map(AnnotationValues::getString)
.map(elements::getTypeElement)
.map(replacedDep -> getPublicDependency(replacedDep, elements))
.collect(toImmutableSet());
}
/** Returns the public Hilt wrapper module, or the module itself if its already public. */
private static TypeElement getPublicDependency(TypeElement dependency, Elements elements) {
return PkgPrivateMetadata.of(elements, dependency, ClassNames.MODULE)
.map(metadata -> elements.getTypeElement(metadata.generatedClassName().toString()))
.orElse(dependency);
}
}