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 com.google.common.base.Preconditions.checkArgument; 20 import static dagger.hilt.processor.internal.AggregatedElements.unwrapProxies; 21 import static dagger.hilt.processor.internal.AnnotationValues.getTypeElements; 22 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet; 23 24 import com.google.auto.value.AutoValue; 25 import com.google.common.collect.ImmutableMap; 26 import com.google.common.collect.ImmutableSet; 27 import com.squareup.javapoet.ClassName; 28 import dagger.hilt.processor.internal.ClassNames; 29 import dagger.hilt.processor.internal.Processors; 30 import dagger.hilt.processor.internal.root.ir.ComponentTreeDepsIr; 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.componenttreedeps.ComponentTreeDeps}. 39 * 40 * <p>This class is used in both writing ({@link ComponentTreeDepsGenerator}) and reading ({@link 41 * ComponentTreeDepsProcessor}) of the {@code @ComponentTreeDeps} annotation. 42 */ 43 @AutoValue 44 abstract class ComponentTreeDepsMetadata { 45 /** 46 * Returns the name of the element annotated with {@link 47 * dagger.hilt.internal.componenttreedeps.ComponentTreeDeps}. 48 */ name()49 abstract ClassName name(); 50 51 /** Returns the {@link dagger.hilt.internal.aggregatedroot.AggregatedRoot} deps. */ aggregatedRootDeps()52 abstract ImmutableSet<TypeElement> aggregatedRootDeps(); 53 54 /** Returns the {@link dagger.hilt.internal.definecomponent.DefineComponentClasses} deps. */ defineComponentDeps()55 abstract ImmutableSet<TypeElement> defineComponentDeps(); 56 57 /** Returns the {@link dagger.hilt.internal.aliasof.AliasOfPropagatedData} deps. */ aliasOfDeps()58 abstract ImmutableSet<TypeElement> aliasOfDeps(); 59 60 /** Returns the {@link dagger.hilt.internal.aggregateddeps.AggregatedDeps} deps. */ aggregatedDeps()61 abstract ImmutableSet<TypeElement> aggregatedDeps(); 62 63 /** Returns the {@link dagger.hilt.android.uninstallmodules.AggregatedUninstallModules} deps. */ aggregatedUninstallModulesDeps()64 abstract ImmutableSet<TypeElement> aggregatedUninstallModulesDeps(); 65 66 /** Returns the {@link dagger.hilt.android.earlyentrypoint.AggregatedEarlyEntryPoint} deps. */ aggregatedEarlyEntryPointDeps()67 abstract ImmutableSet<TypeElement> aggregatedEarlyEntryPointDeps(); 68 from(TypeElement element, Elements elements)69 static ComponentTreeDepsMetadata from(TypeElement element, Elements elements) { 70 checkArgument(Processors.hasAnnotation(element, ClassNames.COMPONENT_TREE_DEPS)); 71 AnnotationMirror annotationMirror = 72 Processors.getAnnotationMirror(element, ClassNames.COMPONENT_TREE_DEPS); 73 74 ImmutableMap<String, AnnotationValue> values = 75 Processors.getAnnotationValues(elements, annotationMirror); 76 77 return create( 78 ClassName.get(element), 79 unwrapProxies(getTypeElements(values.get("rootDeps")), elements), 80 unwrapProxies(getTypeElements(values.get("defineComponentDeps")), elements), 81 unwrapProxies(getTypeElements(values.get("aliasOfDeps")), elements), 82 unwrapProxies(getTypeElements(values.get("aggregatedDeps")), elements), 83 unwrapProxies(getTypeElements(values.get("uninstallModulesDeps")), elements), 84 unwrapProxies(getTypeElements(values.get("earlyEntryPointDeps")), elements)); 85 } 86 from(ComponentTreeDepsIr ir, Elements elements)87 static ComponentTreeDepsMetadata from(ComponentTreeDepsIr ir, Elements elements) { 88 return create( 89 ir.getName(), 90 ir.getRootDeps().stream() 91 .map(it -> elements.getTypeElement(it.canonicalName())) 92 .collect(toImmutableSet()), 93 ir.getDefineComponentDeps().stream() 94 .map(it -> elements.getTypeElement(it.canonicalName())) 95 .collect(toImmutableSet()), 96 ir.getAliasOfDeps().stream() 97 .map(it -> elements.getTypeElement(it.canonicalName())) 98 .collect(toImmutableSet()), 99 ir.getAggregatedDeps().stream() 100 .map(it -> elements.getTypeElement(it.canonicalName())) 101 .collect(toImmutableSet()), 102 ir.getUninstallModulesDeps().stream() 103 .map(it -> elements.getTypeElement(it.canonicalName())) 104 .collect(toImmutableSet()), 105 ir.getEarlyEntryPointDeps().stream() 106 .map(it -> elements.getTypeElement(it.canonicalName())) 107 .collect(toImmutableSet())); 108 } 109 create( ClassName name, ImmutableSet<TypeElement> aggregatedRootDeps, ImmutableSet<TypeElement> defineComponentDeps, ImmutableSet<TypeElement> aliasOfDeps, ImmutableSet<TypeElement> aggregatedDeps, ImmutableSet<TypeElement> aggregatedUninstallModulesDeps, ImmutableSet<TypeElement> aggregatedEarlyEntryPointDeps)110 static ComponentTreeDepsMetadata create( 111 ClassName name, 112 ImmutableSet<TypeElement> aggregatedRootDeps, 113 ImmutableSet<TypeElement> defineComponentDeps, 114 ImmutableSet<TypeElement> aliasOfDeps, 115 ImmutableSet<TypeElement> aggregatedDeps, 116 ImmutableSet<TypeElement> aggregatedUninstallModulesDeps, 117 ImmutableSet<TypeElement> aggregatedEarlyEntryPointDeps) { 118 return new AutoValue_ComponentTreeDepsMetadata( 119 name, 120 aggregatedRootDeps, 121 defineComponentDeps, 122 aliasOfDeps, 123 aggregatedDeps, 124 aggregatedUninstallModulesDeps, 125 aggregatedEarlyEntryPointDeps); 126 } 127 } 128