aboutsummaryrefslogtreecommitdiff
path: root/java/dagger/hilt/processor/internal/aggregateddeps/ComponentDependencies.java
blob: 23ed148c0ecff10f650d09fd634bbce04e3a1b0a (plain)
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * Copyright (C) 2019 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.hilt.processor.internal.aggregateddeps.AggregatedDepsGenerator.AGGREGATING_PACKAGE;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;

import com.google.auto.value.AutoValue;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.SetMultimap;
import com.squareup.javapoet.ClassName;
import dagger.hilt.processor.internal.AnnotationValues;
import dagger.hilt.processor.internal.BadInputException;
import dagger.hilt.processor.internal.ClassNames;
import dagger.hilt.processor.internal.ComponentDescriptor;
import dagger.hilt.processor.internal.ProcessorErrors;
import dagger.hilt.processor.internal.Processors;
import dagger.hilt.processor.internal.aggregateddeps.ComponentDependencies.AggregatedDepMetadata.DependencyType;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;

/** Represents information needed to create a component (i.e. modules, entry points, etc) */
@AutoValue
public abstract class ComponentDependencies {
  private static Builder builder() {
    return new AutoValue_ComponentDependencies.Builder();
  }

  /** Returns the modules for a component, without any filtering. */
  public abstract Dependencies modules();

  /** Returns the entry points associated with the given a component. */
  public abstract Dependencies entryPoints();

  /** Returns the component entry point associated with the given a component. */
  public abstract Dependencies componentEntryPoints();

  @AutoValue.Builder
  abstract static class Builder {
    abstract Dependencies.Builder modulesBuilder();

    abstract Dependencies.Builder entryPointsBuilder();

    abstract Dependencies.Builder componentEntryPointsBuilder();

    abstract ComponentDependencies autoBuild();

    ComponentDependencies build(Elements elements) {
      validateModules(modulesBuilder().build(), elements);
      return autoBuild();
    }
  }

  /** A key used for grouping a test dependency by both its component and test name. */
  @AutoValue
  abstract static class TestDepKey {
    static TestDepKey of(ClassName component, ClassName test) {
      return new AutoValue_ComponentDependencies_TestDepKey(component, test);
    }

    /** Returns the name of the component this dependency should be installed in. */
    abstract ClassName component();

    /** Returns the name of the test that this dependency should be installed in. */
    abstract ClassName test();
  }

  /**
   * Holds a set of component dependencies, e.g. modules or entry points.
   *
   * <p>This class handles separating dependencies into global and test dependencies. Global
   * dependencies are installed with every test, where test dependencies are only installed with the
   * specified test. The total set of dependencies includes all global + test dependencies.
   */
  @AutoValue
  public abstract static class Dependencies {
    static Builder builder() {
      return new AutoValue_ComponentDependencies_Dependencies.Builder();
    }

    /** Returns the global deps keyed by component. */
    abstract ImmutableSetMultimap<ClassName, TypeElement> globalDeps();

    /** Returns the global test deps keyed by component. */
    abstract ImmutableSetMultimap<ClassName, TypeElement> globalTestDeps();

    /** Returns the test deps keyed by component and test. */
    abstract ImmutableSetMultimap<TestDepKey, TypeElement> testDeps();

    /** Returns the uninstalled test deps keyed by test. */
    abstract ImmutableSetMultimap<ClassName, TypeElement> uninstalledTestDeps();

    /** Returns the global uninstalled test deps. */
    abstract ImmutableSet<TypeElement> globalUninstalledTestDeps();

    /** Returns the dependencies to be installed in the given component for the given root. */
    public ImmutableSet<TypeElement> get(ClassName component, ClassName root, boolean isTestRoot) {
      if (!isTestRoot) {
        return globalDeps().get(component);
      }

      ImmutableSet<TypeElement> uninstalledTestDepsForRoot = uninstalledTestDeps().get(root);
      return ImmutableSet.<TypeElement>builder()
          .addAll(
              globalDeps().get(component).stream()
                  .filter(dep -> !uninstalledTestDepsForRoot.contains(dep))
                  .filter(dep -> !globalUninstalledTestDeps().contains(dep))
                  .collect(toImmutableSet()))
          .addAll(globalTestDeps().get(component))
          .addAll(testDeps().get(TestDepKey.of(component, root)))
          .build();
    }

    @AutoValue.Builder
    abstract static class Builder {
      abstract ImmutableSetMultimap.Builder<ClassName, TypeElement> globalDepsBuilder();

      abstract ImmutableSetMultimap.Builder<ClassName, TypeElement> globalTestDepsBuilder();

      abstract ImmutableSetMultimap.Builder<TestDepKey, TypeElement> testDepsBuilder();

      abstract ImmutableSetMultimap.Builder<ClassName, TypeElement> uninstalledTestDepsBuilder();

