• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 static com.google.common.base.Preconditions.checkNotNull;
20 
21 import androidx.room.compiler.processing.XProcessingEnv;
22 import com.squareup.javapoet.CodeBlock;
23 import dagger.assisted.Assisted;
24 import dagger.assisted.AssistedFactory;
25 import dagger.assisted.AssistedInject;
26 import dagger.internal.codegen.binding.ComponentDescriptor.ComponentMethodDescriptor;
27 import dagger.internal.codegen.javapoet.Expression;
28 import dagger.internal.codegen.javapoet.ExpressionType;
29 
30 /**
31  * A binding expression that implements and uses a component method.
32  *
33  * <p>Dependents of this binding expression will just call the component method.
34  */
35 final class ComponentMethodRequestRepresentation extends MethodRequestRepresentation {
36   private final RequestRepresentation wrappedRequestRepresentation;
37   private final ComponentImplementation componentImplementation;
38   private final ComponentMethodDescriptor componentMethod;
39 
40   @AssistedInject
ComponentMethodRequestRepresentation( @ssisted RequestRepresentation wrappedRequestRepresentation, @Assisted ComponentMethodDescriptor componentMethod, ComponentImplementation componentImplementation, XProcessingEnv processingEnv)41   ComponentMethodRequestRepresentation(
42       @Assisted RequestRepresentation wrappedRequestRepresentation,
43       @Assisted ComponentMethodDescriptor componentMethod,
44       ComponentImplementation componentImplementation,
45       XProcessingEnv processingEnv) {
46     super(componentImplementation.getComponentShard(), processingEnv);
47     this.wrappedRequestRepresentation = checkNotNull(wrappedRequestRepresentation);
48     this.componentMethod = checkNotNull(componentMethod);
49     this.componentImplementation = componentImplementation;
50   }
51 
52   @Override
getDependencyExpressionForComponentMethod( ComponentMethodDescriptor componentMethod, ComponentImplementation component)53   protected Expression getDependencyExpressionForComponentMethod(
54       ComponentMethodDescriptor componentMethod, ComponentImplementation component) {
55     // There could be several methods on the component for the same request key and kind.
56     // Only one should use the BindingMethodImplementation; the others can delegate that one.
57     // Separately, the method might be defined on a supertype that is also a supertype of some
58     // parent component. In that case, the same ComponentMethodDescriptor will be used to add a CMBE
59     // for the parent and the child. Only the parent's should use the BindingMethodImplementation;
60     // the child's can delegate to the parent. So use methodImplementation.body() only if
61     // componentName equals the component for this instance.
62     return componentMethod.equals(this.componentMethod) && component.equals(componentImplementation)
63         ? wrappedRequestRepresentation.getDependencyExpressionForComponentMethod(
64             componentMethod, componentImplementation)
65         : super.getDependencyExpressionForComponentMethod(componentMethod, component);
66   }
67 
68   @Override
methodCall()69   protected CodeBlock methodCall() {
70     return CodeBlock.of("$N()", componentMethod.methodElement().getJvmName());
71   }
72 
73   @Override
returnType()74   protected ExpressionType returnType() {
75     return ExpressionType.create(componentMethod.methodElement().getReturnType());
76   }
77 
78   @AssistedFactory
79   static interface Factory {
create( RequestRepresentation wrappedRequestRepresentation, ComponentMethodDescriptor componentMethod)80     ComponentMethodRequestRepresentation create(
81         RequestRepresentation wrappedRequestRepresentation,
82         ComponentMethodDescriptor componentMethod);
83   }
84 }
85