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

Refactor IncompatilblyScopedBindingsValidator. #4470

Merged
merged 1 commit into from
Oct 3, 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
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
Loading