• 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.internal.codegen.binding.ComponentDescriptor.ComponentMethodDescriptor;
23 import dagger.internal.codegen.javapoet.Expression;
24 import dagger.internal.codegen.javapoet.ExpressionType;
25 import dagger.internal.codegen.writing.ComponentImplementation.ShardImplementation;
26 
27 /** A binding expression that wraps another in a nullary method on the component. */
28 abstract class MethodRequestRepresentation extends RequestRepresentation {
29   private final ShardImplementation shardImplementation;
30   private final ProducerEntryPointView producerEntryPointView;
31 
MethodRequestRepresentation( ShardImplementation shardImplementation, XProcessingEnv processingEnv)32   protected MethodRequestRepresentation(
33       ShardImplementation shardImplementation, XProcessingEnv processingEnv) {
34     this.shardImplementation = shardImplementation;
35     this.producerEntryPointView = new ProducerEntryPointView(shardImplementation, processingEnv);
36   }
37 
38   @Override
getDependencyExpression(ClassName requestingClass)39   Expression getDependencyExpression(ClassName requestingClass) {
40     return Expression.create(
41         returnType(),
42         requestingClass.equals(shardImplementation.name())
43             ? methodCall()
44             : CodeBlock.of("$L.$L", shardImplementation.shardFieldReference(), methodCall()));
45   }
46 
47   @Override
getDependencyExpressionForComponentMethod( ComponentMethodDescriptor componentMethod, ComponentImplementation component)48   Expression getDependencyExpressionForComponentMethod(
49       ComponentMethodDescriptor componentMethod, ComponentImplementation component) {
50     return producerEntryPointView
51         .getProducerEntryPointField(this, componentMethod, component.name())
52         .orElseGet(
53             () -> super.getDependencyExpressionForComponentMethod(componentMethod, component));
54   }
55 
56   /** Returns the return type for the dependency request. */
returnType()57   protected abstract ExpressionType returnType();
58 
59   /** Returns the method call. */
methodCall()60   protected abstract CodeBlock methodCall();
61 }
62