From cca779a9d99ec104e6c920b1e6b2988585562ca4 Mon Sep 17 00:00:00 2001 From: Wanying Ding Date: Thu, 29 Aug 2024 11:57:20 -0700 Subject: [PATCH] Internal changes RELNOTES=n/a PiperOrigin-RevId: 669014463 --- .../codegen/binding/BindingGraph.java | 29 ++++++++++++++++++- .../binding/BindingGraphConverter.java | 5 ++-- .../codegen/binding/ComponentDescriptor.java | 14 --------- .../writing/ComponentImplementation.java | 13 +++++---- .../DirectInstanceBindingRepresentation.java | 2 +- 5 files changed, 39 insertions(+), 24 deletions(-) diff --git a/java/dagger/internal/codegen/binding/BindingGraph.java b/java/dagger/internal/codegen/binding/BindingGraph.java index 0090b3d2ac2..7f090532a3d 100644 --- a/java/dagger/internal/codegen/binding/BindingGraph.java +++ b/java/dagger/internal/codegen/binding/BindingGraph.java @@ -17,6 +17,7 @@ package dagger.internal.codegen.binding; import static com.google.common.collect.Iterables.transform; +import static dagger.internal.codegen.binding.BindingRequest.bindingRequest; import static dagger.internal.codegen.extension.DaggerCollectors.toOptional; import static dagger.internal.codegen.extension.DaggerStreams.instancesOf; import static dagger.internal.codegen.extension.DaggerStreams.presentValues; @@ -42,6 +43,7 @@ import com.google.common.graph.ImmutableNetwork; import com.google.common.graph.Traverser; import dagger.internal.codegen.base.TarjanSCCs; +import dagger.internal.codegen.binding.ComponentDescriptor.ComponentMethodDescriptor; import dagger.internal.codegen.model.BindingGraph.ChildFactoryMethodEdge; import dagger.internal.codegen.model.BindingGraph.ComponentNode; import dagger.internal.codegen.model.BindingGraph.DependencyEdge; @@ -52,6 +54,7 @@ import dagger.internal.codegen.model.DependencyRequest; import dagger.internal.codegen.model.Key; import java.util.Comparator; +import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; @@ -74,7 +77,8 @@ public abstract class BindingGraph { public abstract static class TopLevelBindingGraph extends dagger.internal.codegen.model.BindingGraph { static TopLevelBindingGraph create( - ImmutableNetwork network, boolean isFullBindingGraph) { + ImmutableNetwork network, + boolean isFullBindingGraph) { TopLevelBindingGraph topLevelBindingGraph = new AutoValue_BindingGraph_TopLevelBindingGraph(network, isFullBindingGraph); @@ -278,6 +282,29 @@ public final ComponentDescriptor componentDescriptor() { return ((ComponentNodeImpl) componentNode()).componentDescriptor(); } + /** Returns all entry point methods for this component. */ + @Memoized + public ImmutableSet entryPointMethods() { + return componentDescriptor().entryPointMethods().stream() + .collect(toImmutableSet()); + } + + public Optional findFirstMatchingComponentMethod( + BindingRequest request) { + return Optional.ofNullable(firstMatchingComponentMethods().get(request)); + } + + @Memoized + ImmutableMap firstMatchingComponentMethods() { + Map componentMethodDescriptorsByRequest = + new HashMap<>(); + for (ComponentMethodDescriptor method : entryPointMethods()) { + componentMethodDescriptorsByRequest.putIfAbsent( + bindingRequest(method.dependencyRequest().get()), method); + } + return ImmutableMap.copyOf(componentMethodDescriptorsByRequest); + } + /** * Returns the {@link ContributionBinding} for the given {@link Key} in this component or {@link * Optional#empty()} if one doesn't exist. diff --git a/java/dagger/internal/codegen/binding/BindingGraphConverter.java b/java/dagger/internal/codegen/binding/BindingGraphConverter.java index 42b61a61ba7..10dabeb9e69 100644 --- a/java/dagger/internal/codegen/binding/BindingGraphConverter.java +++ b/java/dagger/internal/codegen/binding/BindingGraphConverter.java @@ -69,9 +69,10 @@ BindingGraph convert(LegacyBindingGraph legacyBindingGraph, boolean isFullBindin if (!isFullBindingGraph) { unreachableNodes(network.asGraph(), rootNode).forEach(network::removeNode); } - TopLevelBindingGraph topLevelBindingGraph = - TopLevelBindingGraph.create(ImmutableNetwork.copyOf(network), isFullBindingGraph); + TopLevelBindingGraph.create( + ImmutableNetwork.copyOf(network), + isFullBindingGraph); return BindingGraph.create(rootNode, topLevelBindingGraph); } diff --git a/java/dagger/internal/codegen/binding/ComponentDescriptor.java b/java/dagger/internal/codegen/binding/ComponentDescriptor.java index 535bb37c7ea..fc2b674fd67 100644 --- a/java/dagger/internal/codegen/binding/ComponentDescriptor.java +++ b/java/dagger/internal/codegen/binding/ComponentDescriptor.java @@ -276,20 +276,6 @@ final ComponentDescriptor getChildComponentWithBuilderType(XTypeElement builderT builderType.getQualifiedName()); } - /** Returns the first component method associated with this binding request, if one exists. */ - public Optional firstMatchingComponentMethod(BindingRequest request) { - return Optional.ofNullable(firstMatchingComponentMethods().get(request)); - } - - @Memoized - ImmutableMap firstMatchingComponentMethods() { - Map methods = new HashMap<>(); - for (ComponentMethodDescriptor method : entryPointMethods()) { - methods.putIfAbsent(BindingRequest.bindingRequest(method.dependencyRequest().get()), method); - } - return ImmutableMap.copyOf(methods); - } - /** The entry point methods on the component type. Each has a {@link DependencyRequest}. */ public final ImmutableSet entryPointMethods() { return componentMethods().stream() diff --git a/java/dagger/internal/codegen/writing/ComponentImplementation.java b/java/dagger/internal/codegen/writing/ComponentImplementation.java index 84e6eeccee1..5c2678f6705 100644 --- a/java/dagger/internal/codegen/writing/ComponentImplementation.java +++ b/java/dagger/internal/codegen/writing/ComponentImplementation.java @@ -59,7 +59,6 @@ import com.google.common.collect.ListMultimap; import com.google.common.collect.Lists; import com.google.common.collect.MultimapBuilder; -import com.google.common.collect.Sets; import com.squareup.javapoet.ClassName; import com.squareup.javapoet.CodeBlock; import com.squareup.javapoet.FieldSpec; @@ -93,6 +92,7 @@ import dagger.internal.codegen.xprocessing.XTypeElements; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -443,7 +443,7 @@ public TypeSpec generate() { /** * The implementation of a shard. * - *

The purpose of a shard is to allow a component implemenation to be split into multiple + *

The purpose of a shard is to allow a component implementation to be split into multiple * classes, where each class owns the creation logic for a set of keys. Sharding is useful for * large component implementations, where a single component implementation class can reach size * limitations, such as the constant pool size. @@ -886,10 +886,11 @@ private void addInterfaceMethods() { // Each component method may have been declared by several supertypes. We want to implement // only one method for each distinct signature. XType componentType = graph.componentTypeElement().getType(); - Set signatures = Sets.newHashSet(); - for (ComponentMethodDescriptor method : graph.componentDescriptor().entryPointMethods()) { - if (signatures.add( - MethodSignature.forComponentMethod(method, componentType, processingEnv))) { + Set methodDescriptors = new HashSet<>(); + for (ComponentMethodDescriptor method : graph.entryPointMethods()) { + MethodSignature signature = + MethodSignature.forComponentMethod(method, componentType, processingEnv); + if (methodDescriptors.add(signature)) { addMethod( COMPONENT_METHOD, componentRequestRepresentationsProvider.get().getComponentMethod(method)); diff --git a/java/dagger/internal/codegen/writing/DirectInstanceBindingRepresentation.java b/java/dagger/internal/codegen/writing/DirectInstanceBindingRepresentation.java index 4b518f7e8a8..058ea5fbee7 100644 --- a/java/dagger/internal/codegen/writing/DirectInstanceBindingRepresentation.java +++ b/java/dagger/internal/codegen/writing/DirectInstanceBindingRepresentation.java @@ -105,7 +105,7 @@ RequestRepresentation wrapInMethod(RequestRepresentation bindingExpression) { BindingRequest request = bindingRequest(binding.key(), RequestKind.INSTANCE); Optional matchingComponentMethod = - graph.componentDescriptor().firstMatchingComponentMethod(request); + graph.findFirstMatchingComponentMethod(request); ShardImplementation shardImplementation = componentImplementation.shardImplementation(binding);