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 com.squareup.javapoet.ClassName; 20 import com.squareup.javapoet.JavaFile; 21 import com.squareup.javapoet.MethodSpec; 22 import com.squareup.javapoet.TypeSpec; 23 import dagger.hilt.processor.internal.ClassNames; 24 import dagger.hilt.processor.internal.Processors; 25 import java.io.IOException; 26 import javax.annotation.processing.ProcessingEnvironment; 27 28 /** Generator for the {@code EarlySingletonComponentCreator}. */ 29 final class EarlySingletonComponentCreatorGenerator { 30 private static final ClassName EARLY_SINGLETON_COMPONENT_CREATOR = 31 ClassName.get("dagger.hilt.android.internal.testing", "EarlySingletonComponentCreator"); 32 private static final ClassName EARLY_SINGLETON_COMPONENT_CREATOR_IMPL = 33 ClassName.get( 34 "dagger.hilt.android.internal.testing", "EarlySingletonComponentCreatorImpl"); 35 private static final ClassName DEFAULT_COMPONENT_IMPL = 36 ClassName.get( 37 "dagger.hilt.android.internal.testing.root", "DaggerDefault_HiltComponents_SingletonC"); 38 generate(ProcessingEnvironment env)39 static void generate(ProcessingEnvironment env) throws IOException { 40 TypeSpec.Builder builder = 41 TypeSpec.classBuilder(EARLY_SINGLETON_COMPONENT_CREATOR_IMPL) 42 .superclass(EARLY_SINGLETON_COMPONENT_CREATOR) 43 .addMethod( 44 MethodSpec.methodBuilder("create") 45 .returns(ClassName.OBJECT) 46 .addStatement( 47 "return $T.builder()\n" 48 + ".applicationContextModule(new $T($T.getApplicationContext()))\n" 49 + ".build()", 50 DEFAULT_COMPONENT_IMPL, 51 ClassNames.APPLICATION_CONTEXT_MODULE, 52 ClassNames.APPLICATION_PROVIDER) 53 .build()); 54 55 Processors.addGeneratedAnnotation(builder, env, ClassNames.ROOT_PROCESSOR.toString()); 56 57 JavaFile.builder(EARLY_SINGLETON_COMPONENT_CREATOR_IMPL.packageName(), builder.build()) 58 .build() 59 .writeTo(env.getFiler()); 60 } 61 EarlySingletonComponentCreatorGenerator()62 private EarlySingletonComponentCreatorGenerator() {} 63 } 64