blob: c678e2904fd42c22e314166a60660a581e3e859f [file] [log] [blame]
dpb02db2132018-01-08 07:20:23 -08001/*
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
17package dagger.internal.codegen;
18
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import com.squareup.javapoet.ClassName;
22import com.squareup.javapoet.CodeBlock;
dpb02db2132018-01-08 07:20:23 -080023import dagger.internal.codegen.ComponentDescriptor.ComponentMethodDescriptor;
dstrasburg92031832018-10-17 08:48:40 -070024import dagger.internal.codegen.ModifiableBindingMethods.ModifiableBindingMethod;
25import java.util.Optional;
bcorso5f0a1432018-04-16 08:51:58 -070026import javax.lang.model.type.TypeMirror;
dpb02db2132018-01-08 07:20:23 -080027
28/**
29 * A binding expression that implements and uses a component method.
30 *
31 * <p>Dependents of this binding expression will just call the component method.
32 */
dpbd7685762018-02-16 12:20:09 -080033final class ComponentMethodBindingExpression extends MethodBindingExpression {
dpb02db2132018-01-08 07:20:23 -080034 private final BindingMethodImplementation methodImplementation;
dpb466064a2018-01-26 08:15:43 -080035 private final GeneratedComponentModel generatedComponentModel;
dpb02db2132018-01-08 07:20:23 -080036 private final ComponentMethodDescriptor componentMethod;
dpb02db2132018-01-08 07:20:23 -080037
38 ComponentMethodBindingExpression(
dpb02db2132018-01-08 07:20:23 -080039 BindingMethodImplementation methodImplementation,
dpb466064a2018-01-26 08:15:43 -080040 GeneratedComponentModel generatedComponentModel,
dstrasburg92031832018-10-17 08:48:40 -070041 ComponentMethodDescriptor componentMethod,
42 Optional<ModifiableBindingMethod> matchingModifiableBindingMethod) {
43 super(methodImplementation, generatedComponentModel, matchingModifiableBindingMethod);
dpb02db2132018-01-08 07:20:23 -080044 this.methodImplementation = checkNotNull(methodImplementation);
dpb466064a2018-01-26 08:15:43 -080045 this.generatedComponentModel = checkNotNull(generatedComponentModel);
dpb02db2132018-01-08 07:20:23 -080046 this.componentMethod = checkNotNull(componentMethod);
dpb02db2132018-01-08 07:20:23 -080047 }
48
49 @Override
dpb356a6842018-02-07 07:45:50 -080050 protected CodeBlock getComponentMethodImplementation(
cgdecker68b12d42018-08-13 12:06:24 -070051 ComponentMethodDescriptor componentMethod, GeneratedComponentModel component) {
dpb466064a2018-01-26 08:15:43 -080052 // There could be several methods on the component for the same request key and kind.
53 // Only one should use the BindingMethodImplementation; the others can delegate that one. So
54 // use methodImplementation.body() only if componentMethod equals the method for this instance.
dpb356a6842018-02-07 07:45:50 -080055
dpb466064a2018-01-26 08:15:43 -080056 // Separately, the method might be defined on a supertype that is also a supertype of some
57 // parent component. In that case, the same ComponentMethodDescriptor will be used to add a CMBE
58 // for the parent and the child. Only the parent's should use the BindingMethodImplementation;
59 // the child's can delegate to the parent. So use methodImplementation.body() only if
60 // componentName equals the component for this instance.
cgdecker68b12d42018-08-13 12:06:24 -070061 return componentMethod.equals(this.componentMethod) && component.equals(generatedComponentModel)
dpb466064a2018-01-26 08:15:43 -080062 ? methodImplementation.body()
cgdecker68b12d42018-08-13 12:06:24 -070063 : super.getComponentMethodImplementation(componentMethod, component);
dpb02db2132018-01-08 07:20:23 -080064 }
65
66 @Override
bcorso5f0a1432018-04-16 08:51:58 -070067 Expression getDependencyExpression(ClassName requestingClass) {
68 // If a component method returns a primitive, update the expression's type which might be boxed.
69 Expression expression = super.getDependencyExpression(requestingClass);
70 TypeMirror methodReturnType = componentMethod.methodElement().getReturnType();
71 return methodReturnType.getKind().isPrimitive()
72 ? Expression.create(methodReturnType, expression.codeBlock())
73 : expression;
74 }
75
76 @Override
dpbd7685762018-02-16 12:20:09 -080077 protected void addMethod() {}
78
79 @Override
80 protected String methodName() {
81 return componentMethod.methodElement().getSimpleName().toString();
dpb02db2132018-01-08 07:20:23 -080082 }
83}