Skip to content

Commit

Permalink
Internal changes
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 669014463
  • Loading branch information
wanyingd1996 authored and Dagger Team committed Aug 29, 2024
1 parent 88493b6 commit cca779a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
29 changes: 28 additions & 1 deletion java/dagger/internal/codegen/binding/BindingGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -74,7 +77,8 @@ public abstract class BindingGraph {
public abstract static class TopLevelBindingGraph
extends dagger.internal.codegen.model.BindingGraph {
static TopLevelBindingGraph create(
ImmutableNetwork<Node, Edge> network, boolean isFullBindingGraph) {
ImmutableNetwork<Node, Edge> network,
boolean isFullBindingGraph) {
TopLevelBindingGraph topLevelBindingGraph =
new AutoValue_BindingGraph_TopLevelBindingGraph(network, isFullBindingGraph);

Expand Down Expand Up @@ -278,6 +282,29 @@ public final ComponentDescriptor componentDescriptor() {
return ((ComponentNodeImpl) componentNode()).componentDescriptor();
}

/** Returns all entry point methods for this component. */
@Memoized
public ImmutableSet<ComponentMethodDescriptor> entryPointMethods() {
return componentDescriptor().entryPointMethods().stream()
.collect(toImmutableSet());
}

public Optional<ComponentMethodDescriptor> findFirstMatchingComponentMethod(
BindingRequest request) {
return Optional.ofNullable(firstMatchingComponentMethods().get(request));
}

@Memoized
ImmutableMap<BindingRequest, ComponentMethodDescriptor> firstMatchingComponentMethods() {
Map<BindingRequest, ComponentMethodDescriptor> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
14 changes: 0 additions & 14 deletions java/dagger/internal/codegen/binding/ComponentDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ComponentMethodDescriptor> firstMatchingComponentMethod(BindingRequest request) {
return Optional.ofNullable(firstMatchingComponentMethods().get(request));
}

@Memoized
ImmutableMap<BindingRequest, ComponentMethodDescriptor> firstMatchingComponentMethods() {
Map<BindingRequest, ComponentMethodDescriptor> 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<ComponentMethodDescriptor> entryPointMethods() {
return componentMethods().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -443,7 +443,7 @@ public TypeSpec generate() {
/**
* The implementation of a shard.
*
* <p>The purpose of a shard is to allow a component implemenation to be split into multiple
* <p>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.
Expand Down Expand Up @@ -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<MethodSignature> signatures = Sets.newHashSet();
for (ComponentMethodDescriptor method : graph.componentDescriptor().entryPointMethods()) {
if (signatures.add(
MethodSignature.forComponentMethod(method, componentType, processingEnv))) {
Set<MethodSignature> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ RequestRepresentation wrapInMethod(RequestRepresentation bindingExpression) {

BindingRequest request = bindingRequest(binding.key(), RequestKind.INSTANCE);
Optional<ComponentMethodDescriptor> matchingComponentMethod =
graph.componentDescriptor().firstMatchingComponentMethod(request);
graph.findFirstMatchingComponentMethod(request);

ShardImplementation shardImplementation = componentImplementation.shardImplementation(binding);

Expand Down

0 comments on commit cca779a

Please sign in to comment.