1 /* 2 * Copyright (C) 2021 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.uninstallmodules; 18 19 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList; 20 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet; 21 22 import com.google.auto.value.AutoValue; 23 import com.google.common.collect.ImmutableList; 24 import com.google.common.collect.ImmutableMap; 25 import com.google.common.collect.ImmutableSet; 26 import com.squareup.javapoet.ClassName; 27 import dagger.hilt.processor.internal.AggregatedElements; 28 import dagger.hilt.processor.internal.AnnotationValues; 29 import dagger.hilt.processor.internal.ClassNames; 30 import dagger.hilt.processor.internal.Processors; 31 import dagger.hilt.processor.internal.root.ir.AggregatedUninstallModulesIr; 32 import java.util.stream.Collectors; 33 import javax.lang.model.element.AnnotationMirror; 34 import javax.lang.model.element.AnnotationValue; 35 import javax.lang.model.element.TypeElement; 36 import javax.lang.model.util.Elements; 37 38 /** 39 * A class that represents the values stored in an 40 * {@link dagger.hilt.android.internal.uninstallmodules.AggregatedUninstallModules} annotation. 41 */ 42 @AutoValue 43 public abstract class AggregatedUninstallModulesMetadata { 44 45 /** Returns the aggregating element */ aggregatingElement()46 public abstract TypeElement aggregatingElement(); 47 48 /** Returns the test annotated with {@link dagger.hilt.android.testing.UninstallModules}. */ testElement()49 public abstract TypeElement testElement(); 50 51 /** 52 * Returns the list of uninstall modules in {@link dagger.hilt.android.testing.UninstallModules}. 53 */ uninstallModuleElements()54 public abstract ImmutableList<TypeElement> uninstallModuleElements(); 55 56 /** Returns metadata for all aggregated elements in the aggregating package. */ from(Elements elements)57 public static ImmutableSet<AggregatedUninstallModulesMetadata> from(Elements elements) { 58 return from( 59 AggregatedElements.from( 60 ClassNames.AGGREGATED_UNINSTALL_MODULES_PACKAGE, 61 ClassNames.AGGREGATED_UNINSTALL_MODULES, 62 elements), 63 elements); 64 } 65 66 /** Returns metadata for each aggregated element. */ from( ImmutableSet<TypeElement> aggregatedElements, Elements elements)67 public static ImmutableSet<AggregatedUninstallModulesMetadata> from( 68 ImmutableSet<TypeElement> aggregatedElements, Elements elements) { 69 return aggregatedElements.stream() 70 .map(aggregatedElement -> create(aggregatedElement, elements)) 71 .collect(toImmutableSet()); 72 } 73 toIr(AggregatedUninstallModulesMetadata metadata)74 public static AggregatedUninstallModulesIr toIr(AggregatedUninstallModulesMetadata metadata) { 75 return new AggregatedUninstallModulesIr( 76 ClassName.get(metadata.aggregatingElement()), 77 ClassName.get(metadata.testElement()), 78 metadata.uninstallModuleElements().stream() 79 .map(ClassName::get) 80 .collect(Collectors.toList())); 81 } 82 create(TypeElement element, Elements elements)83 private static AggregatedUninstallModulesMetadata create(TypeElement element, Elements elements) { 84 AnnotationMirror annotationMirror = 85 Processors.getAnnotationMirror(element, ClassNames.AGGREGATED_UNINSTALL_MODULES); 86 87 ImmutableMap<String, AnnotationValue> values = 88 Processors.getAnnotationValues(elements, annotationMirror); 89 90 return new AutoValue_AggregatedUninstallModulesMetadata( 91 element, 92 elements.getTypeElement(AnnotationValues.getString(values.get("test"))), 93 AnnotationValues.getAnnotationValues(values.get("uninstallModules")).stream() 94 .map(AnnotationValues::getString) 95 .map(elements::getTypeElement) 96 .collect(toImmutableList())); 97 } 98 } 99