| 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; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 20 | |
| ronshapiro | 8d759b2 | 2018-11-05 09:57:39 -0800 | [diff] [blame] | 21 | import com.google.common.base.Supplier; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 22 | import com.squareup.javapoet.CodeBlock; |
| ronshapiro | 8d759b2 | 2018-11-05 09:57:39 -0800 | [diff] [blame] | 23 | import dagger.internal.codegen.ComponentDescriptor.ComponentMethodDescriptor; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 24 | import dagger.model.RequestKind; |
| ronshapiro | 66dd69f | 2018-11-27 08:49:26 -0800 | [diff] [blame] | 25 | import java.util.Optional; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 26 | import javax.lang.model.type.TypeMirror; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 27 | |
| 28 | /** Defines a method body and return type for a given {@link BindingExpression}. */ |
| 29 | class BindingMethodImplementation { |
| ronshapiro | 66dd69f | 2018-11-27 08:49:26 -0800 | [diff] [blame] | 30 | private final ComponentImplementation component; |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 31 | private final ContributionBinding binding; |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 32 | private final BindingRequest request; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 33 | private final BindingExpression bindingExpression; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 34 | private final DaggerTypes types; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 35 | |
| 36 | BindingMethodImplementation( |
| ronshapiro | 66dd69f | 2018-11-27 08:49:26 -0800 | [diff] [blame] | 37 | ComponentImplementation component, |
| 38 | ContributionBinding binding, |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 39 | BindingRequest request, |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 40 | BindingExpression bindingExpression, |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 41 | DaggerTypes types) { |
| ronshapiro | 66dd69f | 2018-11-27 08:49:26 -0800 | [diff] [blame] | 42 | this.component = component; |
| 43 | this.binding = binding; |
| 44 | this.request = request; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 45 | this.bindingExpression = checkNotNull(bindingExpression); |
| ronshapiro | 66dd69f | 2018-11-27 08:49:26 -0800 | [diff] [blame] | 46 | this.types = types; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| ronshapiro | 8d759b2 | 2018-11-05 09:57:39 -0800 | [diff] [blame] | 49 | /** The method's body. */ |
| 50 | final CodeBlock body() { |
| ronshapiro | 66dd69f | 2018-11-27 08:49:26 -0800 | [diff] [blame] | 51 | return implementation(bindingExpression.getDependencyExpression(component.name())::codeBlock); |
| ronshapiro | 8d759b2 | 2018-11-05 09:57:39 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** The method's body if this method is a component method. */ |
| ronshapiro | 66dd69f | 2018-11-27 08:49:26 -0800 | [diff] [blame] | 55 | final CodeBlock bodyForComponentMethod(ComponentMethodDescriptor componentMethod) { |
| ronshapiro | 8d759b2 | 2018-11-05 09:57:39 -0800 | [diff] [blame] | 56 | return implementation( |
| ronshapiro | 66dd69f | 2018-11-27 08:49:26 -0800 | [diff] [blame] | 57 | bindingExpression.getDependencyExpressionForComponentMethod(componentMethod, component) |
| 58 | ::codeBlock); |
| ronshapiro | 8d759b2 | 2018-11-05 09:57:39 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 61 | /** |
| 62 | * Returns the method body, which contains zero or more statements (including semicolons). |
| 63 | * |
| 64 | * <p>If the implementation has a non-void return type, the body will also include the {@code |
| 65 | * return} statement. |
| ronshapiro | 8d759b2 | 2018-11-05 09:57:39 -0800 | [diff] [blame] | 66 | * |
| 67 | * @param simpleBindingExpression the expression to retrieve an instance of this binding without |
| 68 | * the wrapping method. |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 69 | */ |
| ronshapiro | 8d759b2 | 2018-11-05 09:57:39 -0800 | [diff] [blame] | 70 | CodeBlock implementation(Supplier<CodeBlock> simpleBindingExpression) { |
| 71 | return CodeBlock.of("return $L;", simpleBindingExpression.get()); |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** Returns the return type for the dependency request. */ |
| 75 | final TypeMirror returnType() { |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 76 | if (request.isRequestKind(RequestKind.INSTANCE) |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 77 | && binding.contributedPrimitiveType().isPresent()) { |
| 78 | return binding.contributedPrimitiveType().get(); |
| 79 | } |
| ronshapiro | 66dd69f | 2018-11-27 08:49:26 -0800 | [diff] [blame] | 80 | |
| ronshapiro | 66dd69f | 2018-11-27 08:49:26 -0800 | [diff] [blame] | 81 | if (matchingComponentMethod().isPresent()) { |
| 82 | // Component methods are part of the user-defined API, and thus we must use the user-defined |
| ronshapiro | aeec8dc | 2018-12-03 13:59:03 -0800 | [diff] [blame] | 83 | // type. |
| 84 | return matchingComponentMethod().get().resolvedReturnType(types); |
| ronshapiro | 66dd69f | 2018-11-27 08:49:26 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // If the component is abstract, this method may be overridden by another implementation in a |
| 88 | // different package for which requestedType is inaccessible. In order to make that method |
| 89 | // overridable, we use the publicly accessible type. If the type is final, we don't need to |
| 90 | // worry about this, and instead just need to check accessibility of the file we're about to |
| 91 | // write |
| ronshapiro | aeec8dc | 2018-12-03 13:59:03 -0800 | [diff] [blame] | 92 | TypeMirror requestedType = request.requestedType(binding.contributedType(), types); |
| ronshapiro | 66dd69f | 2018-11-27 08:49:26 -0800 | [diff] [blame] | 93 | return component.isAbstract() |
| 94 | ? types.publiclyAccessibleType(requestedType) |
| 95 | : types.accessibleType(requestedType, component.name()); |
| 96 | } |
| 97 | |
| 98 | private Optional<ComponentMethodDescriptor> matchingComponentMethod() { |
| 99 | return component.componentDescriptor().firstMatchingComponentMethod(request); |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 100 | } |
| 101 | } |