Skip to content

Commit

Permalink
Generate a class for each @LazyClassKey annotated binding, and move f…
Browse files Browse the repository at this point in the history
…ields from `LazyClassKeyProvider` into the generated class.

RELNOTES=n/a
PiperOrigin-RevId: 692243692
  • Loading branch information
wanyingd1996 authored and Dagger Team committed Nov 4, 2024
1 parent 0f936b5 commit e2d8b31
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 213 deletions.
6 changes: 6 additions & 0 deletions java/dagger/internal/codegen/binding/MapKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ public static boolean useLazyClassKey(Binding binding, BindingGraph graph) {
return false;
}

public static CodeBlock getLazyClassMapKeyExpression(ContributionBinding contributionBinding) {
ClassName proxyClassName =
lazyClassKeyProxyClassName(XElements.asMethod(contributionBinding.bindingElement().get()));
return CodeBlock.of("$T.$N", proxyClassName, LAZY_CLASS_KEY_NAME_FIELD);
}

public static ClassName lazyClassKeyProxyClassName(XMethodElement methodElement) {
return elementBasedClassName(methodElement, "_LazyMapKey");
}
Expand Down
158 changes: 0 additions & 158 deletions java/dagger/internal/codegen/writing/LazyClassKeyProviders.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package dagger.internal.codegen.writing;

import static com.google.common.base.Preconditions.checkNotNull;
import static dagger.internal.codegen.binding.MapKeys.getLazyClassMapKeyExpression;
import static dagger.internal.codegen.binding.MapKeys.getMapKeyExpression;
import static dagger.internal.codegen.binding.SourceFiles.mapFactoryClassName;

Expand All @@ -43,12 +44,10 @@ final class MapFactoryCreationExpression extends MultibindingFactoryCreationExpr
private final BindingGraph graph;
private final MultiboundMapBinding binding;
private final boolean useLazyClassKey;
private final LazyClassKeyProviders lazyClassKeyProviders;

@AssistedInject
MapFactoryCreationExpression(
@Assisted MultiboundMapBinding binding,
LazyClassKeyProviders lazyClassKeyProviders,
XProcessingEnv processingEnv,
ComponentImplementation componentImplementation,
ComponentRequestRepresentations componentRequestRepresentations,
Expand All @@ -59,7 +58,6 @@ final class MapFactoryCreationExpression extends MultibindingFactoryCreationExpr
this.componentImplementation = componentImplementation;
this.graph = graph;
this.useLazyClassKey = MapKeys.useLazyClassKey(binding, graph);
this.lazyClassKeyProviders = lazyClassKeyProviders;
}