      abstract ImmutableSet.Builder<TypeElement> globalUninstalledTestDepsBuilder();

      abstract Dependencies build();
    }
  }

  /**
   * Pulls the component dependencies from the {@code packageName}.
   *
   * <p>Dependency files are generated by the {@link AggregatedDepsProcessor}, and have the form:
   *
   * <pre>{@code
   * {@literal @}AggregatedDeps(
   *   components = {
   *       "foo.FooComponent",
   *       "bar.BarComponent"
   *   },
   *   modules = "baz.BazModule"
   * )
   *
   * }</pre>
   */
  public static ComponentDependencies from(
      ImmutableSet<ComponentDescriptor> descriptors, Elements elements) {
    Map<String, ComponentDescriptor> descriptorLookup = descriptorLookupMap(descriptors);
    ImmutableList<AggregatedDepMetadata> metadatas =
        getAggregatedDeps(elements).stream()
            .map(deps -> AggregatedDepMetadata.create(deps, descriptorLookup, elements))
            .collect(toImmutableList());

    ComponentDependencies.Builder componentDependencies = ComponentDependencies.builder();
    for (AggregatedDepMetadata metadata : metadatas) {
      Dependencies.Builder builder = null;
      switch (metadata.dependencyType()) {
        case MODULE:
          builder = componentDependencies.modulesBuilder();
          break;
        case ENTRY_POINT:
          builder = componentDependencies.entryPointsBuilder();
          break;
        case COMPONENT_ENTRY_POINT:
          builder = componentDependencies.componentEntryPointsBuilder();
          break;
      }

      for (ComponentDescriptor componentDescriptor : metadata.componentDescriptors()) {
        ClassName component = componentDescriptor.component();
        if (metadata.testElement().isPresent()) {
          // In this case the @InstallIn or @TestInstallIn applies to only the given test root.
          ClassName test = ClassName.get(metadata.testElement().get());
          builder.testDepsBuilder().put(TestDepKey.of(component, test), metadata.dependency());
          builder.uninstalledTestDepsBuilder().putAll(test, metadata.replacedDependencies());
        } else {
          // In this case the @InstallIn or @TestInstallIn applies to all roots
          if (!metadata.replacedDependencies().isEmpty()) {
            // If there are replacedDependencies() it means this is a @TestInstallIn
            builder.globalTestDepsBuilder().put(component, metadata.dependency());
            builder.globalUninstalledTestDepsBuilder().addAll(metadata.replacedDependencies());
          } else {
            builder.globalDepsBuilder().put(component, metadata.dependency());
          }
        }
      }
    }

    // Collect all @UninstallModules.
    // TODO(b/176438516): Filter @UninstallModules at the root.
    metadatas.stream()
        .filter(metadata -> metadata.testElement().isPresent())
        .map(metadata -> metadata.testElement().get())
        .distinct()
        .filter(testElement -> Processors.hasAnnotation(testElement, ClassNames.IGNORE_MODULES))
        .forEach(
            testElement ->
                componentDependencies
                    .modulesBuilder()
                    .uninstalledTestDepsBuilder()
                    .putAll(
                        ClassName.get(testElement), getUninstalledModules(testElement, elements)));

    return componentDependencies.build(elements);
  }

  private static ImmutableMap<String, ComponentDescriptor> descriptorLookupMap(
      ImmutableSet<ComponentDescriptor> descriptors) {
    ImmutableMap.Builder<String, ComponentDescriptor> builder = ImmutableMap.builder();
    for (ComponentDescriptor descriptor : descriptors) {
      // 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.
      if (descriptor.component().equals(ClassNames.SINGLETON_COMPONENT)) {
        builder.put("dagger.hilt.android.components.ApplicationComponent", descriptor);
      }
      builder.put(descriptor.component().toString(), descriptor);
    }
    return builder.build();
  }

  // Validate that the @UninstallModules doesn't contain any test modules.
  private static Dependencies validateModules(Dependencies moduleDeps, Elements elements) {
    SetMultimap<ClassName, TypeElement> invalidTestModules = HashMultimap.create();
    moduleDeps.testDeps().entries().stream()
        .filter(
            e -> moduleDeps.uninstalledTestDeps().containsEntry(e.getKey().test(), e.getValue()))
        .forEach(e -> invalidTestModules.put(e.getKey().test(), e.getValue()));

    // Currently we don't have a good way to throw an error for all tests, so we sort (to keep the
    // error reporting order stable) and then choose the first test.
    // TODO(bcorso): Consider using ProcessorErrorHandler directly to report all errors at once?
    Optional<ClassName> invalidTest =
        invalidTestModules.keySet().stream()
            .min((test1, test2) -> test1.toString().compareTo(test2.toString()));
    if (invalidTest.isPresent()) {
      throw new BadInputException(
          String.format(
              "@UninstallModules on test, %s, should not containing test modules, "
                  + "but found: %s",
              invalidTest.get(),
              invalidTestModules.get(invalidTest.get()).stream()
                  // Sort modules to keep stable error messages.
                  .sorted((test1, test2) -> test1.toString().compareTo(test2.toString()))
                  .collect(toImmutableList())),
          elements.getTypeElement(invalidTest.get().toString()));
    }
    return moduleDeps;
  }

