Skip to content

Commit 6236625

Browse files
Chang-Ericpull[bot]
authored andcommitted
Add validation so an error where a set has multiple contributions to the same binding is a compilation error instead of a runtime error.
RELNOTES=Add set validation to avoid runtime error. In rare cases, could be a breaking change. PiperOrigin-RevId: 373196814
1 parent e4ff3dc commit 6236625

File tree

3 files changed

+394
-2
lines changed

3 files changed

+394
-2
lines changed

java/dagger/internal/codegen/bindinggraphvalidation/BindingGraphValidationModule.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ static ImmutableSet<BindingGraphPlugin> providePlugins(
4242
MissingBindingValidator validation7,
4343
NullableBindingValidator validation8,
4444
ProvisionDependencyOnProducerBindingValidator validation9,
45-
SubcomponentFactoryMethodValidator validation10) {
45+
SetMultibindingValidator validation10,
46+
SubcomponentFactoryMethodValidator validation11) {
4647
ImmutableSet<BindingGraphPlugin> plugins = ImmutableSet.of(
4748
validation1,
4849
validation2,
@@ -53,7 +54,8 @@ static ImmutableSet<BindingGraphPlugin> providePlugins(
5354
validation7,
5455
validation8,
5556
validation9,
56-
validation10);
57+
validation10,
58+
validation11);
5759
if (compilerOptions.experimentalDaggerErrorMessages()) {
5860
return ImmutableSet.of(factory.create(plugins, "Dagger/Validation"));
5961
} else {
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (C) 2021 The Dagger Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dagger.internal.codegen.bindinggraphvalidation;
18+
19+
import static dagger.model.BindingKind.DELEGATE;
20+
import static dagger.model.BindingKind.MULTIBOUND_SET;
21+
import static javax.tools.Diagnostic.Kind.ERROR;
22+
23+
import com.google.common.base.Joiner;
24+
import com.google.common.collect.HashMultimap;
25+
import com.google.common.collect.ImmutableSet;
26+
import com.google.common.collect.Iterables;
27+
import com.google.common.collect.Multimap;
28+
import dagger.model.Binding;
29+
import dagger.model.BindingGraph;
30+
import dagger.model.Key;
31+
import dagger.spi.BindingGraphPlugin;
32+
import dagger.spi.DiagnosticReporter;
33+
import javax.inject.Inject;
34+
35+
/** Validates that there are not multiple set binding contributions to the same binding. */
36+
final class SetMultibindingValidator implements BindingGraphPlugin {
37+
38+
@Inject
39+
SetMultibindingValidator() {
40+
}
41+
42+
@Override
43+
public String pluginName() {
44+
return "Dagger/SetMultibinding";
45+
}
46+
47+
@Override
48+
public void visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter) {
49+
bindingGraph.bindings().stream()
50+
.filter(binding -> binding.kind().equals(MULTIBOUND_SET))
51+
.forEach(
52+
binding ->
53+
checkForDuplicateSetContributions(binding, bindingGraph, diagnosticReporter));
54+
}
55+
56+
private void checkForDuplicateSetContributions(
57+
Binding binding, BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter) {
58+
// Map of delegate target key to the original contribution binding
59+
Multimap<Key, Binding> dereferencedBindsTargets = HashMultimap.create();
60+
for (Binding dep : bindingGraph.requestedBindings(binding)) {
61+
if (dep.kind().equals(DELEGATE)) {
62+
dereferencedBindsTargets.put(dereferenceDelegateBinding(dep, bindingGraph), dep);
63+
}
64+
}
65+
66+
dereferencedBindsTargets
67+
.asMap()
68+
.forEach(
69+
(targetKey, contributions) -> {
70+
if (contributions.size() > 1) {
71+
diagnosticReporter.reportComponent(
72+
ERROR,
73+
bindingGraph.componentNode(binding.componentPath()).get(),
74+
"Multiple set contributions into %s for the same contribution key: %s.\n\n"
75+
+ " %s\n",
76+
binding.key(),
77+
targetKey,
78+
Joiner.on("\n ").join(contributions));
79+
}
80+
});
81+
}
82+
83+
/** Returns the delegate target of a delegate binding (going through other delegates as well). */
84+
private Key dereferenceDelegateBinding(Binding binding, BindingGraph bindingGraph) {
85+
ImmutableSet<Binding> delegateSet = bindingGraph.requestedBindings(binding);
86+
if (delegateSet.isEmpty()) {
87+
// There may not be a delegate if the delegate is missing. In this case, we just take the
88+
// requested key and return that.
89+
return Iterables.getOnlyElement(binding.dependencies()).key();
90+
}
91+
// If there is a binding, first we check if that is a delegate binding so we can dereference
92+
// that binding if needed.
93+
Binding delegate = Iterables.getOnlyElement(delegateSet);
94+
if (delegate.kind().equals(DELEGATE)) {
95+
return dereferenceDelegateBinding(delegate, bindingGraph);
96+
}
97+
return delegate.key();
98+
}
99+
}

0 commit comments

Comments
 (0)