• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.aggregateddeps;
18 
19 import com.squareup.javapoet.AnnotationSpec;
20 import com.squareup.javapoet.JavaFile;
21 import com.squareup.javapoet.TypeSpec;
22 import dagger.hilt.processor.internal.Processors;
23 import java.io.IOException;
24 import javax.annotation.processing.ProcessingEnvironment;
25 import javax.lang.model.element.Modifier;
26 
27 /**
28  * Generates a public Dagger module that includes a user's pkg-private module. This allows a user's
29  * module to use pkg-private visibility to hide from external packages, but still allows Hilt to
30  * install the module when the component is created in another package.
31  */
32 final class PkgPrivateModuleGenerator {
33   private final ProcessingEnvironment env;
34   private final PkgPrivateMetadata metadata;
35 
PkgPrivateModuleGenerator(ProcessingEnvironment env, PkgPrivateMetadata metadata)36   PkgPrivateModuleGenerator(ProcessingEnvironment env, PkgPrivateMetadata metadata) {
37     this.env = env;
38     this.metadata = metadata;
39   }
40 
41   // This method creates the following generated code for a pkg-private module, pkg.MyModule:
42   //
43   // package pkg; //same as module
44   //
45   // import dagger.Module;
46   // import dagger.hilt.InstallIn;
47   // import javax.annotation.Generated;
48   //
49   // @Generated("dagger.hilt.processor.internal.aggregateddeps.PkgPrivateModuleGenerator")
50   // @InstallIn(ActivityComponent.class)
51   // @Module(includes = MyModule.class)
52   // public final class HiltModuleWrapper_MyModule {}
generate()53   void generate() throws IOException {
54     TypeSpec.Builder builder =
55         TypeSpec.classBuilder(metadata.generatedClassName().simpleName())
56             .addOriginatingElement(metadata.getTypeElement())
57             .addAnnotation(Processors.getOriginatingElementAnnotation(metadata.getTypeElement()))
58             .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
59             // generated @InstallIn is exactly the same as the module being processed
60             .addAnnotation(
61                 AnnotationSpec.get(metadata.getOptionalInstallInAnnotationMirror().get()))
62             .addAnnotation(
63                 AnnotationSpec.builder(metadata.getAnnotation())
64                     .addMember("includes", "$T.class", metadata.getTypeElement())
65                     .build());
66 
67     Processors.addGeneratedAnnotation(builder, env, getClass());
68 
69     JavaFile.builder(metadata.generatedClassName().packageName(), builder.build())
70         .build()
71         .writeTo(env.getFiler());
72   }
73 }
74