1 /* 2 * Copyright (C) 2020 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 androidx.room.compiler.processing.JavaPoetExtKt; 20 import androidx.room.compiler.processing.XFiler.Mode; 21 import androidx.room.compiler.processing.XProcessingEnv; 22 import androidx.room.compiler.processing.XTypeElement; 23 import com.squareup.javapoet.AnnotationSpec; 24 import com.squareup.javapoet.ClassName; 25 import com.squareup.javapoet.JavaFile; 26 import com.squareup.javapoet.MethodSpec; 27 import com.squareup.javapoet.TypeSpec; 28 import dagger.hilt.processor.internal.ClassNames; 29 import dagger.hilt.processor.internal.Processors; 30 import java.io.IOException; 31 import javax.lang.model.element.Modifier; 32 33 /** Generates an entry point for a test. */ 34 public final class TestInjectorGenerator { 35 private final XProcessingEnv env; 36 private final TestRootMetadata metadata; 37 TestInjectorGenerator(XProcessingEnv env, TestRootMetadata metadata)38 TestInjectorGenerator(XProcessingEnv env, TestRootMetadata metadata) { 39 this.env = env; 40 this.metadata = metadata; 41 } 42 43 // @GeneratedEntryPoint 44 // @InstallIn(SingletonComponent.class) 45 // public interface FooTest_GeneratedInjector { 46 // void injectTest(FooTest fooTest); 47 // } generate()48 public void generate() throws IOException { 49 TypeSpec.Builder builder = 50 TypeSpec.interfaceBuilder(metadata.testInjectorName()) 51 .addAnnotation(Processors.getOriginatingElementAnnotation(metadata.testElement())) 52 .addAnnotation(ClassNames.GENERATED_ENTRY_POINT) 53 .addAnnotation( 54 AnnotationSpec.builder(ClassNames.INSTALL_IN) 55 .addMember("value", "$T.class", installInComponent(metadata.testElement())) 56 .build()) 57 .addModifiers(Modifier.PUBLIC) 58 .addMethod( 59 MethodSpec.methodBuilder("injectTest") 60 .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) 61 .addParameter( 62 metadata.testName(), 63 Processors.upperToLowerCamel(metadata.testName().simpleName())) 64 .build()); 65 JavaPoetExtKt.addOriginatingElement(builder, metadata.testElement()); 66 67 Processors.addGeneratedAnnotation(builder, env, getClass()); 68 69 env.getFiler() 70 .write( 71 JavaFile.builder(metadata.testInjectorName().packageName(), builder.build()).build(), 72 Mode.Isolating); 73 } 74 installInComponent(XTypeElement testElement)75 private static ClassName installInComponent(XTypeElement testElement) { 76 return ClassNames.SINGLETON_COMPONENT; 77 } 78 } 79