| 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 | import static dagger.internal.codegen.RequestKinds.requestType; |
| 21 | |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 22 | import com.squareup.javapoet.ClassName; |
| 23 | import com.squareup.javapoet.CodeBlock; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 24 | import dagger.model.RequestKind; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 25 | import javax.lang.model.type.TypeMirror; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 26 | |
| 27 | /** Defines a method body and return type for a given {@link BindingExpression}. */ |
| 28 | class BindingMethodImplementation { |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame^] | 29 | private final ContributionBinding binding; |
| 30 | private final RequestKind requestKind; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 31 | private final BindingExpression bindingExpression; |
| 32 | private final ClassName componentName; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 33 | private final DaggerTypes types; |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 34 | |
| 35 | BindingMethodImplementation( |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame^] | 36 | ResolvedBindings resolvedBindings, |
| 37 | RequestKind requestKind, |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 38 | BindingExpression bindingExpression, |
| 39 | ClassName componentName, |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame^] | 40 | DaggerTypes types) { |
| 41 | this.binding = resolvedBindings.contributionBinding(); |
| 42 | this.requestKind = checkNotNull(requestKind); |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 43 | this.bindingExpression = checkNotNull(bindingExpression); |
| 44 | this.componentName = checkNotNull(componentName); |
| 45 | this.types = checkNotNull(types); |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Returns the method body, which contains zero or more statements (including semicolons). |
| 50 | * |
| 51 | * <p>If the implementation has a non-void return type, the body will also include the {@code |
| 52 | * return} statement. |
| 53 | */ |
| dpb | 466064a | 2018-01-26 08:15:43 -0800 | [diff] [blame] | 54 | CodeBlock body() { |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame^] | 55 | return CodeBlock.of("return $L;", simpleBindingExpression()); |
| 56 | } |
| 57 | |
| 58 | /** Returns the code for the binding expression. */ |
| 59 | protected final CodeBlock simpleBindingExpression() { |
| 60 | return bindingExpression.getDependencyExpression(componentName).codeBlock(); |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** Returns the return type for the dependency request. */ |
| 64 | final TypeMirror returnType() { |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 65 | if (requestKind.equals(RequestKind.INSTANCE) |
| 66 | && binding.contributedPrimitiveType().isPresent()) { |
| 67 | return binding.contributedPrimitiveType().get(); |
| 68 | } |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame^] | 69 | return types.accessibleType( |
| 70 | requestType(requestKind, binding.contributedType(), types), componentName); |
| dpb | 02db213 | 2018-01-08 07:20:23 -0800 | [diff] [blame] | 71 | } |
| 72 | } |