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 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 import javax.lang.model.element.Modifier;
28 
29 /** Generates an entry point that allows for injection of the given activity */
30 public final class InjectorEntryPointGenerator {
31   private final ProcessingEnvironment env;
32   private final AndroidEntryPointMetadata metadata;
33 
InjectorEntryPointGenerator( ProcessingEnvironment env, AndroidEntryPointMetadata metadata)34   public InjectorEntryPointGenerator(
35       ProcessingEnvironment env, AndroidEntryPointMetadata metadata) {
36     this.env = env;
37     this.metadata = metadata;
38   }
39 
40   // @Generated("InjectorEntryPointGenerator")
41   // @InstallIn({$SCOPES})
42   // public interface FooActivity_GeneratedInjector {
43   //   void injectFoo(FooActivity foo);
44   // }
generate()45   public void generate() throws IOException {
46     ClassName name = metadata.injectorClassName();
47     TypeSpec.Builder builder =
48         TypeSpec.interfaceBuilder(name.simpleName())
49             .addOriginatingElement(metadata.element())
50             .addAnnotation(Processors.getOriginatingElementAnnotation(metadata.element()))
51             .addAnnotation(ClassNames.GENERATED_ENTRY_POINT)
52             .addAnnotation(metadata.injectorInstallInAnnotation())
53             .addModifiers(Modifier.PUBLIC)
54             .addMethod(
55                 MethodSpec.methodBuilder(metadata.injectMethodName())
56                     .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
57                     .addParameter(
58                         metadata.elementClassName(),
59                         Processors.upperToLowerCamel(metadata.elementClassName().simpleName()))
60                     .build());
61 
62     Processors.addGeneratedAnnotation(builder, env, getClass());
63     Generators.copyLintAnnotations(metadata.element(), builder);
64 
65     JavaFile.builder(name.packageName(), builder.build()).build().writeTo(env.getFiler());
66   }
67 }
68