• 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 androidx.room.compiler.processing.XTypeElement;
20 import com.squareup.javapoet.AnnotationSpec;
21 import dagger.hilt.processor.internal.ClassNames;
22 import dagger.hilt.processor.internal.Processors;
23 
24 /** Generates an {@link dagger.hilt.internal.aggregatedroot.AggregatedRoot}. */
25 final class AggregatedRootGenerator {
26   private final XTypeElement rootElement;
27   private final XTypeElement originatingRootElement;
28   private final XTypeElement rootAnnotation;
29 
AggregatedRootGenerator( XTypeElement rootElement, XTypeElement originatingRootElement, XTypeElement rootAnnotation)30   AggregatedRootGenerator(
31       XTypeElement rootElement, XTypeElement originatingRootElement, XTypeElement rootAnnotation) {
32     this.rootElement = rootElement;
33     this.originatingRootElement = originatingRootElement;
34     this.rootAnnotation = rootAnnotation;
35   }
36 
generate()37   void generate() {
38     AnnotationSpec.Builder aggregatedRootAnnotation =
39         AnnotationSpec.builder(ClassNames.AGGREGATED_ROOT)
40             .addMember("root", "$S", rootElement.getQualifiedName())
41             .addMember("rootPackage", "$S", rootElement.getClassName().packageName())
42             .addMember("originatingRoot", "$S", originatingRootElement.getQualifiedName())
43             .addMember(
44                 "originatingRootPackage", "$S", originatingRootElement.getClassName().packageName())
45             .addMember("rootAnnotation", "$T.class", rootAnnotation.getClassName());
46     rootElement
47         .getClassName()
48         .simpleNames()
49         .forEach(name -> aggregatedRootAnnotation.addMember("rootSimpleNames", "$S", name));
50     originatingRootElement
51         .getClassName()
52         .simpleNames()
53         .forEach(
54             name -> aggregatedRootAnnotation.addMember("originatingRootSimpleNames", "$S", name));
55     Processors.generateAggregatingClass(
56         ClassNames.AGGREGATED_ROOT_PACKAGE,
57         aggregatedRootAnnotation.build(),
58         rootElement,
59         getClass());
60   }
61 }
62