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