|
20 | 20 | import static dagger.internal.codegen.binding.MapKeys.KEEP_FIELD_TYPE_FIELD; |
21 | 21 | import static dagger.internal.codegen.binding.MapKeys.LAZY_CLASS_KEY_NAME_FIELD; |
22 | 22 | import static dagger.internal.codegen.binding.MapKeys.lazyClassKeyProxyClassName; |
23 | | -import static dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression.CAST; |
24 | | -import static dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression.DEPRECATION; |
25 | | -import static dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression.KOTLIN_INTERNAL; |
26 | | -import static dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression.RAWTYPES; |
27 | | -import static dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression.UNCHECKED; |
28 | 23 | import static javax.lang.model.element.Modifier.FINAL; |
29 | 24 | import static javax.lang.model.element.Modifier.PUBLIC; |
30 | 25 | import static javax.lang.model.element.Modifier.STATIC; |
31 | 26 |
|
| 27 | +import androidx.room.compiler.processing.XElement; |
32 | 28 | import androidx.room.compiler.processing.XFiler; |
33 | 29 | import androidx.room.compiler.processing.XMethodElement; |
34 | 30 | import androidx.room.compiler.processing.XProcessingEnv; |
35 | 31 | import com.google.common.collect.ImmutableList; |
36 | | -import com.google.common.collect.ImmutableSet; |
37 | | -import com.squareup.javapoet.AnnotationSpec; |
38 | 32 | import com.squareup.javapoet.ClassName; |
39 | 33 | import com.squareup.javapoet.FieldSpec; |
40 | | -import com.squareup.javapoet.JavaFile; |
41 | 34 | import com.squareup.javapoet.TypeSpec; |
42 | | -import dagger.internal.DaggerGenerated; |
43 | | -import dagger.internal.codegen.javapoet.AnnotationSpecs; |
44 | | -import dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression; |
| 35 | +import dagger.internal.codegen.base.SourceFileGenerator; |
45 | 36 | import dagger.internal.codegen.javapoet.TypeNames; |
46 | | -import java.util.Optional; |
47 | 37 | import javax.inject.Inject; |
48 | 38 |
|
49 | 39 | /** |
50 | 40 | * Generate a class containing fields that works with proguard rules to support @LazyClassKey |
51 | 41 | * usages. |
52 | 42 | */ |
53 | | -public final class LazyMapKeyProxyGenerator { |
54 | | - private static final String GENERATED_COMMENTS = "https://dagger.dev"; |
55 | | - private final XProcessingEnv processingEnv; |
56 | | - private final XFiler filer; |
| 43 | +public final class LazyMapKeyProxyGenerator extends SourceFileGenerator<XMethodElement> { |
57 | 44 |
|
58 | 45 | @Inject |
59 | | - LazyMapKeyProxyGenerator(XProcessingEnv processingEnv, XFiler filer) { |
60 | | - this.processingEnv = processingEnv; |
61 | | - this.filer = filer; |
| 46 | + LazyMapKeyProxyGenerator(XFiler filer, XProcessingEnv processingEnv) { |
| 47 | + super(filer, processingEnv); |
62 | 48 | } |
63 | 49 |
|
64 | | - public void generate(XMethodElement element) { |
65 | | - ClassName lazyClassKeyProxyClassName = lazyClassKeyProxyClassName(element); |
66 | | - TypeSpec.Builder typeSpecBuilder = |
67 | | - classBuilder(lazyClassKeyProxyClassName) |
68 | | - .addModifiers(PUBLIC, FINAL) |
69 | | - .addAnnotation(TypeNames.IDENTIFIER_NAME_STRING) |
70 | | - .addAnnotation(DaggerGenerated.class) |
71 | | - .addFields(lazyClassKeyFields(element)); |
72 | | - Optional<AnnotationSpec> generatedAnnotation = |
73 | | - Optional.ofNullable(processingEnv.findGeneratedAnnotation()) |
74 | | - .map( |
75 | | - annotation -> |
76 | | - AnnotationSpec.builder(annotation.getClassName()) |
77 | | - .addMember("value", "$S", "dagger.internal.codegen.LazyClassKeyProcessor") |
78 | | - .addMember("comments", "$S", GENERATED_COMMENTS) |
79 | | - .build()); |
80 | | - generatedAnnotation.ifPresent(typeSpecBuilder::addAnnotation); |
81 | | - typeSpecBuilder.addAnnotation( |
82 | | - AnnotationSpecs.suppressWarnings( |
83 | | - ImmutableSet.<Suppression>builder() |
84 | | - .add(UNCHECKED, RAWTYPES, KOTLIN_INTERNAL, CAST, DEPRECATION) |
85 | | - .build())); |
| 50 | + @Override |
| 51 | + public XElement originatingElement(XMethodElement input) { |
| 52 | + return input; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public ImmutableList<TypeSpec.Builder> topLevelTypes(XMethodElement input) { |
| 57 | + return ImmutableList.of(lazyClassKeyProxyTypeSpec(input).toBuilder()); |
| 58 | + } |
86 | 59 |
|
87 | | - filer.write( |
88 | | - JavaFile.builder(lazyClassKeyProxyClassName.packageName(), typeSpecBuilder.build()).build(), |
89 | | - XFiler.Mode.Isolating); |
| 60 | + private TypeSpec lazyClassKeyProxyTypeSpec(XMethodElement element) { |
| 61 | + return classBuilder(lazyClassKeyProxyClassName(element)) |
| 62 | + .addModifiers(PUBLIC, FINAL) |
| 63 | + .addAnnotation(TypeNames.IDENTIFIER_NAME_STRING) |
| 64 | + .addFields(lazyClassKeyFields(element)) |
| 65 | + .build(); |
90 | 66 | } |
91 | 67 |
|
92 | 68 | private static ImmutableList<FieldSpec> lazyClassKeyFields(XMethodElement element) { |
|
0 commit comments