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.aliasof; 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 * A class that represents the values stored in an {@link 35 * dagger.hilt.internal.aliasof.AliasOfPropagatedData} annotation. 36 */ 37 @AutoValue 38 abstract class AliasOfPropagatedDataMetadata { 39 defineComponentScopeElement()40 abstract TypeElement defineComponentScopeElement(); 41 aliasElement()42 abstract TypeElement aliasElement(); 43 from(Elements elements)44 static ImmutableSet<AliasOfPropagatedDataMetadata> from(Elements elements) { 45 return AggregatedElements.from( 46 ClassNames.ALIAS_OF_PROPAGATED_DATA_PACKAGE, 47 ClassNames.ALIAS_OF_PROPAGATED_DATA, 48 elements) 49 .stream() 50 .map(aggregatedElement -> create(aggregatedElement, elements)) 51 .collect(toImmutableSet()); 52 } 53 create(TypeElement element, Elements elements)54 private static AliasOfPropagatedDataMetadata create(TypeElement element, Elements elements) { 55 AnnotationMirror annotationMirror = 56 Processors.getAnnotationMirror(element, ClassNames.ALIAS_OF_PROPAGATED_DATA); 57 58 ImmutableMap<String, AnnotationValue> values = 59 Processors.getAnnotationValues(elements, annotationMirror); 60 61 return new AutoValue_AliasOfPropagatedDataMetadata( 62 AnnotationValues.getTypeElement(values.get("defineComponentScope")), 63 AnnotationValues.getTypeElement(values.get("alias"))); 64 } 65 } 66