Skip to content

Commit

Permalink
Avoid Maps.transformValues with a side-effecting transform.
Browse files Browse the repository at this point in the history
The order in which entries in the input map are subjected to the transform is not specified. If it changes then some golden-file tests will fail.

RELNOTES=n/a
PiperOrigin-RevId: 410941857
  • Loading branch information
eamonnmcmanus authored and Dagger Team committed Nov 19, 2021
1 parent a7318cc commit 75b93a0
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions java/dagger/internal/codegen/writing/FactoryGenerator.java
Expand Up @@ -17,7 +17,6 @@
package dagger.internal.codegen.writing;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Maps.transformValues;
import static com.squareup.javapoet.MethodSpec.constructorBuilder;
import static com.squareup.javapoet.MethodSpec.methodBuilder;
import static com.squareup.javapoet.TypeSpec.classBuilder;
Expand Down Expand Up @@ -171,13 +170,16 @@ private ImmutableMap<DependencyRequest, FieldSpec> frameworkFields(ProvisionBind
UniqueNameSet uniqueFieldNames = new UniqueNameSet();
// TODO(bcorso, dpb): Add a test for the case when a Factory parameter is named "module".
moduleParameter(binding).ifPresent(module -> uniqueFieldNames.claim(module.name));
return ImmutableMap.copyOf(
transformValues(
generateBindingFieldsForDependencies(binding),
field ->
// We avoid Maps.transformValues here because it would implicitly depend on the order in which
// the transform function is evaluated on each entry in the map.
ImmutableMap.Builder<DependencyRequest, FieldSpec> builder = ImmutableMap.builder();
generateBindingFieldsForDependencies(binding).forEach(
(dependency, field) ->
builder.put(dependency,
FieldSpec.builder(
field.type(), uniqueFieldNames.getUniqueName(field.name()), PRIVATE, FINAL)
.build()));
return builder.build();
}

private void addCreateMethod(ProvisionBinding binding, TypeSpec.Builder factoryBuilder) {
Expand Down

0 comments on commit 75b93a0

Please sign in to comment.