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.earlyentrypoint; 18 19 import static androidx.room.compiler.processing.compat.XConverters.getProcessingEnv; 20 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet; 21 22 import androidx.room.compiler.processing.XAnnotation; 23 import androidx.room.compiler.processing.XProcessingEnv; 24 import androidx.room.compiler.processing.XTypeElement; 25 import com.google.auto.value.AutoValue; 26 import com.google.common.collect.ImmutableSet; 27 import dagger.hilt.processor.internal.AggregatedElements; 28 import dagger.hilt.processor.internal.ClassNames; 29 import dagger.hilt.processor.internal.root.ir.AggregatedEarlyEntryPointIr; 30 31 /** 32 * A class that represents the values stored in an {@link 33 * dagger.hilt.android.internal.earlyentrypoint.AggregatedEarlyEntryPoint} annotation. 34 */ 35 @AutoValue 36 public abstract class AggregatedEarlyEntryPointMetadata { 37 38 /** Returns the aggregating element */ aggregatingElement()39 public abstract XTypeElement aggregatingElement(); 40 41 /** Returns the element annotated with {@link dagger.hilt.android.EarlyEntryPoint}. */ earlyEntryPoint()42 public abstract XTypeElement earlyEntryPoint(); 43 44 /** Returns metadata for all aggregated elements in the aggregating package. */ from(XProcessingEnv env)45 public static ImmutableSet<AggregatedEarlyEntryPointMetadata> from(XProcessingEnv env) { 46 return from( 47 AggregatedElements.from( 48 ClassNames.AGGREGATED_EARLY_ENTRY_POINT_PACKAGE, 49 ClassNames.AGGREGATED_EARLY_ENTRY_POINT, 50 env)); 51 } 52 53 /** Returns metadata for each aggregated element. */ from( ImmutableSet<XTypeElement> aggregatedElements)54 public static ImmutableSet<AggregatedEarlyEntryPointMetadata> from( 55 ImmutableSet<XTypeElement> aggregatedElements) { 56 return aggregatedElements.stream() 57 .map(aggregatedElement -> create(aggregatedElement, getProcessingEnv(aggregatedElement))) 58 .collect(toImmutableSet()); 59 } 60 toIr(AggregatedEarlyEntryPointMetadata metadata)61 public static AggregatedEarlyEntryPointIr toIr(AggregatedEarlyEntryPointMetadata metadata) { 62 return new AggregatedEarlyEntryPointIr( 63 metadata.aggregatingElement().getClassName(), 64 metadata.earlyEntryPoint().getClassName().canonicalName()); 65 } 66 create( XTypeElement element, XProcessingEnv env)67 private static AggregatedEarlyEntryPointMetadata create( 68 XTypeElement element, XProcessingEnv env) { 69 XAnnotation annotation = element.getAnnotation(ClassNames.AGGREGATED_EARLY_ENTRY_POINT); 70 71 return new AutoValue_AggregatedEarlyEntryPointMetadata( 72 element, env.requireTypeElement(annotation.getAsString("earlyEntryPoint"))); 73 } 74 } 75