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 dagger.hilt.processor.internal.AggregatedElements; 27 import dagger.hilt.processor.internal.AnnotationValues; 28 import dagger.hilt.processor.internal.ClassNames; 29 import dagger.hilt.processor.internal.Processors; 30 import javax.lang.model.element.AnnotationMirror; 31 import javax.lang.model.element.AnnotationValue; 32 import javax.lang.model.element.TypeElement; 33 import javax.lang.model.util.Elements; 34 35 /** 36 * A class that represents the values stored in an 37 * {@link dagger.hilt.android.internal.uninstallmodules.AggregatedUninstallModules} annotation. 38 */ 39 @AutoValue 40 public abstract class AggregatedUninstallModulesMetadata { 41 42 /** Returns the test annotated with {@link dagger.hilt.android.testing.UninstallModules}. */ testElement()43 public abstract TypeElement testElement(); 44 45 /** 46 * Returns the list of uninstall modules in {@link dagger.hilt.android.testing.UninstallModules}. 47 */ uninstallModuleElements()48 public abstract ImmutableList<TypeElement> uninstallModuleElements(); 49 50 /** Returns all aggregated deps in the aggregating package mapped by the top-level element. */ from(Elements elements)51 public static ImmutableSet<AggregatedUninstallModulesMetadata> from(Elements elements) { 52 return AggregatedElements.from( 53 ClassNames.AGGREGATED_UNINSTALL_MODULES_PACKAGE, 54 ClassNames.AGGREGATED_UNINSTALL_MODULES, 55 elements) 56 .stream() 57 .map(aggregatedElement -> create(aggregatedElement, elements)) 58 .collect(toImmutableSet()); 59 } 60 create(TypeElement element, Elements elements)61 private static AggregatedUninstallModulesMetadata create(TypeElement element, Elements elements) { 62 AnnotationMirror annotationMirror = 63 Processors.getAnnotationMirror(element, ClassNames.AGGREGATED_UNINSTALL_MODULES); 64 65 ImmutableMap<String, AnnotationValue> values = 66 Processors.getAnnotationValues(elements, annotationMirror); 67 68 return new AutoValue_AggregatedUninstallModulesMetadata( 69 elements.getTypeElement(AnnotationValues.getString(values.get("test"))), 70 AnnotationValues.getAnnotationValues(values.get("uninstallModules")).stream() 71 .map(AnnotationValues::getString) 72 .map(elements::getTypeElement) 73 .collect(toImmutableList())); 74 } 75 } 76