• 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.definecomponent;
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 dagger.hilt.processor.internal.AggregatedElements;
25 import dagger.hilt.processor.internal.AnnotationValues;
26 import dagger.hilt.processor.internal.ClassNames;
27 import dagger.hilt.processor.internal.ProcessorErrors;
28 import dagger.hilt.processor.internal.Processors;
29 import javax.lang.model.element.AnnotationMirror;
30 import javax.lang.model.element.AnnotationValue;
31 import javax.lang.model.element.TypeElement;
32 import javax.lang.model.util.Elements;
33 
34 /**
35  * A class that represents the values stored in an {@link
36  * dagger.hilt.internal.definecomponent.DefineComponentClasses} annotation.
37  */
38 @AutoValue
39 abstract class DefineComponentClassesMetadata {
40 
41   /**
42    * Returns the element annotated with {@code dagger.hilt.internal.definecomponent.DefineComponent}
43    * or {@code dagger.hilt.internal.definecomponent.DefineComponent.Builder}.
44    */
element()45   abstract TypeElement element();
46 
47   /** Returns {@code true} if this element represents a component. */
isComponent()48   abstract boolean isComponent();
49 
50   /** Returns {@code true} if this element represents a component builder. */
isComponentBuilder()51   boolean isComponentBuilder() {
52     return !isComponent();
53   }
54 
from(Elements elements)55   static ImmutableSet<DefineComponentClassesMetadata> from(Elements elements) {
56     return AggregatedElements.from(
57             ClassNames.DEFINE_COMPONENT_CLASSES_PACKAGE,
58             ClassNames.DEFINE_COMPONENT_CLASSES,
59             elements)
60         .stream()
61         .map(aggregatedElement -> create(aggregatedElement, elements))
62         .collect(toImmutableSet());
63   }
64 
create(TypeElement element, Elements elements)65   private static DefineComponentClassesMetadata create(TypeElement element, Elements elements) {
66     AnnotationMirror annotationMirror =
67         Processors.getAnnotationMirror(element, ClassNames.DEFINE_COMPONENT_CLASSES);
68 
69     ImmutableMap<String, AnnotationValue> values =
70         Processors.getAnnotationValues(elements, annotationMirror);
71 
72     String componentName = AnnotationValues.getString(values.get("component"));
73     String builderName = AnnotationValues.getString(values.get("builder"));
74 
75     ProcessorErrors.checkState(
76         !(componentName.isEmpty() && builderName.isEmpty()),
77         element,
78         "@DefineComponentClasses missing both `component` and `builder` members.");
79 
80     ProcessorErrors.checkState(
81         componentName.isEmpty() || builderName.isEmpty(),
82         element,
83         "@DefineComponentClasses should not include both `component` and `builder` members.");
84 
85     boolean isComponent = !componentName.isEmpty();
86     String componentOrBuilderName = isComponent ? componentName : builderName;
87     TypeElement componentOrBuilderElement = elements.getTypeElement(componentOrBuilderName);
88     ProcessorErrors.checkState(
89         componentOrBuilderElement != null,
90         componentOrBuilderElement,
91         "%s.%s(), has invalid value: `%s`.",
92         ClassNames.DEFINE_COMPONENT_CLASSES.simpleName(),
93         isComponent ? "component" : "builder",
94         componentOrBuilderName);
95     return new AutoValue_DefineComponentClassesMetadata(componentOrBuilderElement, isComponent);
96   }
97 }
98