Skip to content

Commit

Permalink
Refactor IncompatilblyScopedBindingsValidator.
Browse files Browse the repository at this point in the history
This CL refactors the code to have a single filter so that it's easier to see which bindings are being filtered and reported on. I've also switched to using `Collectors.groupingBy` which makes the stream flow better.

RELNOTES=N/A
PiperOrigin-RevId: 673110053
  • Loading branch information
bcorso authored and Dagger Team committed Oct 3, 2024
1 parent 9091f76 commit 12a40a6
Showing 1 changed file with 27 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
import static dagger.internal.codegen.model.BindingKind.INJECTION;
import static dagger.internal.codegen.xprocessing.XElements.asExecutable;
import static dagger.internal.codegen.xprocessing.XElements.closestEnclosingTypeElement;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.joining;
import static javax.tools.Diagnostic.Kind.ERROR;

import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Multimaps;
import dagger.internal.codegen.base.Scopes;
import dagger.internal.codegen.binding.MethodSignatureFormatter;
import dagger.internal.codegen.compileroption.CompilerOptions;
Expand All @@ -35,8 +34,8 @@
import dagger.internal.codegen.model.DiagnosticReporter;
import dagger.internal.codegen.validation.DiagnosticMessageGenerator;
import dagger.internal.codegen.validation.ValidationBindingGraphPlugin;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.inject.Inject;
import javax.tools.Diagnostic;

Expand All @@ -45,7 +44,6 @@
* component.
*/
final class IncompatiblyScopedBindingsValidator extends ValidationBindingGraphPlugin {

private final MethodSignatureFormatter methodSignatureFormatter;
private final CompilerOptions compilerOptions;
private final DiagnosticMessageGenerator.Factory diagnosticMessageGeneratorFactory;
Expand All @@ -69,36 +67,36 @@ public String pluginName() {
public void visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter) {
DiagnosticMessageGenerator diagnosticMessageGenerator =
diagnosticMessageGeneratorFactory.create(bindingGraph);
ImmutableSetMultimap.Builder<ComponentNode, dagger.internal.codegen.model.Binding>
incompatibleBindings = ImmutableSetMultimap.builder();
for (dagger.internal.codegen.model.Binding binding : bindingGraph.bindings()) {
binding
.scope()
.filter(scope -> !scope.isReusable())
.ifPresent(
scope -> {
ComponentNode componentNode =
bindingGraph.componentNode(binding.componentPath()).get();
if (!componentNode.scopes().contains(scope)) {
// @Inject bindings in module or subcomponent binding graphs will appear at the
// properly scoped ancestor component, so ignore them here.
if (binding.kind().equals(INJECTION)
&& (bindingGraph.rootComponentNode().isSubcomponent()
|| !bindingGraph.rootComponentNode().isRealComponent())) {
return;
}
incompatibleBindings.put(componentNode, binding);
}
});
bindingGraph.bindings().stream()
.filter(binding -> hasIncompatibleScope(bindingGraph, binding))
.collect(groupingBy(binding -> owningComponent(bindingGraph, binding)))
.forEach((owningComponent, bindings) ->
report(owningComponent, bindings, diagnosticReporter, diagnosticMessageGenerator));
}

private static boolean hasIncompatibleScope(BindingGraph bindingGraph, Binding binding) {
if (binding.scope().isEmpty()
|| binding.scope().get().isReusable()
// @Inject bindings in module or subcomponent binding graphs will appear at the
// properly scoped ancestor component, so ignore them here.
|| (binding.kind() == INJECTION && isSubcomponentOrModuleRoot(bindingGraph))) {
return false;
}
Multimaps.asMap(incompatibleBindings.build())
.forEach((componentNode, bindings) ->
report(componentNode, bindings, diagnosticReporter, diagnosticMessageGenerator));
return !owningComponent(bindingGraph, binding).scopes().contains(binding.scope().get());
}

private static boolean isSubcomponentOrModuleRoot(BindingGraph bindingGraph) {
ComponentNode rootComponent = bindingGraph.rootComponentNode();
return rootComponent.isSubcomponent() || !rootComponent.isRealComponent();
}

private static ComponentNode owningComponent(BindingGraph bindingGraph, Binding binding) {
return bindingGraph.componentNode(binding.componentPath()).get();
}

private void report(
ComponentNode componentNode,
Set<Binding> bindings,
List<Binding> bindings,
DiagnosticReporter diagnosticReporter,
DiagnosticMessageGenerator diagnosticMessageGenerator) {
Diagnostic.Kind diagnosticKind = ERROR;
Expand Down

0 comments on commit 12a40a6

Please sign in to comment.