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.internal.codegen.extension.DaggerStreams.toImmutableSet; 22 23 import androidx.room.compiler.processing.XAnnotation; 24 import androidx.room.compiler.processing.XProcessingEnv; 25 import androidx.room.compiler.processing.XTypeElement; 26 import com.google.auto.value.AutoValue; 27 import com.google.common.collect.ImmutableSet; 28 import com.squareup.javapoet.ClassName; 29 import dagger.hilt.processor.internal.ClassNames; 30 import dagger.hilt.processor.internal.aggregateddeps.AggregatedDepsMetadata; 31 import dagger.hilt.processor.internal.root.ir.ComponentTreeDepsIr; 32 import dagger.internal.codegen.xprocessing.XAnnotations; 33 34 /** 35 * Represents the values stored in an {@link 36 * dagger.hilt.internal.componenttreedeps.ComponentTreeDeps}. 37 * 38 * <p>This class is used in both writing ({@link ComponentTreeDepsGenerator}) and reading ({@link 39 * ComponentTreeDepsProcessor}) of the {@code @ComponentTreeDeps} annotation. 40 */ 41 @AutoValue 42 abstract class ComponentTreeDepsMetadata { 43 /** 44 * Returns the name of the element annotated with {@link 45 * dagger.hilt.internal.componenttreedeps.ComponentTreeDeps}. 46 */ name()47 abstract ClassName name(); 48 49 /** Returns the {@link dagger.hilt.internal.aggregatedroot.AggregatedRoot} deps. */ aggregatedRootDeps()50 abstract ImmutableSet<XTypeElement> aggregatedRootDeps(); 51 52 /** Returns the {@link dagger.hilt.internal.definecomponent.DefineComponentClasses} deps. */ defineComponentDeps()53 abstract ImmutableSet<XTypeElement> defineComponentDeps(); 54 55 /** Returns the {@link dagger.hilt.internal.aliasof.AliasOfPropagatedData} deps. */ aliasOfDeps()56 abstract ImmutableSet<XTypeElement> aliasOfDeps(); 57 58 /** Returns the {@link dagger.hilt.internal.aggregateddeps.AggregatedDeps} deps. */ aggregatedDeps()59 abstract ImmutableSet<XTypeElement> aggregatedDeps(); 60 61 /** Returns the {@link dagger.hilt.android.uninstallmodules.AggregatedUninstallModules} deps. */ aggregatedUninstallModulesDeps()62 abstract ImmutableSet<XTypeElement> aggregatedUninstallModulesDeps(); 63 64 /** Returns the {@link dagger.hilt.android.earlyentrypoint.AggregatedEarlyEntryPoint} deps. */ aggregatedEarlyEntryPointDeps()65 abstract ImmutableSet<XTypeElement> aggregatedEarlyEntryPointDeps(); 66 from(XTypeElement element, XProcessingEnv env)67 static ComponentTreeDepsMetadata from(XTypeElement element, XProcessingEnv env) { 68 checkArgument(element.hasAnnotation(ClassNames.COMPONENT_TREE_DEPS)); 69 XAnnotation annotation = element.getAnnotation(ClassNames.COMPONENT_TREE_DEPS); 70 71 return create( 72 element.getClassName(), 73 unwrapProxies(XAnnotations.getAsTypeElementList(annotation, "rootDeps")), 74 unwrapProxies(XAnnotations.getAsTypeElementList(annotation, "defineComponentDeps")), 75 unwrapProxies(XAnnotations.getAsTypeElementList(annotation, "aliasOfDeps")), 76 unwrapProxies(XAnnotations.getAsTypeElementList(annotation, "aggregatedDeps")), 77 unwrapProxies(XAnnotations.getAsTypeElementList(annotation, "uninstallModulesDeps")), 78 unwrapProxies(XAnnotations.getAsTypeElementList(annotation, "earlyEntryPointDeps"))); 79 } 80 from(ComponentTreeDepsIr ir, XProcessingEnv env)81 static ComponentTreeDepsMetadata from(ComponentTreeDepsIr ir, XProcessingEnv env) { 82 return create( 83 ir.getName(), 84 ir.getRootDeps().stream() 85 .map(it -> env.requireTypeElement(it.canonicalName())) 86 .collect(toImmutableSet()), 87 ir.getDefineComponentDeps().stream() 88 .map(it -> env.requireTypeElement(it.canonicalName())) 89 .collect(toImmutableSet()), 90 ir.getAliasOfDeps().stream() 91 .map(it -> env.requireTypeElement(it.canonicalName())) 92 .collect(toImmutableSet()), 93 ir.getAggregatedDeps().stream() 94 .map(it -> env.requireTypeElement(it.canonicalName())) 95 .collect(toImmutableSet()), 96 ir.getUninstallModulesDeps().stream() 97 .map(it -> env.requireTypeElement(it.canonicalName())) 98 .collect(toImmutableSet()), 99 ir.getEarlyEntryPointDeps().stream() 100 .map(it -> env.requireTypeElement(it.canonicalName())) 101 .collect(toImmutableSet())); 102 } 103 104 /** Returns all modules included in a component tree deps. */ modules()105 public ImmutableSet<XTypeElement> modules() { 106 return AggregatedDepsMetadata.from(aggregatedDeps()).stream() 107 .filter(AggregatedDepsMetadata::isModule) 108 .map(AggregatedDepsMetadata::dependency) 109 .collect(toImmutableSet()); 110 } 111 112 /** Returns all entry points included in a component tree deps. */ entrypoints()113 public ImmutableSet<XTypeElement> entrypoints() { 114 return AggregatedDepsMetadata.from(aggregatedDeps()).stream() 115 .filter(dependency -> !dependency.isModule()) 116 .map(AggregatedDepsMetadata::dependency) 117 .collect(toImmutableSet()); 118 } 119 create( ClassName name, ImmutableSet<XTypeElement> aggregatedRootDeps, ImmutableSet<XTypeElement> defineComponentDeps, ImmutableSet<XTypeElement> aliasOfDeps, ImmutableSet<XTypeElement> aggregatedDeps, ImmutableSet<XTypeElement> aggregatedUninstallModulesDeps, ImmutableSet<XTypeElement> aggregatedEarlyEntryPointDeps)120 static ComponentTreeDepsMetadata create( 121 ClassName name, 122 ImmutableSet<XTypeElement> aggregatedRootDeps, 123 ImmutableSet<XTypeElement> defineComponentDeps, 124 ImmutableSet<XTypeElement> aliasOfDeps, 125 ImmutableSet<XTypeElement> aggregatedDeps, 126 ImmutableSet<XTypeElement> aggregatedUninstallModulesDeps, 127 ImmutableSet<XTypeElement> aggregatedEarlyEntryPointDeps) { 128 return new AutoValue_ComponentTreeDepsMetadata( 129 name, 130 aggregatedRootDeps, 131 defineComponentDeps, 132 aliasOfDeps, 133 aggregatedDeps, 134 aggregatedUninstallModulesDeps, 135 aggregatedEarlyEntryPointDeps); 136 } 137 } 138