| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 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; |
| 18 | |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
| 21 | import com.squareup.javapoet.ClassName; |
| 22 | import com.squareup.javapoet.CodeBlock; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 23 | import dagger.internal.codegen.ComponentDescriptor.ComponentMethodDescriptor; |
| dstrasburg | 9203183 | 2018-10-17 08:48:40 -0700 | [diff] [blame] | 24 | import dagger.internal.codegen.ModifiableBindingMethods.ModifiableBindingMethod; |
| 25 | import java.util.Optional; |
| bcorso | 5f0a143 | 2018-04-16 08:51:58 -0700 | [diff] [blame] | 26 | import javax.lang.model.type.TypeMirror; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 27 | |
| 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 | */ |
| dpb | d768576 | 2018-02-16 12:20:09 -0800 | [diff] [blame] | 33 | final class ComponentMethodBindingExpression extends MethodBindingExpression { |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 34 | private final BindingMethodImplementation methodImplementation; |
| dpb | 466064a | 2018-01-26 08:15:43 -0800 | [diff] [blame] | 35 | private final GeneratedComponentModel generatedComponentModel; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 36 | private final ComponentMethodDescriptor componentMethod; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 37 | |
| 38 | ComponentMethodBindingExpression( |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 39 | BindingMethodImplementation methodImplementation, |
| dpb | 466064a | 2018-01-26 08:15:43 -0800 | [diff] [blame] | 40 | GeneratedComponentModel generatedComponentModel, |
| dstrasburg | 9203183 | 2018-10-17 08:48:40 -0700 | [diff] [blame] | 41 | ComponentMethodDescriptor componentMethod, |
| 42 | Optional<ModifiableBindingMethod> matchingModifiableBindingMethod) { |
| 43 | super(methodImplementation, generatedComponentModel, matchingModifiableBindingMethod); |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 44 | this.methodImplementation = checkNotNull(methodImplementation); |
| dpb | 466064a | 2018-01-26 08:15:43 -0800 | [diff] [blame] | 45 | this.generatedComponentModel = checkNotNull(generatedComponentModel); |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 46 | this.componentMethod = checkNotNull(componentMethod); |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Override |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 50 | protected CodeBlock getComponentMethodImplementation( |
| cgdecker | 68b12d4 | 2018-08-13 12:06:24 -0700 | [diff] [blame] | 51 | ComponentMethodDescriptor componentMethod, GeneratedComponentModel component) { |
| dpb | 466064a | 2018-01-26 08:15:43 -0800 | [diff] [blame] | 52 | // 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. |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 55 | |
| dpb | 466064a | 2018-01-26 08:15:43 -0800 | [diff] [blame] | 56 | // 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. |
| cgdecker | 68b12d4 | 2018-08-13 12:06:24 -0700 | [diff] [blame] | 61 | return componentMethod.equals(this.componentMethod) && component.equals(generatedComponentModel) |
| dpb | 466064a | 2018-01-26 08:15:43 -0800 | [diff] [blame] | 62 | ? methodImplementation.body() |
| cgdecker | 68b12d4 | 2018-08-13 12:06:24 -0700 | [diff] [blame] | 63 | : super.getComponentMethodImplementation(componentMethod, component); |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
| bcorso | 5f0a143 | 2018-04-16 08:51:58 -0700 | [diff] [blame] | 67 | 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 |
| dpb | d768576 | 2018-02-16 12:20:09 -0800 | [diff] [blame] | 77 | protected void addMethod() {} |
| 78 | |
| 79 | @Override |
| 80 | protected String methodName() { |
| 81 | return componentMethod.methodElement().getSimpleName().toString(); |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 82 | } |
| 83 | } |