From 3d71e845cac1b15a6b8f137ee137cc7d89d491b0 Mon Sep 17 00:00:00 2001 From: Wanying Ding Date: Tue, 9 Nov 2021 01:14:50 -0800 Subject: [PATCH] Make `FrameworkInstanceBindingRepresentation` do not use `DirectInstanceBindingRepresentation`. RELNOTES=n/a PiperOrigin-RevId: 408544948 --- .../codegen/binding/BindingGraph.java | 66 ++++++++++++++++ .../writing/FrameworkFieldInitializer.java | 10 +-- .../ProvisionBindingRepresentation.java | 26 +++++-- .../SwitchingProviderInstanceSupplier.java | 22 +----- .../internal/codegen/AssistedFactoryTest.java | 11 ++- .../codegen/ComponentProcessorTest.java | 76 ++++++++++--------- .../ComponentRequirementFieldTest.java | 21 +++-- .../internal/codegen/ComponentShardTest.java | 7 +- .../internal/codegen/ElidedFactoriesTest.java | 7 +- .../MapBindingComponentProcessorTest.java | 62 ++++++--------- .../codegen/MapRequestRepresentationTest.java | 30 +++++++- ...MapRequestRepresentationWithGuavaTest.java | 39 +++++++++- ...OptionalBindingRequestFulfillmentTest.java | 9 ++- .../ProductionComponentProcessorTest.java | 7 +- 14 files changed, 245 insertions(+), 148 deletions(-) diff --git a/java/dagger/internal/codegen/binding/BindingGraph.java b/java/dagger/internal/codegen/binding/BindingGraph.java index 9d3c1fc853e..c9a0ce93e97 100644 --- a/java/dagger/internal/codegen/binding/BindingGraph.java +++ b/java/dagger/internal/codegen/binding/BindingGraph.java @@ -18,6 +18,7 @@ import static com.google.common.collect.Iterables.transform; import static dagger.internal.codegen.extension.DaggerCollectors.toOptional; +import static dagger.internal.codegen.extension.DaggerStreams.instancesOf; import static dagger.internal.codegen.extension.DaggerStreams.presentValues; import static dagger.internal.codegen.extension.DaggerStreams.stream; import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList; @@ -40,15 +41,20 @@ import dagger.internal.codegen.base.TarjanSCCs; import dagger.spi.model.BindingGraph.ChildFactoryMethodEdge; import dagger.spi.model.BindingGraph.ComponentNode; +import dagger.spi.model.BindingGraph.DependencyEdge; import dagger.spi.model.BindingGraph.Edge; import dagger.spi.model.BindingGraph.Node; +import dagger.spi.model.BindingKind; import dagger.spi.model.ComponentPath; import dagger.spi.model.DaggerTypeElement; +import dagger.spi.model.DependencyRequest; import dagger.spi.model.Key; import java.util.Comparator; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.stream.Stream; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; @@ -90,11 +96,14 @@ static TopLevelBindingGraph create( // AutoValue to prevent exposing this data outside of the class. topLevelBindingGraph.componentNodes = componentNodes; topLevelBindingGraph.subcomponentNodes = subcomponentNodesBuilder.build(); + topLevelBindingGraph.frameworkTypeBindings = + frameworkRequestBindingSet(network, topLevelBindingGraph.bindings()); return topLevelBindingGraph; } private ImmutableMap componentNodes; private ImmutableSetMultimap subcomponentNodes; + private ImmutableSet frameworkTypeBindings; TopLevelBindingGraph() {} @@ -148,6 +157,63 @@ public ImmutableSet> stronglyConnectedNodes() { node -> network().successors(node).stream().sorted(nodeOrder()).collect(toImmutableList())); } + + public boolean hasframeworkRequest(Binding binding) { + return frameworkTypeBindings.contains(binding); + } + + private static ImmutableSet frameworkRequestBindingSet( + ImmutableNetwork network, ImmutableSet bindings) { + Set frameworkRequestBindings = new HashSet<>(); + for (dagger.spi.model.Binding binding : bindings) { + // When a delegator binding received an instance request, it will manually create an + // instance request for its delegated binding in direct instance binding representation. It + // is possible a provider.get() expression will be returned to satisfy the request for the + // delegated binding. In this case, the returned expression should have a type cast, because + // the returned expression's type can be Object. The type cast is handled by + // DelegateRequestRepresentation. If we change to use framework instance binding + // representation to handle the delegate bindings, then we will be missing the type cast. + // Because in this case, when requesting an instance for the delegator binding, framework + // instance binding representation will manually create a provider request for delegated + // binding first, then use DerivedFromFrameworkInstanceRequestRepresentaion to wrap that + // provider expression. Then we will still have a provider.get(), but it is generated with + // two different request representation, so the type cast step is skipped. As the result, we + // can't directly switch the delegation binding to always use the framework instance if a + // framework request already exists. So I'm adding an temporary exemption for delegate + // binding here to make it still use the old generation logic. We might be able to remove + // the exemption when we handle the type cast differently. + // TODO(wanyingd): fix the type cast problem and remove the exemption for delegate binding. + if (binding.kind().equals(BindingKind.DELEGATE) + // In fast init mode, for Assisted injection binding, since we manually create a direct + // instance when the request type is a Provider, then there can't really be any + // framework requests for the binding. + // TODO(wanyingd): inline assisted injection binding expression in assisted factory. + || binding.kind().equals(BindingKind.ASSISTED_INJECTION)) { + continue; + } + ImmutableList edges = + network.inEdges(binding).stream() + .flatMap(instancesOf(DependencyEdge.class)) + .collect(toImmutableList()); + for (DependencyEdge edge : edges) { + DependencyRequest request = edge.dependencyRequest(); + switch (request.kind()) { + case INSTANCE: + case FUTURE: + continue; + case PRODUCED: + case PRODUCER: + case MEMBERS_INJECTION: + case PROVIDER_OF_LAZY: + case LAZY: + case PROVIDER: + frameworkRequestBindings.add(((BindingNode) binding).delegate()); + break; + } + } + } + return ImmutableSet.copyOf(frameworkRequestBindings); + } } static BindingGraph create( diff --git a/java/dagger/internal/codegen/writing/FrameworkFieldInitializer.java b/java/dagger/internal/codegen/writing/FrameworkFieldInitializer.java index c64bb270f98..6bbfeb6a7ae 100644 --- a/java/dagger/internal/codegen/writing/FrameworkFieldInitializer.java +++ b/java/dagger/internal/codegen/writing/FrameworkFieldInitializer.java @@ -111,13 +111,13 @@ private void initializeField() { case INITIALIZING: fieldSpec = getOrCreateField(); - // If this is an ASSISTED_FACTORY binding in fastInit, then we don't have to worry about - // cycles since the creation of the factory itself doesn't actually take any dependencies. - // TODO(wanyingd): all switching providers do not need the delegation - if (isFastInit && binding.kind().equals(BindingKind.ASSISTED_FACTORY)) { + // We were recursively invoked, so create a delegate factory instead to break the loop. + // However, because SwitchingProvider takes no dependencies, even if they are recursively + // invoked, we don't need to delegate it since there is no dependency cycle. + if (ProvisionBindingRepresentation.usesSwitchingProvider(binding, isFastInit)) { break; } - // We were recursively invoked, so create a delegate factory instead + fieldInitializationState = InitializationState.DELEGATED; shardImplementation.addInitialization( CodeBlock.of("this.$N = new $T<>();", fieldSpec, delegateType())); diff --git a/java/dagger/internal/codegen/writing/ProvisionBindingRepresentation.java b/java/dagger/internal/codegen/writing/ProvisionBindingRepresentation.java index 73ca2bc4602..22970abb03b 100644 --- a/java/dagger/internal/codegen/writing/ProvisionBindingRepresentation.java +++ b/java/dagger/internal/codegen/writing/ProvisionBindingRepresentation.java @@ -25,6 +25,7 @@ import dagger.assisted.AssistedInject; import dagger.internal.codegen.binding.BindingGraph; import dagger.internal.codegen.binding.BindingRequest; +import dagger.internal.codegen.binding.ContributionBinding; import dagger.internal.codegen.binding.ProvisionBinding; import dagger.internal.codegen.compileroption.CompilerOptions; import dagger.internal.codegen.langmodel.DaggerTypes; @@ -59,10 +60,8 @@ final class ProvisionBindingRepresentation implements BindingRepresentation { this.directInstanceBindingRepresentation = directInstanceBindingRepresentationFactory.create(binding); FrameworkInstanceSupplier frameworkInstanceSupplier = null; - if (usesSwitchingProvider()) { - frameworkInstanceSupplier = - switchingProviderInstanceSupplierFactory.create( - binding, directInstanceBindingRepresentation); + if (usesSwitchingProvider(binding, isFastInit)) { + frameworkInstanceSupplier = switchingProviderInstanceSupplierFactory.create(binding); } else if (usesStaticFactoryCreation(binding, isFastInit)) { frameworkInstanceSupplier = staticFactoryInstanceSupplierFactory.create(binding); } else { @@ -74,16 +73,27 @@ final class ProvisionBindingRepresentation implements BindingRepresentation { @Override public RequestRepresentation getRequestRepresentation(BindingRequest request) { - return usesDirectInstanceExpression(request.requestKind(), binding, graph, isFastInit) + return usesDirectInstanceExpression(request.requestKind()) ? directInstanceBindingRepresentation.getRequestRepresentation(request) : frameworkInstanceBindingRepresentation.getRequestRepresentation(request); } - static boolean usesDirectInstanceExpression( - RequestKind requestKind, ProvisionBinding binding, BindingGraph graph, boolean isFastInit) { + private boolean usesDirectInstanceExpression(RequestKind requestKind) { if (requestKind != RequestKind.INSTANCE && requestKind != RequestKind.FUTURE) { return false; } + + // In fast init mode, we can avoid generating direct instance expressions if a framework + // instance expression already exists in the graph. Default mode has more edge cases, so can not + // be handled with simple pre-check in the graph. For example, a provider for a subcomponent + // builder is backed with its direct instance, returning framework instance for both cases will + // form a loop. There are also difficulties introduced by manually created framework requests. + // TODO(wanyingd): refactor framework instance so that we don't need to generate both direct + // instance and framework instance representation for the same binding. + if (isFastInit && graph.topLevelBindingGraph().hasframeworkRequest(binding)) { + return false; + } + switch (binding.kind()) { case MEMBERS_INJECTOR: // Currently, we always use a framework instance for MembersInjectors, e.g. @@ -107,7 +117,7 @@ static boolean usesDirectInstanceExpression( } } - private boolean usesSwitchingProvider() { + public static boolean usesSwitchingProvider(ContributionBinding binding, boolean isFastInit) { if (!isFastInit) { return false; } diff --git a/java/dagger/internal/codegen/writing/SwitchingProviderInstanceSupplier.java b/java/dagger/internal/codegen/writing/SwitchingProviderInstanceSupplier.java index b01bf745704..0d81c4a9214 100644 --- a/java/dagger/internal/codegen/writing/SwitchingProviderInstanceSupplier.java +++ b/java/dagger/internal/codegen/writing/SwitchingProviderInstanceSupplier.java @@ -16,10 +16,8 @@ package dagger.internal.codegen.writing; -import static dagger.internal.codegen.binding.BindingRequest.bindingRequest; import static dagger.internal.codegen.javapoet.TypeNames.DOUBLE_CHECK; import static dagger.internal.codegen.javapoet.TypeNames.SINGLE_CHECK; -import static dagger.internal.codegen.writing.ProvisionBindingRepresentation.usesDirectInstanceExpression; import com.squareup.javapoet.CodeBlock; import dagger.assisted.Assisted; @@ -27,11 +25,9 @@ import dagger.assisted.AssistedInject; import dagger.internal.codegen.binding.Binding; import dagger.internal.codegen.binding.BindingGraph; -import dagger.internal.codegen.binding.BindingRequest; import dagger.internal.codegen.binding.ProvisionBinding; import dagger.internal.codegen.writing.FrameworkFieldInitializer.FrameworkInstanceCreationExpression; import dagger.spi.model.BindingKind; -import dagger.spi.model.RequestKind; /** * An object that initializes a framework-type component field for a binding using instances created @@ -43,26 +39,14 @@ final class SwitchingProviderInstanceSupplier implements FrameworkInstanceSuppli @AssistedInject SwitchingProviderInstanceSupplier( @Assisted ProvisionBinding binding, - @Assisted DirectInstanceBindingRepresentation directInstanceBindingRepresentation, SwitchingProviders switchingProviders, BindingGraph graph, ComponentImplementation componentImplementation, UnscopedDirectInstanceRequestRepresentationFactory unscopedDirectInstanceRequestRepresentationFactory) { - BindingRequest instanceRequest = bindingRequest(binding.key(), RequestKind.INSTANCE); FrameworkInstanceCreationExpression frameworkInstanceCreationExpression = switchingProviders.newFrameworkInstanceCreationExpression( - binding, - // Use the directInstanceBindingRepresentation if possible, that way we share a private - // method implementation if one already exists. Otherwise, we use the - // unscopedDirectInstanceRequestRepresentation and, since we're guaranteed this is the - // only place that will be using the expression in this case, there is no need to wrap - // the expression in a private method. - // Note: we can't use ComponentBindingRepresentation.getRequestRepresentation( - // instanceRequest) here, since that would return fooProvider.get() and cause a cycle. - usesDirectInstanceExpression(RequestKind.INSTANCE, binding, graph, true) - ? directInstanceBindingRepresentation.getRequestRepresentation(instanceRequest) - : unscopedDirectInstanceRequestRepresentationFactory.create(binding)); + binding, unscopedDirectInstanceRequestRepresentationFactory.create(binding)); this.frameworkInstanceSupplier = new FrameworkFieldInitializer( componentImplementation, binding, scope(binding, frameworkInstanceCreationExpression)); @@ -91,8 +75,6 @@ private FrameworkInstanceCreationExpression scope( @AssistedFactory static interface Factory { - SwitchingProviderInstanceSupplier create( - ProvisionBinding binding, - DirectInstanceBindingRepresentation directInstanceBindingRepresentation); + SwitchingProviderInstanceSupplier create(ProvisionBinding binding); } } diff --git a/javatests/dagger/internal/codegen/AssistedFactoryTest.java b/javatests/dagger/internal/codegen/AssistedFactoryTest.java index 919c8d5e65d..a78c05192a1 100644 --- a/javatests/dagger/internal/codegen/AssistedFactoryTest.java +++ b/javatests/dagger/internal/codegen/AssistedFactoryTest.java @@ -125,7 +125,7 @@ public void testAssistedFactory() { " @Override", " public T get() {", " switch (id) {", - " case 0: // test.FooFactory ", + " case 0:", " return (T) new FooFactory() {", " @Override", " public Foo create(String str) {", @@ -249,7 +249,7 @@ public void testAssistedFactoryCycle() { " @Override", " public T get() {", " switch (id) {", - " case 0: // test.FooFactory ", + " case 0:", " return (T) new FooFactory() {", " @Override", " public Foo create(String str) {", @@ -375,7 +375,7 @@ public void testInjectParamDuplicateNames() { " @Override", " public T get() {", " switch (id) {", - " case 0: // test.FooFactory ", + " case 0:", " return (T) new FooFactory() {", " @Override", " public Foo create(Integer arg) {", @@ -522,7 +522,7 @@ public void testAssistParamDuplicateNames() { " @Override", " public T get() {", " switch (id) {", - " case 0: // test.FooFactory ", + " case 0:", " return (T) new FooFactory() {", " @Override", " public Foo create(Integer arg) {", @@ -756,8 +756,7 @@ public void testParameterizedAssistParam() { " @Override", " public T get() {", " switch (id) {", - " case 0: // test.FooFactory ", - " return (T) new FooFactory() {", + " case 0: return (T) new FooFactory() {", " @Override", " public Foo create(String arg) {", " return testComponent.fooOfString(arg);", diff --git a/javatests/dagger/internal/codegen/ComponentProcessorTest.java b/javatests/dagger/internal/codegen/ComponentProcessorTest.java index 095cc94bc04..28b294dbb13 100644 --- a/javatests/dagger/internal/codegen/ComponentProcessorTest.java +++ b/javatests/dagger/internal/codegen/ComponentProcessorTest.java @@ -188,7 +188,7 @@ public ComponentProcessorTest(CompilerMode compilerMode) { "", " @Override", " public SomeInjectableType someInjectableType() {", - " return new SomeInjectableType();", + " return someInjectableTypeProvider.get();", " }", "", " @Override", @@ -983,32 +983,42 @@ public void testDefaultPackage() { " Provider selfProvider();", "}"); JavaFileObject generatedComponent = - JavaFileObjects.forSourceLines( - "test.DaggerSimpleComponent", - "package test;", - "", - GeneratedLines.generatedAnnotations(), - "final class DaggerSimpleComponent implements SimpleComponent {", - " private final DaggerSimpleComponent simpleComponent = this;", - "", - " private Provider simpleComponentProvider;", - "", - " @SuppressWarnings(\"unchecked\")", - " private void initialize() {", - " this.simpleComponentProvider =", - " InstanceFactory.create((SimpleComponent) simpleComponent);", - " }", - "", - " @Override", - " public SomeInjectableType someInjectableType() {", - " return new SomeInjectableType(this)", - " }", - "", - " @Override", - " public Provider selfProvider() {", - " return simpleComponentProvider;", - " }", - "}"); + compilerMode + .javaFileBuilder("test.DaggerSimpleComponent") + .addLines( + "package test;", + "", + GeneratedLines.generatedAnnotations(), + "final class DaggerSimpleComponent implements SimpleComponent {", + " private final DaggerSimpleComponent simpleComponent = this;", + "", + " private Provider simpleComponentProvider;", + "", + " @SuppressWarnings(\"unchecked\")", + " private void initialize() {", + " this.simpleComponentProvider =", + " InstanceFactory.create((SimpleComponent) simpleComponent);", + " }", + "") + .addLinesIn( + DEFAULT_MODE, + " @Override", + " public SomeInjectableType someInjectableType() {", + " return new SomeInjectableType(this)", + " }") + .addLinesIn( + FAST_INIT_MODE, + " @Override", + " public SomeInjectableType someInjectableType() {", + " return new SomeInjectableType(simpleComponentProvider.get());", + " }") + .addLines( + " @Override", + " public Provider selfProvider() {", + " return simpleComponentProvider;", + " }", + "}") + .build(); Compilation compilation = compilerWithOptions(compilerMode.javacopts()) .compile(injectableTypeFile, componentFile); @@ -2743,10 +2753,6 @@ public void providerComponentType() { " }") .addLinesIn( FAST_INIT_MODE, - " private Foo foo() {", - " return new Foo(new Bar());", - " }", - "", " @SuppressWarnings(\"unchecked\")", " private void initialize() {", " this.fooProvider = new SwitchingProvider<>(testComponent, 0);", @@ -2754,7 +2760,7 @@ public void providerComponentType() { "", " @Override", " public SomeEntryPoint someEntryPoint() {", - " return new SomeEntryPoint(foo(), fooProvider);", + " return new SomeEntryPoint(fooProvider.get(), fooProvider);", " }", "", " private static final class SwitchingProvider implements Provider {", @@ -2762,11 +2768,13 @@ public void providerComponentType() { " @Override", " public T get() {", " switch (id) {", - " case 0: return (T) testComponent.foo();", + " case 0: return (T) new Foo(new Bar());", + "", " default: throw new AssertionError(id);", " }", " }", - " }") + " }", + "}") .build()); } diff --git a/javatests/dagger/internal/codegen/ComponentRequirementFieldTest.java b/javatests/dagger/internal/codegen/ComponentRequirementFieldTest.java index 76d74f2f5c9..b0e30e80b26 100644 --- a/javatests/dagger/internal/codegen/ComponentRequirementFieldTest.java +++ b/javatests/dagger/internal/codegen/ComponentRequirementFieldTest.java @@ -391,11 +391,6 @@ public void componentRequirementNeededInFactoryCreationOfSubcomponent() { " ParentModule_ContributionFactory.contribution());", " }", "", - " private Object object() {", - " return ParentModule_ReliesOnMultibindingFactory", - " .reliesOnMultibinding(parentModule, setOfObject());", - " }", - "", " @SuppressWarnings(\"unchecked\")", " private void initialize(final ParentModule parentModuleParam) {", " this.reliesOnMultibindingProvider =", @@ -435,11 +430,6 @@ public void componentRequirementNeededInFactoryCreationOfSubcomponent() { " ChildModule_ContributionFactory.contribution());", " }", "", - " private Object object() {", - " return ParentModule_ReliesOnMultibindingFactory.reliesOnMultibinding(", - " testComponent.parentModule, setOfObject());", - " }", - "", " @SuppressWarnings(\"unchecked\")", " private void initialize() {", " this.reliesOnMultibindingProvider =", @@ -457,7 +447,11 @@ public void componentRequirementNeededInFactoryCreationOfSubcomponent() { " @Override", " public T get() {", " switch (id) {", - " case 0: return (T) testSubcomponentImpl.object();", + " case 0:", + " return (T) ParentModule_ReliesOnMultibindingFactory", + " .reliesOnMultibinding(", + " testComponent.parentModule,", + " testSubcomponentImpl.setOfObject());", " default: throw new AssertionError(id);", " }", " }", @@ -469,7 +463,10 @@ public void componentRequirementNeededInFactoryCreationOfSubcomponent() { " @Override", " public T get() {", " switch (id) {", - " case 0: return (T) testComponent.object();", + " case 0:", + " return (T) ParentModule_ReliesOnMultibindingFactory", + " .reliesOnMultibinding(", + " testComponent.parentModule, testComponent.setOfObject());", " default: throw new AssertionError(id);", " }", " }", diff --git a/javatests/dagger/internal/codegen/ComponentShardTest.java b/javatests/dagger/internal/codegen/ComponentShardTest.java index 9912db927a9..015a8cf7006 100644 --- a/javatests/dagger/internal/codegen/ComponentShardTest.java +++ b/javatests/dagger/internal/codegen/ComponentShardTest.java @@ -240,15 +240,12 @@ public void testNewShardCreated() { " private void initialize() {", " this.binding5Provider = DoubleCheck.provider(", " new SwitchingProvider(testComponent, 4));", - " this.binding2Provider = new DelegateFactory<>();", " this.binding4Provider = DoubleCheck.provider(", " new SwitchingProvider(testComponent, 3));", " this.binding3Provider = DoubleCheck.provider(", " new SwitchingProvider(testComponent, 2));", - " DelegateFactory.setDelegate(", - " binding2Provider,", - " DoubleCheck.provider(", - " new SwitchingProvider(testComponent, 1)));", + " this.binding2Provider = DoubleCheck.provider(", + " new SwitchingProvider(testComponent, 1));", " }", " }") .addLines( diff --git a/javatests/dagger/internal/codegen/ElidedFactoriesTest.java b/javatests/dagger/internal/codegen/ElidedFactoriesTest.java index d2305980c79..5180c4894bf 100644 --- a/javatests/dagger/internal/codegen/ElidedFactoriesTest.java +++ b/javatests/dagger/internal/codegen/ElidedFactoriesTest.java @@ -202,10 +202,6 @@ public void simpleComponent_injectsProviderOf_dependsOnScoped() { " }") .addLinesIn( FAST_INIT_MODE, - " private DependsOnScoped dependsOnScoped() {", - " return new DependsOnScoped(scopedTypeProvider.get());", - " }", - "", " @SuppressWarnings(\"unchecked\")", " private void initialize() {", " this.scopedTypeProvider =", @@ -226,7 +222,8 @@ public void simpleComponent_injectsProviderOf_dependsOnScoped() { " @Override", " public T get() {", " switch (id) {", - " case 0: return (T) simpleComponent.dependsOnScoped();", + " case 0: return (T) new DependsOnScoped(", + " simpleComponent.scopedTypeProvider.get());", " case 1: return (T) new ScopedType();", " default: throw new AssertionError(id);", " }", diff --git a/javatests/dagger/internal/codegen/MapBindingComponentProcessorTest.java b/javatests/dagger/internal/codegen/MapBindingComponentProcessorTest.java index 5ddc465bd69..622ca860189 100644 --- a/javatests/dagger/internal/codegen/MapBindingComponentProcessorTest.java +++ b/javatests/dagger/internal/codegen/MapBindingComponentProcessorTest.java @@ -170,13 +170,6 @@ public void mapBindingsWithEnumKey() { " }") .addLinesIn( FAST_INIT_MODE, - " private Map>", - " mapOfPathEnumAndProviderOfHandler() {", - " return ImmutableMap.>of(", - " PathEnum.ADMIN, provideAdminHandlerProvider,", - " PathEnum.LOGIN, provideLoginHandlerProvider);", - " }", - "", " @SuppressWarnings(\"unchecked\")", " private void initialize(final MapModuleOne mapModuleOneParam,", " final MapModuleTwo mapModuleTwoParam) {", @@ -200,8 +193,11 @@ public void mapBindingsWithEnumKey() { " @Override", " public T get() {", " switch (id) {", - " case 0: return (T) testComponent", - " .mapOfPathEnumAndProviderOfHandler();", + " case 0: return (T) ImmutableMap.>of(", + " PathEnum.ADMIN,", + " testComponent.provideAdminHandlerProvider,", + " PathEnum.LOGIN,", + " testComponent.provideLoginHandlerProvider);", " case 1: return (T) MapModuleOne_ProvideAdminHandlerFactory", " .provideAdminHandler(testComponent.mapModuleOne);", " case 2: return (T) MapModuleTwo_ProvideLoginHandlerFactory", @@ -537,12 +533,6 @@ public void mapBindingsWithStringKey() { " }") .addLinesIn( FAST_INIT_MODE, - " private Map> mapOfStringAndProviderOfHandler() {", - " return ImmutableMap.>of(", - " \"Admin\", provideAdminHandlerProvider,", - " \"Login\", provideLoginHandlerProvider);", - " }", - "", " @SuppressWarnings(\"unchecked\")", " private void initialize(", " final MapModuleOne mapModuleOneParam,", @@ -566,8 +556,11 @@ public void mapBindingsWithStringKey() { " @Override", " public T get() {", " switch (id) {", - " case 0: return (T) testComponent", - " .mapOfStringAndProviderOfHandler();", + " case 0: return (T) ImmutableMap.>of(", + " \"Admin\",", + " testComponent.provideAdminHandlerProvider,", + " \"Login\",", + " testComponent.provideLoginHandlerProvider);", " case 1: return (T) MapModuleOne_ProvideAdminHandlerFactory", " .provideAdminHandler(testComponent.mapModuleOne);", " case 2: return (T) MapModuleTwo_ProvideLoginHandlerFactory", @@ -701,15 +694,6 @@ public void mapBindingsWithWrappedKey() { " }") .addLinesIn( FAST_INIT_MODE, - " private Map>", - " mapOfWrappedClassKeyAndProviderOfHandler() {", - " return ImmutableMap.>of(", - " WrappedClassKeyCreator.createWrappedClassKey(Integer.class),", - " provideAdminHandlerProvider,", - " WrappedClassKeyCreator.createWrappedClassKey(Long.class),", - " provideLoginHandlerProvider);", - " }", - "", " @SuppressWarnings(\"unchecked\")", " private void initialize(final MapModuleOne mapModuleOneParam,", " final MapModuleTwo mapModuleTwoParam) {", @@ -732,8 +716,12 @@ public void mapBindingsWithWrappedKey() { " @Override", " public T get() {", " switch (id) {", - " case 0: return (T) testComponent", - " .mapOfWrappedClassKeyAndProviderOfHandler();", + " case 0:", + " return (T) ImmutableMap.>of(", + " WrappedClassKeyCreator.createWrappedClassKey(Integer.class),", + " testComponent.provideAdminHandlerProvider,", + " WrappedClassKeyCreator.createWrappedClassKey(Long.class),", + " testComponent.provideLoginHandlerProvider);", " case 1: return (T) MapModuleOne_ProvideAdminHandlerFactory", " .provideAdminHandler(testComponent.mapModuleOne);", " case 2: return (T) MapModuleTwo_ProvideLoginHandlerFactory", @@ -868,16 +856,6 @@ public void mapBindingsWithNonProviderValue() { FAST_INIT_MODE, " private Provider> mapOfPathEnumAndHandlerProvider;", "", - " private Map mapOfPathEnumAndHandler() {", - " return ImmutableMap.of(", - " PathEnum.ADMIN,", - " MapModuleOne_ProvideAdminHandlerFactory", - " .provideAdminHandler(mapModuleOne),", - " PathEnum.LOGIN,", - " MapModuleTwo_ProvideLoginHandlerFactory", - " .provideLoginHandler(mapModuleTwo));", - " }", - "", " @SuppressWarnings(\"unchecked\")", " private void initialize(final MapModuleOne mapModuleOneParam,", " final MapModuleTwo mapModuleTwoParam) {", @@ -896,7 +874,13 @@ public void mapBindingsWithNonProviderValue() { " @Override", " public T get() {", " switch (id) {", - " case 0: return (T) testComponent.mapOfPathEnumAndHandler();", + " case 0: return (T) ImmutableMap.of(", + " PathEnum.ADMIN,", + " MapModuleOne_ProvideAdminHandlerFactory.provideAdminHandler(", + " testComponent.mapModuleOne),", + " PathEnum.LOGIN,", + " MapModuleTwo_ProvideLoginHandlerFactory.provideLoginHandler(", + " testComponent.mapModuleTwo));", " default: throw new AssertionError(id);", " }", " }", diff --git a/javatests/dagger/internal/codegen/MapRequestRepresentationTest.java b/javatests/dagger/internal/codegen/MapRequestRepresentationTest.java index 3d0f264bc3c..1f59bd8de58 100644 --- a/javatests/dagger/internal/codegen/MapRequestRepresentationTest.java +++ b/javatests/dagger/internal/codegen/MapRequestRepresentationTest.java @@ -115,13 +115,22 @@ public void mapBindings() { " public Map> providerStrings() {", " return Collections.>emptyMap();", " }", - "", + "") + .addLinesIn( + DEFAULT_MODE, " @Override", " public Map ints() {", " return Collections.", " singletonMap(0, MapModule.provideInt());", - " }", - "", + " }") + .addLinesIn( + FAST_INIT_MODE, + " @Override", + " public Map ints() {", + " return Collections.singletonMap(0," + + " provideIntProvider.get());", + " }") + .addLines( " @Override", " public Map> providerInts() {", " return Collections.>singletonMap(") @@ -129,7 +138,8 @@ public void mapBindings() { DEFAULT_MODE, // " 0, MapModule_ProvideIntFactory.create());") .addLinesIn(FAST_INIT_MODE, " 0, provideIntProvider;") - .addLines( + .addLinesIn( + DEFAULT_MODE, " }", "", " @Override", @@ -139,8 +149,20 @@ public void mapBindings() { " .put(1L, MapModule.provideLong1())", " .put(2L, MapModule.provideLong2())", " .build();", + " }") + .addLinesIn( + FAST_INIT_MODE, " }", "", + " @Override", + " public Map longs() {", + " return MapBuilder.newMapBuilder(3)", + " .put(0L, provideLong0Provider.get())", + " .put(1L, provideLong1Provider.get())", + " .put(2L, provideLong2Provider.get())", + " .build();", + " }") + .addLines( " @Override", " public Map> providerLongs() {", " return MapBuilder.>newMapBuilder(3)") diff --git a/javatests/dagger/internal/codegen/MapRequestRepresentationWithGuavaTest.java b/javatests/dagger/internal/codegen/MapRequestRepresentationWithGuavaTest.java index 06d20b2f20a..db7d039f86b 100644 --- a/javatests/dagger/internal/codegen/MapRequestRepresentationWithGuavaTest.java +++ b/javatests/dagger/internal/codegen/MapRequestRepresentationWithGuavaTest.java @@ -151,12 +151,19 @@ public void mapBindings() { " @Override", " public Map> providerStrings() {", " return ImmutableMap.>of();", - " }", - "", + " }") + .addLinesIn( + DEFAULT_MODE, " @Override", " public Map ints() {", " return ImmutableMap.of(0, MapModule.provideInt());", " }") + .addLinesIn( + FAST_INIT_MODE, + " @Override", + " public Map ints() {", + " return ImmutableMap.of(0, provideIntProvider.get());", + " }") .addLinesIn( DEFAULT_MODE, " @Override", @@ -170,7 +177,8 @@ public void mapBindings() { " public Map> providerInts() {", " return ImmutableMap.>of(0, provideIntProvider);", " }") - .addLines( + .addLinesIn( + DEFAULT_MODE, " @Override", " public Map longs() {", " return ImmutableMap.of(", @@ -178,6 +186,15 @@ public void mapBindings() { " 1L, MapModule.provideLong1(),", " 2L, MapModule.provideLong2());", " }") + .addLinesIn( + FAST_INIT_MODE, + " @Override", + " public Map longs() {", + " return ImmutableMap.of(", + " 0L, provideLong0Provider.get(),", + " 1L, provideLong1Provider.get(),", + " 2L, provideLong2Provider.get());", + " }") .addLinesIn( DEFAULT_MODE, " @Override", @@ -218,7 +235,8 @@ public void mapBindings() { " this.provideLong5Provider =", " new SwitchingProvider<>(testComponent, subImpl, 2);", " }") - .addLines( + .addLinesIn( + DEFAULT_MODE, " @Override", " public Map longs() {", " return ImmutableMap.builderWithExpectedSize(6)", @@ -230,6 +248,19 @@ public void mapBindings() { " .put(5L, SubcomponentMapModule.provideLong5())", " .build();", " }") + .addLinesIn( + FAST_INIT_MODE, + " @Override", + " public Map longs() {", + " return ImmutableMap.builderWithExpectedSize(6)", + " .put(0L, testComponent.provideLong0Provider.get())", + " .put(1L, testComponent.provideLong1Provider.get())", + " .put(2L, testComponent.provideLong2Provider.get())", + " .put(3L, provideLong3Provider.get())", + " .put(4L, provideLong4Provider.get())", + " .put(5L, provideLong5Provider.get())", + " .build();", + " }") .addLinesIn( DEFAULT_MODE, " @Override", diff --git a/javatests/dagger/internal/codegen/OptionalBindingRequestFulfillmentTest.java b/javatests/dagger/internal/codegen/OptionalBindingRequestFulfillmentTest.java index f7d33d712e3..c78d1995feb 100644 --- a/javatests/dagger/internal/codegen/OptionalBindingRequestFulfillmentTest.java +++ b/javatests/dagger/internal/codegen/OptionalBindingRequestFulfillmentTest.java @@ -117,11 +117,18 @@ public void inlinedOptionalBindings() { " private void initialize() {", " this.provideMaybeProvider = new SwitchingProvider<>(testComponent, 0);", " }") - .addLines( + .addLinesIn( + DEFAULT_MODE, " @Override", " public Optional maybe() {", " return Optional.of(Maybe_MaybeModule_ProvideMaybeFactory.provideMaybe());", " }") + .addLinesIn( + FAST_INIT_MODE, + " @Override", + " public Optional maybe() {", + " return Optional.of(provideMaybeProvider.get());", + " }") .addLinesIn( DEFAULT_MODE, " @Override", diff --git a/javatests/dagger/internal/codegen/ProductionComponentProcessorTest.java b/javatests/dagger/internal/codegen/ProductionComponentProcessorTest.java index e45857ddee2..e0ad55abc06 100644 --- a/javatests/dagger/internal/codegen/ProductionComponentProcessorTest.java +++ b/javatests/dagger/internal/codegen/ProductionComponentProcessorTest.java @@ -275,10 +275,6 @@ public void simpleComponent() { " }") .addLinesIn( FAST_INIT_MODE, - " private TestClass.B b() {", - " return TestClass_BModule_BFactory.b(bModule, new TestClass.C());", - " }", - "", " @SuppressWarnings(\"unchecked\")", " private void initialize(", " final TestClass.AModule aModuleParam,", @@ -325,7 +321,8 @@ public void simpleComponent() { " .monitor(", " simpleComponent.simpleComponentProvider,", " SetFactory.empty());", - " case 2: return (T) simpleComponent.b();", + " case 2: return (T) TestClass_BModule_BFactory.b(", + " simpleComponent.bModule, new TestClass.C());", " default: throw new AssertionError(id);", " }", " }",