• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.android.processor.internal.androidentrypoint;
18 
19 import androidx.room.compiler.processing.JavaPoetExtKt;
20 import androidx.room.compiler.processing.XFiler;
21 import androidx.room.compiler.processing.XProcessingEnv;
22 import com.squareup.javapoet.ClassName;
23 import com.squareup.javapoet.JavaFile;
24 import com.squareup.javapoet.MethodSpec;
25 import com.squareup.javapoet.TypeSpec;
26 import dagger.hilt.processor.internal.ClassNames;
27 import dagger.hilt.processor.internal.Processors;
28 import java.io.IOException;
29 import javax.lang.model.element.Modifier;
30 
31 /** Generates an entry point that allows for injection of the given activity */
32 public final class InjectorEntryPointGenerator {
33 
34   private final XProcessingEnv env;
35   private final AndroidEntryPointMetadata metadata;
36 
InjectorEntryPointGenerator(XProcessingEnv env, AndroidEntryPointMetadata metadata)37   public InjectorEntryPointGenerator(XProcessingEnv env, AndroidEntryPointMetadata metadata) {
38     this.env = env;
39     this.metadata = metadata;
40   }
41 
42   // @Generated("InjectorEntryPointGenerator")
43   // @InstallIn({$SCOPES})
44   // public interface FooActivity_GeneratedInjector {
45   //   void injectFoo(FooActivity foo);
46   // }
generate()47   public void generate() throws IOException {
48     ClassName name = metadata.injectorClassName();
49     TypeSpec.Builder builder =
50         TypeSpec.interfaceBuilder(name.simpleName())
51             .addAnnotation(Processors.getOriginatingElementAnnotation(metadata.element()))
52             .addAnnotation(ClassNames.GENERATED_ENTRY_POINT)
53             .addAnnotation(metadata.injectorInstallInAnnotation())
54             .addModifiers(Modifier.PUBLIC)
55             .addMethod(
56                 MethodSpec.methodBuilder(metadata.injectMethodName())
57                     .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
58                     .addParameter(
59                         metadata.elementClassName(),
60                         Processors.upperToLowerCamel(metadata.elementClassName().simpleName()))
61                     .build());
62 
63     JavaPoetExtKt.addOriginatingElement(builder, metadata.element());
64     Processors.addGeneratedAnnotation(builder, env, getClass());
65     Generators.copyLintAnnotations(metadata.element(), builder);
66     Generators.copySuppressAnnotations(metadata.element(), builder);
67 
68     env.getFiler()
69         .write(
70             JavaFile.builder(name.packageName(), builder.build()).build(), XFiler.Mode.Isolating);
71   }
72 }
73