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