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