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.root; 18 19 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet; 20 21 import com.google.auto.value.AutoValue; 22 import com.google.common.collect.ImmutableMap; 23 import com.google.common.collect.ImmutableSet; 24 import dagger.hilt.processor.internal.AggregatedElements; 25 import dagger.hilt.processor.internal.AnnotationValues; 26 import dagger.hilt.processor.internal.ClassNames; 27 import dagger.hilt.processor.internal.Processors; 28 import javax.lang.model.element.AnnotationMirror; 29 import javax.lang.model.element.AnnotationValue; 30 import javax.lang.model.element.TypeElement; 31 import javax.lang.model.util.Elements; 32 33 /** 34 * Represents the values stored in an {@link dagger.hilt.internal.aggregatedroot.AggregatedRoot}. 35 */ 36 @AutoValue 37 abstract class AggregatedRootMetadata { 38 39 /** Returns the element that was annotated with the root annotation. */ rootElement()40 abstract TypeElement rootElement(); 41 42 /** Returns the root annotation as an element. */ rootAnnotation()43 abstract TypeElement rootAnnotation(); 44 from(Elements elements)45 static ImmutableSet<AggregatedRootMetadata> from(Elements elements) { 46 return AggregatedElements.from( 47 ClassNames.AGGREGATED_ROOT_PACKAGE, ClassNames.AGGREGATED_ROOT, elements) 48 .stream() 49 .map(aggregatedElement -> create(aggregatedElement, elements)) 50 .collect(toImmutableSet()); 51 } 52 create(TypeElement element, Elements elements)53 private static AggregatedRootMetadata create(TypeElement element, Elements elements) { 54 AnnotationMirror annotationMirror = 55 Processors.getAnnotationMirror(element, ClassNames.AGGREGATED_ROOT); 56 57 ImmutableMap<String, AnnotationValue> values = 58 Processors.getAnnotationValues(elements, annotationMirror); 59 60 return new AutoValue_AggregatedRootMetadata( 61 elements.getTypeElement(AnnotationValues.getString(values.get("root"))), 62 AnnotationValues.getTypeElement(values.get("rootAnnotation"))); 63 } 64 } 65