@Override
Expand All @@ -83,7 +81,7 @@ public CodeBlock creationExpression() {
builder.add(
".put($L, $L)",
useLazyClassKey
? lazyClassKeyProviders.getMapKeyExpression(dependency.key())
? getLazyClassMapKeyExpression(graph.contributionBinding(dependency.key()))
: getMapKeyExpression(
contributionBinding, componentImplementation.name(), processingEnv),
multibindingDependencyExpression(dependency));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Iterables.getOnlyElement;
import static dagger.internal.codegen.binding.BindingRequest.bindingRequest;
import static dagger.internal.codegen.binding.MapKeys.getLazyClassMapKeyExpression;
import static dagger.internal.codegen.binding.MapKeys.getMapKeyExpression;
import static dagger.internal.codegen.javapoet.CodeBlocks.toParametersCodeBlock;
import static dagger.internal.codegen.langmodel.Accessibility.isTypeAccessibleFrom;
Expand Down Expand Up @@ -56,12 +57,10 @@ final class MapRequestRepresentation extends RequestRepresentation {
private final ImmutableMap<DependencyRequest, ContributionBinding> dependencies;
private final ComponentRequestRepresentations componentRequestRepresentations;
private final boolean useLazyClassKey;
private final LazyClassKeyProviders lazyClassKeyProviders;

@AssistedInject
MapRequestRepresentation(
@Assisted MultiboundMapBinding binding,
LazyClassKeyProviders lazyClassKeyProviders,
XProcessingEnv processingEnv,
BindingGraph graph,
ComponentImplementation componentImplementation,
Expand All @@ -74,7 +73,6 @@ final class MapRequestRepresentation extends RequestRepresentation {
this.dependencies =
Maps.toMap(binding.dependencies(), dep -> graph.contributionBinding(dep.key()));
this.useLazyClassKey = MapKeys.useLazyClassKey(binding, graph);
this.lazyClassKeyProviders = lazyClassKeyProviders;
}

@Override
Expand Down Expand Up @@ -153,7 +151,7 @@ private CodeBlock keyAndValueExpression(DependencyRequest dependency, ClassName
return CodeBlock.of(
"$L, $L",
useLazyClassKey
? lazyClassKeyProviders.getMapKeyExpression(dependency.key())
? getLazyClassMapKeyExpression(dependencies.get(dependency))
: getMapKeyExpression(dependencies.get(dependency), requestingClass, processingEnv),
componentRequestRepresentations
.getDependencyExpression(bindingRequest(dependency), requestingClass)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package test;

import com.google.common.collect.ImmutableMap;
import dagger.internal.DaggerGenerated;
import dagger.internal.IdentifierNameString;
import dagger.internal.KeepFieldType;
import dagger.internal.LazyClassKeyMap;
import java.util.Map;
import javax.annotation.processing.Generated;
Expand Down Expand Up @@ -52,21 +50,8 @@ final class DaggerTestComponent {

@Override
public Map<Class<?>, Integer> classKey() {
return LazyClassKeyMap.<Integer>of(ImmutableMap.<String, Integer>of(LazyClassKeyProvider.test_Foo_Bar, MapKeyBindingsModule.classKey(), LazyClassKeyProvider.test_Foo_Bar3, MapKeyBindingsModule.classKey2()));
return LazyClassKeyMap.<Integer>of(ImmutableMap.<String, Integer>of(MapKeyBindingsModule_ClassKey_LazyMapKey.lazyClassKeyName, MapKeyBindingsModule.classKey(), MapKeyBindingsModule_ClassKey2_LazyMapKey.lazyClassKeyName, MapKeyBindingsModule.classKey2()));
}
}

@IdentifierNameString
private static final class LazyClassKeyProvider {
static String test_Foo_Bar = "test.Foo_Bar";

static String test_Foo_Bar3 = "test.Foo$Bar";

@KeepFieldType
Foo_Bar test_Foo_Bar2;

@KeepFieldType
Foo.Bar test_Foo_Bar4;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package test;

import com.google.common.collect.ImmutableMap;
import dagger.internal.DaggerGenerated;
import dagger.internal.IdentifierNameString;
import dagger.internal.KeepFieldType;
import dagger.internal.LazyClassKeyMap;
import java.util.Map;
import javax.annotation.processing.Generated;
Expand Down Expand Up @@ -52,21 +50,8 @@ final class DaggerTestComponent {

@Override
public Map<Class<?>, Integer> classKey() {
return LazyClassKeyMap.<Integer>of(ImmutableMap.<String, Integer>of(LazyClassKeyProvider.test_Foo_Bar, MapKeyBindingsModule.classKey(), LazyClassKeyProvider.test_Foo_Bar3, MapKeyBindingsModule.classKey2()));
return LazyClassKeyMap.<Integer>of(ImmutableMap.<String, Integer>of(MapKeyBindingsModule_ClassKey_LazyMapKey.lazyClassKeyName, MapKeyBindingsModule.classKey(), MapKeyBindingsModule_ClassKey2_LazyMapKey.lazyClassKeyName, MapKeyBindingsModule.classKey2()));
}
}

@IdentifierNameString
private static final class LazyClassKeyProvider {
static String test_Foo_Bar = "test.Foo_Bar";

static String test_Foo_Bar3 = "test.Foo$Bar";

@KeepFieldType
Foo_Bar test_Foo_Bar2;

@KeepFieldType
Foo.Bar test_Foo_Bar4;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package test;

import com.google.common.collect.ImmutableMap;
import dagger.internal.DaggerGenerated;
import dagger.internal.IdentifierNameString;
import dagger.internal.LazyClassKeyMap;
import dagger.internal.MapFactory;
import dagger.internal.Provider;
Expand All @@ -11,6 +10,7 @@ import javax.annotation.processing.Generated;
import mapkeys.MapKeys;
import mapkeys.MapModule;
import mapkeys.MapModule_ClassKeyFactory;
import mapkeys.MapModule_ClassKey_LazyMapKey;
import mapkeys.MapModule_ComplexKeyWithInaccessibleAnnotationValueFactory;
import mapkeys.MapModule_ComplexKeyWithInaccessibleAnnotationValueMapKey;
import mapkeys.MapModule_ComplexKeyWithInaccessibleArrayValueFactory;
Expand Down Expand Up @@ -67,13 +67,13 @@ final class DaggerTestComponent {

@SuppressWarnings("unchecked")
private void initialize() {
this.mapOfClassOfAndIntegerProvider = LazyClassKeyMap.MapFactory.<Integer>of(MapFactory.<String, Integer>builder(1).put(LazyClassKeyProvider.mapkeys_MapKeys_Inaccessible, MapModule_ClassKeyFactory.create()).build());
this.mapOfClassOfAndIntegerProvider = LazyClassKeyMap.MapFactory.<Integer>of(MapFactory.<String, Integer>builder(1).put(MapModule_ClassKey_LazyMapKey.lazyClassKeyName, MapModule_ClassKeyFactory.create()).build());
this.mapOfComplexKeyAndIntegerProvider = MapFactory.<MapKeys.ComplexKey, Integer>builder(3).put(MapModule_ComplexKeyWithInaccessibleValueMapKey.create(), MapModule_ComplexKeyWithInaccessibleValueFactory.create()).put(MapModule_ComplexKeyWithInaccessibleArrayValueMapKey.create(), MapModule_ComplexKeyWithInaccessibleArrayValueFactory.create()).put(MapModule_ComplexKeyWithInaccessibleAnnotationValueMapKey.create(), MapModule_ComplexKeyWithInaccessibleAnnotationValueFactory.create()).build();
}

@Override
public Map<Class<?>, Integer> classKey() {
return LazyClassKeyMap.<Integer>of(ImmutableMap.<String, Integer>of(LazyClassKeyProvider.mapkeys_MapKeys_Inaccessible, MapModule.classKey()));
return LazyClassKeyMap.<Integer>of(ImmutableMap.<String, Integer>of(MapModule_ClassKey_LazyMapKey.lazyClassKeyName, MapModule.classKey()));
}

@Override
Expand All @@ -91,10 +91,5 @@ final class DaggerTestComponent {
return mapOfComplexKeyAndIntegerProvider;
}
}

@IdentifierNameString
private static final class LazyClassKeyProvider {
static String mapkeys_MapKeys_Inaccessible = "mapkeys.MapKeys$Inaccessible";
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package test;

import com.google.common.collect.ImmutableMap;
import dagger.internal.DaggerGenerated;
import dagger.internal.IdentifierNameString;
import dagger.internal.LazyClassKeyMap;
import dagger.internal.Provider;
import java.util.Map;
import javax.annotation.processing.Generated;
import mapkeys.MapKeys;
import mapkeys.MapModule;
import mapkeys.MapModule_ClassKey_LazyMapKey;
import mapkeys.MapModule_ComplexKeyWithInaccessibleAnnotationValueMapKey;
import mapkeys.MapModule_ComplexKeyWithInaccessibleArrayValueMapKey;
import mapkeys.MapModule_ComplexKeyWithInaccessibleValueMapKey;
Expand Down Expand Up @@ -101,7 +101,7 @@ final class DaggerTestComponent {
public T get() {
switch (id) {
case 0: // java.util.Map<java.lang.Class<?>,java.lang.Integer>
return (T) LazyClassKeyMap.<Integer>of(ImmutableMap.<String, Integer>of(LazyClassKeyProvider.mapkeys_MapKeys_Inaccessible, MapModule.classKey()));
return (T) LazyClassKeyMap.<Integer>of(ImmutableMap.<String, Integer>of(MapModule_ClassKey_LazyMapKey.lazyClassKeyName, MapModule.classKey()));

case 1: // java.util.Map<mapkeys.MapKeys.ComplexKey,java.lang.Integer>
return (T) ImmutableMap.<MapKeys.ComplexKey, Integer>of(MapModule_ComplexKeyWithInaccessibleValueMapKey.create(), MapModule.complexKeyWithInaccessibleValue(), MapModule_ComplexKeyWithInaccessibleArrayValueMapKey.create(), MapModule.complexKeyWithInaccessibleArrayValue(), MapModule_ComplexKeyWithInaccessibleAnnotationValueMapKey.create(), MapModule.complexKeyWithInaccessibleAnnotationValue());
Expand All @@ -111,10 +111,5 @@ final class DaggerTestComponent {
}
}
}

@IdentifierNameString
private static final class LazyClassKeyProvider {
static String mapkeys_MapKeys_Inaccessible = "mapkeys.MapKeys$Inaccessible";
}
}

0 comments on commit e2d8b31

Please sign in to comment.