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