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 com.squareup.javapoet.ClassName; 25 import dagger.hilt.processor.internal.AggregatedElements; 26 import dagger.hilt.processor.internal.AnnotationValues; 27 import dagger.hilt.processor.internal.ClassNames; 28 import dagger.hilt.processor.internal.Processors; 29 import dagger.hilt.processor.internal.root.ir.ProcessedRootSentinelIr; 30 import java.util.stream.Collectors; 31 import javax.lang.model.element.AnnotationMirror; 32 import javax.lang.model.element.AnnotationValue; 33 import javax.lang.model.element.TypeElement; 34 import javax.lang.model.util.Elements; 35 36 /** 37 * Represents the values stored in an {@link 38 * dagger.hilt.internal.processedrootsentinel.ProcessedRootSentinel}. 39 */ 40 @AutoValue 41 abstract class ProcessedRootSentinelMetadata { 42 43 /** Returns the aggregating element */ aggregatingElement()44 public abstract TypeElement aggregatingElement(); 45 46 /** Returns the processed root elements. */ rootElements()47 abstract ImmutableSet<TypeElement> rootElements(); 48 from(Elements elements)49 static ImmutableSet<ProcessedRootSentinelMetadata> from(Elements elements) { 50 return AggregatedElements.from( 51 ClassNames.PROCESSED_ROOT_SENTINEL_PACKAGE, 52 ClassNames.PROCESSED_ROOT_SENTINEL, 53 elements) 54 .stream() 55 .map(aggregatedElement -> create(aggregatedElement, elements)) 56 .collect(toImmutableSet()); 57 } 58 toIr(ProcessedRootSentinelMetadata metadata)59 static ProcessedRootSentinelIr toIr(ProcessedRootSentinelMetadata metadata) { 60 return new ProcessedRootSentinelIr( 61 ClassName.get(metadata.aggregatingElement()), 62 metadata.rootElements().stream().map(ClassName::get).collect(Collectors.toList()) 63 ); 64 } 65 create(TypeElement element, Elements elements)66 private static ProcessedRootSentinelMetadata create(TypeElement element, Elements elements) { 67 AnnotationMirror annotationMirror = 68 Processors.getAnnotationMirror(element, ClassNames.PROCESSED_ROOT_SENTINEL); 69 70 ImmutableMap<String, AnnotationValue> values = 71 Processors.getAnnotationValues(elements, annotationMirror); 72 73 return new AutoValue_ProcessedRootSentinelMetadata( 74 element, 75 AnnotationValues.getStrings(values.get("roots")).stream() 76 .map(elements::getTypeElement) 77 .collect(toImmutableSet())); 78 } 79 } 80