Skip to content

Commit

Permalink
Add the originating element in LazyMapKeyProxyGenerator.
Browse files Browse the repository at this point in the history
Forgetting the originating element was an oversight. Gradle's incremental processing requires originating elements to determine when to reprocess files. This CL fixes `LazyMapKeyProxyGenerator` by extending `SourceFileGenerator`, which takes care of assigning the originating element. In addition, it removes some duplicate code that's already handled in `SourceFileGenerator` (e.g. adding the generated and suppress annotations).

Fixes #4526

RELNOTES=Fixes #4526: Add the originating element in LazyMapKeyProxyGenerator.
PiperOrigin-RevId: 702804185
  • Loading branch information
bcorso authored and Dagger Team committed Dec 4, 2024
1 parent 384ad08 commit 9b49353
Showing 1 changed file with 20 additions and 44 deletions.
64 changes: 20 additions & 44 deletions java/dagger/internal/codegen/writing/LazyMapKeyProxyGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,73 +20,49 @@
import static dagger.internal.codegen.binding.MapKeys.KEEP_FIELD_TYPE_FIELD;
import static dagger.internal.codegen.binding.MapKeys.LAZY_CLASS_KEY_NAME_FIELD;
import static dagger.internal.codegen.binding.MapKeys.lazyClassKeyProxyClassName;
import static dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression.CAST;
import static dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression.DEPRECATION;
import static dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression.KOTLIN_INTERNAL;
import static dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression.RAWTYPES;
import static dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression.UNCHECKED;
import static javax.lang.model.element.Modifier.FINAL;
import static javax.lang.model.element.Modifier.PUBLIC;
import static javax.lang.model.element.Modifier.STATIC;

import androidx.room.compiler.processing.XElement;
import androidx.room.compiler.processing.XFiler;
import androidx.room.compiler.processing.XMethodElement;
import androidx.room.compiler.processing.XProcessingEnv;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.TypeSpec;
import dagger.internal.DaggerGenerated;
import dagger.internal.codegen.javapoet.AnnotationSpecs;
import dagger.internal.codegen.javapoet.AnnotationSpecs.Suppression;
import dagger.internal.codegen.base.SourceFileGenerator;
import dagger.internal.codegen.javapoet.TypeNames;
import java.util.Optional;
import javax.inject.Inject;

/**
* Generate a class containing fields that works with proguard rules to support @LazyClassKey
* usages.
*/
public final class LazyMapKeyProxyGenerator {
private static final String GENERATED_COMMENTS = "https://dagger.dev";
private final XProcessingEnv processingEnv;
private final XFiler filer;
public final class LazyMapKeyProxyGenerator extends SourceFileGenerator<XMethodElement> {

@Inject
LazyMapKeyProxyGenerator(XProcessingEnv processingEnv, XFiler filer) {
this.processingEnv = processingEnv;
this.filer = filer;
LazyMapKeyProxyGenerator(XFiler filer, XProcessingEnv processingEnv) {
super(filer, processingEnv);
}

public void generate(XMethodElement element) {
ClassName lazyClassKeyProxyClassName = lazyClassKeyProxyClassName(element);
TypeSpec.Builder typeSpecBuilder =
classBuilder(lazyClassKeyProxyClassName)
.addModifiers(PUBLIC, FINAL)
.addAnnotation(TypeNames.IDENTIFIER_NAME_STRING)
.addAnnotation(DaggerGenerated.class)
.addFields(lazyClassKeyFields(element));
Optional<AnnotationSpec> generatedAnnotation =
Optional.ofNullable(processingEnv.findGeneratedAnnotation())
.map(
annotation ->
AnnotationSpec.builder(annotation.getClassName())
.addMember("value", "$S", "dagger.internal.codegen.LazyClassKeyProcessor")
.addMember("comments", "$S", GENERATED_COMMENTS)
.build());
generatedAnnotation.ifPresent(typeSpecBuilder::addAnnotation);
typeSpecBuilder.addAnnotation(
AnnotationSpecs.suppressWarnings(
ImmutableSet.<Suppression>builder()
.add(UNCHECKED, RAWTYPES, KOTLIN_INTERNAL, CAST, DEPRECATION)
.build()));
@Override
public XElement originatingElement(XMethodElement input) {
return input;
}

@Override
public ImmutableList<TypeSpec.Builder> topLevelTypes(XMethodElement input) {
return ImmutableList.of(lazyClassKeyProxyTypeSpec(input).toBuilder());
}

filer.write(
JavaFile.builder(lazyClassKeyProxyClassName.packageName(), typeSpecBuilder.build()).build(),
XFiler.Mode.Isolating);
private TypeSpec lazyClassKeyProxyTypeSpec(XMethodElement element) {
return classBuilder(lazyClassKeyProxyClassName(element))
.addModifiers(PUBLIC, FINAL)
.addAnnotation(TypeNames.IDENTIFIER_NAME_STRING)
.addFields(lazyClassKeyFields(element))
.build();
}

private static ImmutableList<FieldSpec> lazyClassKeyFields(XMethodElement element) {
Expand Down

0 comments on commit 9b49353

Please sign in to comment.