• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.uninstallmodules;
18 
19 import static androidx.room.compiler.processing.compat.XConverters.getProcessingEnv;
20 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList;
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.ImmutableList;
28 import com.google.common.collect.ImmutableSet;
29 import com.squareup.javapoet.ClassName;
30 import dagger.hilt.processor.internal.AggregatedElements;
31 import dagger.hilt.processor.internal.ClassNames;
32 import dagger.hilt.processor.internal.root.ir.AggregatedUninstallModulesIr;
33 import java.util.stream.Collectors;
34 
35 /**
36  * A class that represents the values stored in an
37  * {@link dagger.hilt.android.internal.uninstallmodules.AggregatedUninstallModules} annotation.
38  */
39 @AutoValue
40 public abstract class AggregatedUninstallModulesMetadata {
41 
42   /** Returns the aggregating element */
aggregatingElement()43   public abstract XTypeElement aggregatingElement();
44 
45   /** Returns the test annotated with {@link dagger.hilt.android.testing.UninstallModules}. */
testElement()46   public abstract XTypeElement testElement();
47 
48   /**
49    * Returns the list of uninstall modules in {@link dagger.hilt.android.testing.UninstallModules}.
50    */
uninstallModuleElements()51   public abstract ImmutableList<XTypeElement> uninstallModuleElements();
52 
53   /** Returns metadata for all aggregated elements in the aggregating package. */
from(XProcessingEnv env)54   public static ImmutableSet<AggregatedUninstallModulesMetadata> from(XProcessingEnv env) {
55     return from(
56         AggregatedElements.from(
57             ClassNames.AGGREGATED_UNINSTALL_MODULES_PACKAGE,
58             ClassNames.AGGREGATED_UNINSTALL_MODULES,
59             env));
60   }
61 
62   /** Returns metadata for each aggregated element. */
from( ImmutableSet<XTypeElement> aggregatedElements)63   public static ImmutableSet<AggregatedUninstallModulesMetadata> from(
64       ImmutableSet<XTypeElement> aggregatedElements) {
65     return aggregatedElements.stream()
66         .map(aggregatedElement -> create(aggregatedElement, getProcessingEnv(aggregatedElement)))
67         .collect(toImmutableSet());
68   }
69 
toIr(AggregatedUninstallModulesMetadata metadata)70   public static AggregatedUninstallModulesIr toIr(AggregatedUninstallModulesMetadata metadata) {
71     return new AggregatedUninstallModulesIr(
72         metadata.aggregatingElement().getClassName(),
73         metadata.testElement().getClassName().canonicalName(),
74         metadata.uninstallModuleElements().stream()
75             .map(XTypeElement::getClassName)
76             .map(ClassName::canonicalName)
77             .collect(Collectors.toList()));
78   }
79 
create( XTypeElement element, XProcessingEnv env)80   private static AggregatedUninstallModulesMetadata create(
81       XTypeElement element, XProcessingEnv env) {
82     XAnnotation annotationMirror = element.getAnnotation(ClassNames.AGGREGATED_UNINSTALL_MODULES);
83 
84     return new AutoValue_AggregatedUninstallModulesMetadata(
85         element,
86         env.requireTypeElement(annotationMirror.getAsString("test")),
87         annotationMirror.getAsStringList("uninstallModules").stream()
88             .map(env::requireTypeElement)
89             .collect(toImmutableList()));
90   }
91 }
92