forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ActivityGenerator.java
122 lines (106 loc) · 4.52 KB
/
ActivityGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (C) 2019 The Dagger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dagger.hilt.android.processor.internal.androidentrypoint;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.TypeVariableName;
import dagger.hilt.android.processor.internal.AndroidClassNames;
import dagger.hilt.processor.internal.Processors;
import java.io.IOException;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Modifier;
/** Generates an Hilt Activity class for the @AndroidEntryPoint annotated class. */
public final class ActivityGenerator {
private final ProcessingEnvironment env;
private final AndroidEntryPointMetadata metadata;
private final ClassName generatedClassName;
public ActivityGenerator(ProcessingEnvironment env, AndroidEntryPointMetadata metadata) {
this.env = env;
this.metadata = metadata;
generatedClassName = metadata.generatedClassName();
}
// @Generated("ActivityGenerator")
// abstract class Hilt_$CLASS extends $BASE implements ComponentManager<?> {
// ...
// }
public void generate() throws IOException {
TypeSpec.Builder builder =
TypeSpec.classBuilder(generatedClassName.simpleName())
.addOriginatingElement(metadata.element())
.superclass(metadata.baseClassName())
.addModifiers(metadata.generatedClassModifiers());
Generators.addGeneratedBaseClassJavadoc(builder, AndroidClassNames.ANDROID_ENTRY_POINT);
Processors.addGeneratedAnnotation(builder, env, getClass());
Generators.copyConstructors(
metadata.baseElement(), CodeBlock.builder().addStatement("init()").build(), builder);
builder.addMethod(init());
metadata.baseElement().getTypeParameters().stream()
.map(TypeVariableName::get)
.forEachOrdered(builder::addTypeVariable);
Generators.addComponentOverride(metadata, builder);
Generators.copyLintAnnotations(metadata.element(), builder);
Generators.addInjectionMethods(metadata, builder);
if (Processors.isAssignableFrom(metadata.baseElement(), AndroidClassNames.COMPONENT_ACTIVITY)
&& !metadata.overridesAndroidEntryPointClass()) {
builder.addMethod(getDefaultViewModelProviderFactory());
}
JavaFile.builder(generatedClassName.packageName(), builder.build())
.build()
.writeTo(env.getFiler());
}
// private void init() {
// addOnContextAvailableListener(new OnContextAvailableListener() {
// @Override
// public void onContextAvailable(Context context) {
// inject();
// }
// });
// }
private MethodSpec init() {
return MethodSpec.methodBuilder("init")
.addModifiers(Modifier.PRIVATE)
.addStatement(
"addOnContextAvailableListener($L)",
TypeSpec.anonymousClassBuilder("")
.addSuperinterface(AndroidClassNames.ON_CONTEXT_AVAILABLE_LISTENER)
.addMethod(
MethodSpec.methodBuilder("onContextAvailable")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.addParameter(AndroidClassNames.CONTEXT, "context")
.addStatement("inject()")
.build())
.build())
.build();
}
// @Override
// public ViewModelProvider.Factory getDefaultViewModelProviderFactory() {
// return DefaultViewModelFactories.getActivityFactory(this);
// }
private MethodSpec getDefaultViewModelProviderFactory() {
return MethodSpec.methodBuilder("getDefaultViewModelProviderFactory")
.addAnnotation(Override.class)
.addModifiers(Modifier.PUBLIC)
.returns(AndroidClassNames.VIEW_MODEL_PROVIDER_FACTORY)
.addStatement(
"return $T.getActivityFactory(this)",
AndroidClassNames.DEFAULT_VIEW_MODEL_FACTORIES)
.build();
}
}