• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.writing;
18 
19 import androidx.room.compiler.processing.XProcessingEnv;
20 import com.squareup.javapoet.ClassName;
21 import com.squareup.javapoet.CodeBlock;
22 import dagger.assisted.Assisted;
23 import dagger.assisted.AssistedFactory;
24 import dagger.assisted.AssistedInject;
25 import dagger.internal.codegen.binding.ComponentDescriptor.ComponentMethodDescriptor;
26 import dagger.internal.codegen.binding.ContributionBinding;
27 import dagger.internal.codegen.binding.FrameworkType;
28 import dagger.internal.codegen.javapoet.Expression;
29 import dagger.internal.codegen.javapoet.TypeNames;
30 import dagger.internal.codegen.model.Key;
31 import dagger.internal.codegen.writing.ComponentImplementation.ShardImplementation;
32 
33 /** Binding expression for producer node instances. */
34 final class ProducerNodeInstanceRequestRepresentation
35     extends FrameworkInstanceRequestRepresentation {
36   private final ShardImplementation shardImplementation;
37   private final Key key;
38   private final ProducerEntryPointView producerEntryPointView;
39 
40   @AssistedInject
ProducerNodeInstanceRequestRepresentation( @ssisted ContributionBinding binding, @Assisted FrameworkInstanceSupplier frameworkInstanceSupplier, XProcessingEnv processingEnv, ComponentImplementation componentImplementation)41   ProducerNodeInstanceRequestRepresentation(
42       @Assisted ContributionBinding binding,
43       @Assisted FrameworkInstanceSupplier frameworkInstanceSupplier,
44       XProcessingEnv processingEnv,
45       ComponentImplementation componentImplementation) {
46     super(binding, frameworkInstanceSupplier, processingEnv);
47     this.shardImplementation = componentImplementation.shardImplementation(binding);
48     this.key = binding.key();
49     this.producerEntryPointView = new ProducerEntryPointView(shardImplementation, processingEnv);
50   }
51 
52   @Override
frameworkType()53   protected FrameworkType frameworkType() {
54     return FrameworkType.PRODUCER_NODE;
55   }
56 
57   @Override
getDependencyExpression(ClassName requestingClass)58   Expression getDependencyExpression(ClassName requestingClass) {
59     Expression result = super.getDependencyExpression(requestingClass);
60     shardImplementation.addCancellation(
61         key,
62         CodeBlock.of(
63             "$T.cancel($L, $N);",
64             TypeNames.PRODUCERS,
65             result.codeBlock(),
66             ComponentImplementation.MAY_INTERRUPT_IF_RUNNING_PARAM));
67     return result;
68   }
69 
70   @Override
getDependencyExpressionForComponentMethod( ComponentMethodDescriptor componentMethod, ComponentImplementation component)71   Expression getDependencyExpressionForComponentMethod(
72       ComponentMethodDescriptor componentMethod, ComponentImplementation component) {
73     return producerEntryPointView
74         .getProducerEntryPointField(this, componentMethod, component.name())
75         .orElseGet(
76             () -> super.getDependencyExpressionForComponentMethod(componentMethod, component));
77   }
78 
79   @AssistedFactory
80   static interface Factory {
create( ContributionBinding binding, FrameworkInstanceSupplier frameworkInstanceSupplier)81     ProducerNodeInstanceRequestRepresentation create(
82         ContributionBinding binding, FrameworkInstanceSupplier frameworkInstanceSupplier);
83   }
84 }
85