• 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.root;
18 
19 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
20 
21 import androidx.room.compiler.processing.XAnnotation;
22 import androidx.room.compiler.processing.XProcessingEnv;
23 import androidx.room.compiler.processing.XTypeElement;
24 import com.google.auto.value.AutoValue;
25 import com.google.auto.value.extension.memoized.Memoized;
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.AggregatedRootIr;
30 
31 /**
32  * Represents the values stored in an {@link dagger.hilt.internal.aggregatedroot.AggregatedRoot}.
33  */
34 @AutoValue
35 abstract class AggregatedRootMetadata {
36 
37   /** Returns the aggregating element */
aggregatingElement()38   public abstract XTypeElement aggregatingElement();
39 
40   /** Returns the element that was annotated with the root annotation. */
rootElement()41   abstract XTypeElement rootElement();
42 
43   /**
44    * Returns the originating root element. In most cases this will be the same as {@link
45    * #rootElement()}.
46    */
originatingRootElement()47   abstract XTypeElement originatingRootElement();
48 
49   /** Returns the root annotation as an element. */
rootAnnotation()50   abstract XTypeElement rootAnnotation();
51 
52   /** Returns whether this root can use a shared component. */
allowsSharingComponent()53   abstract boolean allowsSharingComponent();
54 
55   @Memoized
rootType()56   RootType rootType() {
57     return RootType.of(rootElement());
58   }
59 
from(XProcessingEnv env)60   static ImmutableSet<AggregatedRootMetadata> from(XProcessingEnv env) {
61     return from(
62         AggregatedElements.from(
63             ClassNames.AGGREGATED_ROOT_PACKAGE, ClassNames.AGGREGATED_ROOT, env),
64         env);
65   }
66 
67   /** Returns metadata for each aggregated element. */
from( ImmutableSet<XTypeElement> aggregatedElements, XProcessingEnv env)68   public static ImmutableSet<AggregatedRootMetadata> from(
69       ImmutableSet<XTypeElement> aggregatedElements, XProcessingEnv env) {
70     return aggregatedElements.stream()
71         .map(aggregatedElement -> create(aggregatedElement, env))
72         .collect(toImmutableSet());
73   }
74 
toIr(AggregatedRootMetadata metadata)75   public static AggregatedRootIr toIr(AggregatedRootMetadata metadata) {
76     return new AggregatedRootIr(
77         metadata.aggregatingElement().getClassName(),
78         metadata.rootElement().getClassName(),
79         metadata.originatingRootElement().getClassName(),
80         metadata.rootAnnotation().getClassName(),
81         metadata.allowsSharingComponent());
82   }
83 
create(XTypeElement element, XProcessingEnv env)84   private static AggregatedRootMetadata create(XTypeElement element, XProcessingEnv env) {
85     XAnnotation annotation = element.getAnnotation(ClassNames.AGGREGATED_ROOT);
86 
87     XTypeElement rootElement = env.requireTypeElement(annotation.getAsString("root"));
88     boolean allowSharingComponent = true;
89     return new AutoValue_AggregatedRootMetadata(
90         element,
91         rootElement,
92         env.requireTypeElement(annotation.getAsString("originatingRoot")),
93         annotation.getAsType("rootAnnotation").getTypeElement(),
94         allowSharingComponent);
95   }
96 }
97