  private static ImmutableSet<TypeElement> getUninstalledModules(
      TypeElement testElement, Elements elements) {
    ImmutableList<TypeElement> userUninstallModules =
        Processors.getAnnotationClassValues(
            elements,
            Processors.getAnnotationMirror(testElement, ClassNames.IGNORE_MODULES),
            "value");

    // For pkg-private modules, find the generated wrapper class and uninstall that instead.
    return userUninstallModules.stream()
        .map(uninstallModule -> getPublicDependency(uninstallModule, 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);
  }

  /** Returns the top-level elements of the aggregated deps package. */
  private static ImmutableList<AnnotationMirror> getAggregatedDeps(Elements elements) {
    PackageElement packageElement = elements.getPackageElement(AGGREGATING_PACKAGE);
    checkState(
        packageElement != null,
        "Couldn't find package %s. Did you mark your @Module classes with @InstallIn annotations?",
        AGGREGATING_PACKAGE);

    List<? extends Element> aggregatedDepsElements = packageElement.getEnclosedElements();
    checkState(
        !aggregatedDepsElements.isEmpty(),
        "No dependencies found. Did you mark your @Module classes with @InstallIn annotations?");

    ImmutableList.Builder<AnnotationMirror> builder = ImmutableList.builder();
    for (Element element : aggregatedDepsElements) {
      ProcessorErrors.checkState(
          element.getKind() == ElementKind.CLASS,
          element,
          "Only classes may be in package %s. Did you add custom code in the package?",
          AGGREGATING_PACKAGE);

      AnnotationMirror aggregatedDeps =
          Processors.getAnnotationMirror(element, ClassNames.AGGREGATED_DEPS);
      ProcessorErrors.checkState(
          aggregatedDeps != null,
          element,
          "Classes in package %s must be annotated with @AggregatedDeps: %s. Found: %s.",
          AGGREGATING_PACKAGE,
          element.getSimpleName(),
          element.getAnnotationMirrors());

      builder.add(aggregatedDeps);
    }
    return builder.build();
  }

  @AutoValue
  abstract static class AggregatedDepMetadata {
    static AggregatedDepMetadata create(
        AnnotationMirror aggregatedDeps,
        Map<String, ComponentDescriptor> descriptorLookup,
        Elements elements) {
      ImmutableMap<String, AnnotationValue> aggregatedDepsValues =
          Processors.getAnnotationValues(elements, aggregatedDeps);

      return new AutoValue_ComponentDependencies_AggregatedDepMetadata(
          getTestElement(aggregatedDepsValues.get("test"), elements),
          getComponents(aggregatedDepsValues.get("components"), descriptorLookup),
          getDependencyType(
              aggregatedDepsValues.get("modules"),
              aggregatedDepsValues.get("entryPoints"),
              aggregatedDepsValues.get("componentEntryPoints")),
          getDependency(
              aggregatedDepsValues.get("modules"),
              aggregatedDepsValues.get("entryPoints"),
              aggregatedDepsValues.get("componentEntryPoints"),
              elements),
          getReplacedDependencies(aggregatedDepsValues.get("replaces"), elements));
    }

    enum DependencyType {
      MODULE,
      ENTRY_POINT,
      COMPONENT_ENTRY_POINT
    }

    abstract Optional<TypeElement> testElement();

    abstract ImmutableList<ComponentDescriptor> componentDescriptors();

    abstract DependencyType dependencyType();

    abstract TypeElement dependency();

    abstract ImmutableSet<TypeElement> replacedDependencies();

    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 ImmutableList<ComponentDescriptor> getComponents(
        AnnotationValue componentsValue, Map<String, ComponentDescriptor> descriptorLookup) {
      checkNotNull(componentsValue);
      ImmutableList<String> componentNames =
          AnnotationValues.getAnnotationValues(componentsValue).stream()
              .map(AnnotationValues::getString)
              .collect(toImmutableList());

      checkState(!componentNames.isEmpty());
      ImmutableList.Builder<ComponentDescriptor> components = ImmutableList.builder();
      for (String componentName : componentNames) {
        checkState(
            descriptorLookup.containsKey(componentName),
            "%s is not a valid Component. Did you add or remove code in package %s?",
            componentName,
            AGGREGATING_PACKAGE);
        components.add(descriptorLookup.get(componentName));
      }
      return components.build();
    }

    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(
                  ImmutableList.<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());
    }
  }
}