• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Dagger Authors.
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 dagger.hilt.processor.internal.aggregateddeps;
18 
19 import com.google.common.collect.ImmutableSet;
20 import com.squareup.javapoet.AnnotationSpec;
21 import com.squareup.javapoet.ClassName;
22 import com.squareup.javapoet.JavaFile;
23 import com.squareup.javapoet.TypeSpec;
24 import dagger.hilt.processor.internal.Processors;
25 import java.io.IOException;
26 import java.util.Optional;
27 import javax.annotation.processing.ProcessingEnvironment;
28 import javax.lang.model.element.TypeElement;
29 
30 /**
31  * Generates the @AggregatedDeps annotated class used to pass information
32  * about modules and entry points through multiple javac runs.
33  */
34 final class AggregatedDepsGenerator {
35   static final String AGGREGATING_PACKAGE = "hilt_aggregated_deps";
36   private static final ClassName AGGREGATED_DEPS =
37       ClassName.get("dagger.hilt.processor.internal.aggregateddeps", "AggregatedDeps");
38 
39   private final String dependencyType;
40   private final TypeElement dependency;
41   private final Optional<ClassName> testName;
42   private final ImmutableSet<ClassName> components;
43   private final ImmutableSet<ClassName> replacedDependencies;
44   private final ProcessingEnvironment processingEnv;
45 
AggregatedDepsGenerator( String dependencyType, TypeElement dependency, Optional<ClassName> testName, ImmutableSet<ClassName> components, ImmutableSet<ClassName> replacedDependencies, ProcessingEnvironment processingEnv)46   AggregatedDepsGenerator(
47       String dependencyType,
48       TypeElement dependency,
49       Optional<ClassName> testName,
50       ImmutableSet<ClassName> components,
51       ImmutableSet<ClassName> replacedDependencies,
52       ProcessingEnvironment processingEnv) {
53     this.dependencyType = dependencyType;
54     this.dependency = dependency;
55     this.testName = testName;
56     this.components = components;
57     this.replacedDependencies = replacedDependencies;
58     this.processingEnv = processingEnv;
59   }
60 
generate()61   void generate() throws IOException {
62     ClassName name =
63         ClassName.get(
64             AGGREGATING_PACKAGE, Processors.getFullEnclosedName(dependency) + "ModuleDeps");
65     TypeSpec.Builder generator =
66         TypeSpec.classBuilder(name.simpleName())
67             .addOriginatingElement(dependency)
68             .addAnnotation(aggregatedDepsAnnotation())
69             .addJavadoc("Generated class to pass information through multiple javac runs.\n");
70 
71     Processors.addGeneratedAnnotation(generator, processingEnv, getClass());
72 
73     JavaFile.builder(name.packageName(), generator.build())
74         .build()
75         .writeTo(processingEnv.getFiler());
76   }
77 
aggregatedDepsAnnotation()78   private AnnotationSpec aggregatedDepsAnnotation() {
79     AnnotationSpec.Builder annotationBuilder = AnnotationSpec.builder(AGGREGATED_DEPS);
80     components.forEach(component -> annotationBuilder.addMember("components", "$S", component));
81     replacedDependencies.forEach(dep -> annotationBuilder.addMember("replaces", "$S", dep));
82     testName.ifPresent(test -> annotationBuilder.addMember("test", "$S", test));
83     annotationBuilder.addMember(dependencyType, "$S", dependency.getQualifiedName());
84     return annotationBuilder.build();
85   }
86 }
87