Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the originating element in LazyMapKeyProxyGenerator. #4529

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading