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 androidx.room.compiler.processing.XAnnotation; 22 import androidx.room.compiler.processing.XProcessingEnv; 23 import androidx.room.compiler.processing.XTypeElement; 24 import com.google.auto.value.AutoValue; 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.ClassNames; 29 import dagger.hilt.processor.internal.root.ir.ProcessedRootSentinelIr; 30 import java.util.stream.Collectors; 31 32 /** 33 * Represents the values stored in an {@link 34 * dagger.hilt.internal.processedrootsentinel.ProcessedRootSentinel}. 35 */ 36 @AutoValue 37 abstract class ProcessedRootSentinelMetadata { 38 39 /** Returns the aggregating element */ aggregatingElement()40 public abstract XTypeElement aggregatingElement(); 41 42 /** Returns the processed root elements. */ rootElements()43 abstract ImmutableSet<XTypeElement> rootElements(); 44 from(XProcessingEnv env)45 static ImmutableSet<ProcessedRootSentinelMetadata> from(XProcessingEnv env) { 46 return AggregatedElements.from( 47 ClassNames.PROCESSED_ROOT_SENTINEL_PACKAGE, ClassNames.PROCESSED_ROOT_SENTINEL, env) 48 .stream() 49 .map(aggregatedElement -> create(aggregatedElement, env)) 50 .collect(toImmutableSet()); 51 } 52 toIr(ProcessedRootSentinelMetadata metadata)53 static ProcessedRootSentinelIr toIr(ProcessedRootSentinelMetadata metadata) { 54 return new ProcessedRootSentinelIr( 55 metadata.aggregatingElement().getClassName(), 56 metadata.rootElements().stream() 57 .map(XTypeElement::getClassName) 58 .map(ClassName::canonicalName) 59 .collect(Collectors.toList())); 60 } 61 create(XTypeElement element, XProcessingEnv env)62 private static ProcessedRootSentinelMetadata create(XTypeElement element, XProcessingEnv env) { 63 XAnnotation annotationMirror = element.getAnnotation(ClassNames.PROCESSED_ROOT_SENTINEL); 64 65 return new AutoValue_ProcessedRootSentinelMetadata( 66 element, 67 annotationMirror.getAsStringList("roots").stream() 68 .map(env::requireTypeElement) 69 .collect(toImmutableSet())); 70 } 71 } 72