Skip to content

Commit

Permalink
Make DerivedFromFrameworkInstanceRequestRepresentation a wrapper fo…
Browse files Browse the repository at this point in the history
…r the passed in `RequestRepresentation` from `BindingRepresentation`.

RELNOTES=N/A
PiperOrigin-RevId: 398826986
  • Loading branch information
wanyingd1996 authored and Dagger Team committed Sep 24, 2021
1 parent 7997b1d commit 756922b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,59 +17,58 @@
package dagger.internal.codegen.writing;

import static com.google.common.base.Preconditions.checkNotNull;
import static dagger.internal.codegen.binding.BindingRequest.bindingRequest;

import com.squareup.javapoet.ClassName;
import dagger.assisted.Assisted;
import dagger.assisted.AssistedFactory;
import dagger.assisted.AssistedInject;
import dagger.internal.codegen.binding.BindingRequest;
import dagger.internal.codegen.binding.ComponentDescriptor.ComponentMethodDescriptor;
import dagger.internal.codegen.binding.FrameworkType;
import dagger.internal.codegen.javapoet.Expression;
import dagger.internal.codegen.langmodel.DaggerTypes;
import dagger.spi.model.RequestKind;

/** A binding expression that depends on a framework instance. */
final class DerivedFromFrameworkInstanceRequestRepresentation extends RequestRepresentation {
private final BindingRequest bindingRequest;
private final BindingRequest frameworkRequest;
private final RequestRepresentation frameworkRequestRepresentation;
private final RequestKind requestKind;
private final FrameworkType frameworkType;
private final ComponentRequestRepresentations componentRequestRepresentations;
private final DaggerTypes types;

@AssistedInject
DerivedFromFrameworkInstanceRequestRepresentation(
@Assisted BindingRequest bindingRequest,
@Assisted RequestRepresentation frameworkRequestRepresentation,
@Assisted RequestKind requestKind,
@Assisted FrameworkType frameworkType,
ComponentRequestRepresentations componentRequestRepresentations,
DaggerTypes types) {
this.bindingRequest = checkNotNull(bindingRequest);
this.frameworkRequestRepresentation = checkNotNull(frameworkRequestRepresentation);
this.requestKind = requestKind;
this.frameworkType = checkNotNull(frameworkType);
this.frameworkRequest = bindingRequest(bindingRequest.key(), frameworkType);
this.componentRequestRepresentations = componentRequestRepresentations;
this.types = types;
}

@Override
Expression getDependencyExpression(ClassName requestingClass) {
return frameworkType.to(
bindingRequest.requestKind(),
componentRequestRepresentations.getDependencyExpression(frameworkRequest, requestingClass),
requestKind,
frameworkRequestRepresentation.getDependencyExpression(requestingClass),
types);
}

@Override
Expression getDependencyExpressionForComponentMethod(
ComponentMethodDescriptor componentMethod, ComponentImplementation component) {
Expression frameworkInstance =
componentRequestRepresentations.getDependencyExpressionForComponentMethod(
frameworkRequest, componentMethod, component);
return frameworkType.to(bindingRequest.requestKind(), frameworkInstance, types);
frameworkRequestRepresentation.getDependencyExpressionForComponentMethod(
componentMethod, component);
return frameworkType.to(requestKind, frameworkInstance, types);
}

@AssistedFactory
static interface Factory {
DerivedFromFrameworkInstanceRequestRepresentation create(
BindingRequest request, FrameworkType frameworkType);
RequestRepresentation frameworkRequestRepresentation,
RequestKind requestKind,
FrameworkType frameworkType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,13 @@ public RequestRepresentation getRequestRepresentation(BindingRequest request) {
private RequestRepresentation getRequestRepresentationUncached(BindingRequest request) {
switch (request.requestKind()) {
case INSTANCE:
return derivedFromFrameworkInstanceRequestRepresentationFactory.create(
bindingRequest(binding.key(), RequestKind.INSTANCE), FrameworkType.PROVIDER);

case PROVIDER:
return providerRequestRepresentation;

case LAZY:
case PRODUCED:
case PROVIDER_OF_LAZY:
return derivedFromFrameworkInstanceRequestRepresentationFactory.create(
request, FrameworkType.PROVIDER);

providerRequestRepresentation, request.requestKind(), FrameworkType.PROVIDER);
case PROVIDER:
return providerRequestRepresentation;
case PRODUCER:
return producerFromProviderRepresentation;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import dagger.internal.codegen.binding.FrameworkType;
import dagger.internal.codegen.binding.ProductionBinding;
import dagger.internal.codegen.langmodel.DaggerTypes;
import dagger.internal.codegen.writing.FrameworkFieldInitializer.FrameworkInstanceCreationExpression;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand All @@ -39,13 +38,9 @@
*/
final class ProductionBindingRepresentation implements BindingRepresentation {
private final ProductionBinding binding;
private final ComponentImplementation componentImplementation;
private final DerivedFromFrameworkInstanceRequestRepresentation.Factory
derivedFromFrameworkInstanceRequestRepresentationFactory;
private final ProducerNodeInstanceRequestRepresentation.Factory
producerNodeInstanceRequestRepresentationFactory;
private final UnscopedFrameworkInstanceCreationExpressionFactory
unscopedFrameworkInstanceCreationExpressionFactory;
private final RequestRepresentation frameworkInstanceRequestRepresentation;
private final Map<BindingRequest, RequestRepresentation> requestRepresentations = new HashMap<>();

@AssistedInject
Expand All @@ -60,13 +55,21 @@ final class ProductionBindingRepresentation implements BindingRepresentation {
unscopedFrameworkInstanceCreationExpressionFactory,
DaggerTypes types) {
this.binding = binding;
this.componentImplementation = componentImplementation;
this.derivedFromFrameworkInstanceRequestRepresentationFactory =
derivedFromFrameworkInstanceRequestRepresentationFactory;
this.producerNodeInstanceRequestRepresentationFactory =
producerNodeInstanceRequestRepresentationFactory;
this.unscopedFrameworkInstanceCreationExpressionFactory =
unscopedFrameworkInstanceCreationExpressionFactory;
Optional<MemberSelect> staticMethod = staticFactoryCreation();
FrameworkInstanceSupplier frameworkInstanceSupplier =
staticMethod.isPresent()
? staticMethod::get
: new FrameworkFieldInitializer(
componentImplementation,
binding,
binding.scope().isPresent()
? scope(
binding, unscopedFrameworkInstanceCreationExpressionFactory.create(binding))
: unscopedFrameworkInstanceCreationExpressionFactory.create(binding));
this.frameworkInstanceRequestRepresentation =
producerNodeInstanceRequestRepresentationFactory.create(binding, frameworkInstanceSupplier);
}

@Override
Expand All @@ -77,32 +80,12 @@ public RequestRepresentation getRequestRepresentation(BindingRequest request) {

private RequestRepresentation getRequestRepresentationUncached(BindingRequest request) {
return request.frameworkType().isPresent()
? frameworkInstanceRequestRepresentation()
? frameworkInstanceRequestRepresentation
: derivedFromFrameworkInstanceRequestRepresentationFactory.create(
request, FrameworkType.PRODUCER_NODE);
}

/**
* Returns a binding expression that uses a {@link dagger.producers.Producer} for production
* bindings.
*/
private RequestRepresentation frameworkInstanceRequestRepresentation() {
Optional<MemberSelect> staticMethod = staticFactoryCreation();
if (staticMethod.isPresent()) {
return producerNodeInstanceRequestRepresentationFactory.create(binding, staticMethod::get);
}
FrameworkInstanceCreationExpression frameworkInstanceCreationExpression =
unscopedFrameworkInstanceCreationExpressionFactory.create(binding);
return producerNodeInstanceRequestRepresentationFactory.create(
binding,
new FrameworkFieldInitializer(
componentImplementation,
binding,
binding.scope().isPresent()
? scope(binding, frameworkInstanceCreationExpression)
: frameworkInstanceCreationExpression));
frameworkInstanceRequestRepresentation,
request.requestKind(),
FrameworkType.PRODUCER_NODE);
}

/**
* If {@code resolvedBindings} is an unscoped provision binding with no factory arguments, then we
* don't need a field to hold its factory. In that case, this method returns the static member
Expand Down

0 comments on commit 756922b

Please sign in to comment.