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 35 * dagger.hilt.internal.processedrootsentinel.ProcessedRootSentinel}. 36 */ 37 @AutoValue 38 abstract class ProcessedRootSentinelMetadata { 39 40 /** Returns the processed root elements. */ rootElements()41 abstract ImmutableSet<TypeElement> rootElements(); 42 from(Elements elements)43 static ImmutableSet<ProcessedRootSentinelMetadata> from(Elements elements) { 44 return AggregatedElements.from( 45 ClassNames.PROCESSED_ROOT_SENTINEL_PACKAGE, 46 ClassNames.PROCESSED_ROOT_SENTINEL, 47 elements) 48 .stream() 49 .map(aggregatedElement -> create(aggregatedElement, elements)) 50 .collect(toImmutableSet()); 51 } 52 create(TypeElement element, Elements elements)53 private static ProcessedRootSentinelMetadata create(TypeElement element, Elements elements) { 54 AnnotationMirror annotationMirror = 55 Processors.getAnnotationMirror(element, ClassNames.PROCESSED_ROOT_SENTINEL); 56 57 ImmutableMap<String, AnnotationValue> values = 58 Processors.getAnnotationValues(elements, annotationMirror); 59 60 return new AutoValue_ProcessedRootSentinelMetadata( 61 AnnotationValues.getStrings(values.get("roots")).stream() 62 .map(elements::getTypeElement) 63 .collect(toImmutableSet())); 64 } 65 } 66