• 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.earlyentrypoint;
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 com.squareup.javapoet.ClassName;
25 import dagger.hilt.processor.internal.AggregatedElements;
26 import dagger.hilt.processor.internal.AnnotationValues;
27 import dagger.hilt.processor.internal.ClassNames;
28 import dagger.hilt.processor.internal.Processors;
29 import dagger.hilt.processor.internal.root.ir.AggregatedEarlyEntryPointIr;
30 import javax.lang.model.element.AnnotationMirror;
31 import javax.lang.model.element.AnnotationValue;
32 import javax.lang.model.element.TypeElement;
33 import javax.lang.model.util.Elements;
34 
35 /**
36  * A class that represents the values stored in an {@link
37  * dagger.hilt.android.internal.earlyentrypoint.AggregatedEarlyEntryPoint} annotation.
38  */
39 @AutoValue
40 public abstract class AggregatedEarlyEntryPointMetadata {
41 
42   /** Returns the aggregating element */
aggregatingElement()43   public abstract TypeElement aggregatingElement();
44 
45   /** Returns the element annotated with {@link dagger.hilt.android.EarlyEntryPoint}. */
earlyEntryPoint()46   public abstract TypeElement earlyEntryPoint();
47 
48   /** Returns metadata for all aggregated elements in the aggregating package. */
from(Elements elements)49   public static ImmutableSet<AggregatedEarlyEntryPointMetadata> from(Elements elements) {
50     return from(
51         AggregatedElements.from(
52             ClassNames.AGGREGATED_EARLY_ENTRY_POINT_PACKAGE,
53             ClassNames.AGGREGATED_EARLY_ENTRY_POINT,
54             elements),
55         elements);
56   }
57 
58   /** Returns metadata for each aggregated element. */
from( ImmutableSet<TypeElement> aggregatedElements, Elements elements)59   public static ImmutableSet<AggregatedEarlyEntryPointMetadata> from(
60       ImmutableSet<TypeElement> aggregatedElements, Elements elements) {
61     return aggregatedElements.stream()
62         .map(aggregatedElement -> create(aggregatedElement, elements))
63         .collect(toImmutableSet());
64   }
65 
toIr(AggregatedEarlyEntryPointMetadata metadata)66   public static AggregatedEarlyEntryPointIr toIr(AggregatedEarlyEntryPointMetadata metadata) {
67     return new AggregatedEarlyEntryPointIr(
68         ClassName.get(metadata.aggregatingElement()),
69         ClassName.get(metadata.earlyEntryPoint()));
70   }
71 
create(TypeElement element, Elements elements)72   private static AggregatedEarlyEntryPointMetadata create(TypeElement element, Elements elements) {
73     AnnotationMirror annotationMirror =
74         Processors.getAnnotationMirror(element, ClassNames.AGGREGATED_EARLY_ENTRY_POINT);
75 
76     ImmutableMap<String, AnnotationValue> values =
77         Processors.getAnnotationValues(elements, annotationMirror);
78 
79     return new AutoValue_AggregatedEarlyEntryPointMetadata(
80         element,
81         elements.getTypeElement(AnnotationValues.getString(values.get("earlyEntryPoint"))));
82   }
83 }
84