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