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 dagger.hilt.processor.internal.Processors; 23 import java.io.IOException; 24 import java.util.Optional; 25 import javax.annotation.processing.ProcessingEnvironment; 26 import javax.lang.model.element.TypeElement; 27 28 /** 29 * Generates the @AggregatedDeps annotated class used to pass information 30 * about modules and entry points through multiple javac runs. 31 */ 32 final class AggregatedDepsGenerator { 33 static final String AGGREGATING_PACKAGE = "hilt_aggregated_deps"; 34 private static final ClassName AGGREGATED_DEPS = 35 ClassName.get("dagger.hilt.processor.internal.aggregateddeps", "AggregatedDeps"); 36 37 private final String dependencyType; 38 private final TypeElement dependency; 39 private final Optional<ClassName> testName; 40 private final ImmutableSet<ClassName> components; 41 private final ImmutableSet<ClassName> replacedDependencies; 42 private final ProcessingEnvironment processingEnv; 43 AggregatedDepsGenerator( String dependencyType, TypeElement dependency, Optional<ClassName> testName, ImmutableSet<ClassName> components, ImmutableSet<ClassName> replacedDependencies, ProcessingEnvironment processingEnv)44 AggregatedDepsGenerator( 45 String dependencyType, 46 TypeElement dependency, 47 Optional<ClassName> testName, 48 ImmutableSet<ClassName> components, 49 ImmutableSet<ClassName> replacedDependencies, 50 ProcessingEnvironment processingEnv) { 51 this.dependencyType = dependencyType; 52 this.dependency = dependency; 53 this.testName = testName; 54 this.components = components; 55 this.replacedDependencies = replacedDependencies; 56 this.processingEnv = processingEnv; 57 } 58 generate()59 void generate() throws IOException { 60 Processors.generateAggregatingClass( 61 AGGREGATING_PACKAGE, aggregatedDepsAnnotation(), dependency, getClass(), processingEnv); 62 } 63 aggregatedDepsAnnotation()64 private AnnotationSpec aggregatedDepsAnnotation() { 65 AnnotationSpec.Builder annotationBuilder = AnnotationSpec.builder(AGGREGATED_DEPS); 66 components.forEach(component -> annotationBuilder.addMember("components", "$S", component)); 67 replacedDependencies.forEach(dep -> annotationBuilder.addMember("replaces", "$S", dep)); 68 testName.ifPresent(test -> annotationBuilder.addMember("test", "$S", test)); 69 annotationBuilder.addMember(dependencyType, "$S", dependency.getQualifiedName()); 70 return annotationBuilder.build(); 71 } 72 } 73