• 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 entrypoint that includes a user's pkg-private entrypoint. This allows a
29  * user's entrypoint to use pkg-private visibility to hide from external packages.
30  */
31 final class PkgPrivateEntryPointGenerator {
32   private final ProcessingEnvironment env;
33   private final PkgPrivateMetadata metadata;
34 
PkgPrivateEntryPointGenerator(ProcessingEnvironment env, PkgPrivateMetadata metadata)35   PkgPrivateEntryPointGenerator(ProcessingEnvironment env, PkgPrivateMetadata metadata) {
36     this.env = env;
37     this.metadata = metadata;
38   }
39 
40   // This method creates the following generated code for an EntryPoint in pkg.MyEntryPoint that is
41   // package
42   // private
43   //
44   // package pkg; //same package
45   //
46   // import dagger.hilt.InstallIn;
47   // import dagger.hilt.EntryPoint;;
48   // import javax.annotation.Generated;
49   //
50   // @Generated("dagger.hilt.processor.internal.aggregateddeps.PkgPrivateEntryPointGenerator")
51   // @InstallIn(InstallIn.Component.ACTIVITY)
52   // @EntryPoint
53   // public final class HiltWrapper_MyEntryPoint extends MyEntryPoint  {
54   // }
generate()55   void generate() throws IOException {
56 
57     TypeSpec.Builder entryPointInterfaceBuilder =
58         TypeSpec.interfaceBuilder(metadata.generatedClassName().simpleName())
59             .addOriginatingElement(metadata.getTypeElement())
60             .addAnnotation(Processors.getOriginatingElementAnnotation(metadata.getTypeElement()))
61             .addModifiers(Modifier.PUBLIC)
62             .addSuperinterface(metadata.baseClassName())
63             .addAnnotation(metadata.getAnnotation());
64 
65     Processors.addGeneratedAnnotation(entryPointInterfaceBuilder, env, getClass());
66 
67     if (metadata.getOptionalInstallInAnnotationMirror().isPresent()) {
68       entryPointInterfaceBuilder.addAnnotation(
69           AnnotationSpec.get(metadata.getOptionalInstallInAnnotationMirror().get()));
70     }
71 
72     JavaFile.builder(
73             metadata.generatedClassName().packageName(), entryPointInterfaceBuilder.build())
74         .build()
75         .writeTo(env.getFiler());
76   }
77 }
78