| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [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 com.google.common.base.Preconditions.checkState; |
| 21 | import static com.squareup.javapoet.MethodSpec.methodBuilder; |
| 22 | import static javax.lang.model.element.Modifier.PRIVATE; |
| 23 | import static javax.lang.model.element.Modifier.PUBLIC; |
| 24 | |
| 25 | import com.squareup.javapoet.TypeName; |
| 26 | import dagger.internal.codegen.ModifiableBindingMethods.ModifiableBindingMethod; |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 27 | import java.util.Optional; |
| 28 | |
| 29 | /** |
| 30 | * A binding expression that wraps a modifiable binding expression in a public, no-arg method. |
| 31 | * |
| 32 | * <p>Dependents of this binding expression will just call the modifiable binding method. |
| 33 | */ |
| 34 | final class ModifiableConcreteMethodBindingExpression extends MethodBindingExpression { |
| 35 | private final ContributionBinding binding; |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 36 | private final BindingRequest request; |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 37 | private final ModifiableBindingType modifiableBindingType; |
| 38 | private final BindingMethodImplementation methodImplementation; |
| 39 | private final GeneratedComponentModel generatedComponentModel; |
| 40 | private final boolean bindingFinalized; |
| 41 | private Optional<String> methodName; |
| 42 | |
| 43 | ModifiableConcreteMethodBindingExpression( |
| 44 | ResolvedBindings resolvedBindings, |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 45 | BindingRequest request, |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 46 | ModifiableBindingType modifiableBindingType, |
| 47 | BindingMethodImplementation methodImplementation, |
| 48 | GeneratedComponentModel generatedComponentModel, |
| 49 | Optional<ModifiableBindingMethod> matchingModifiableBindingMethod, |
| 50 | boolean bindingFinalized) { |
| dstrasburg | 9203183 | 2018-10-17 08:48:40 -0700 | [diff] [blame] | 51 | super(methodImplementation, generatedComponentModel, matchingModifiableBindingMethod); |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 52 | this.binding = resolvedBindings.contributionBinding(); |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 53 | this.request = checkNotNull(request); |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 54 | this.modifiableBindingType = checkNotNull(modifiableBindingType); |
| 55 | this.methodImplementation = checkNotNull(methodImplementation); |
| 56 | this.generatedComponentModel = checkNotNull(generatedComponentModel); |
| 57 | this.bindingFinalized = bindingFinalized; |
| 58 | this.methodName = |
| 59 | matchingModifiableBindingMethod.map(modifiableMethod -> modifiableMethod.methodSpec().name); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | protected void addMethod() { |
| 64 | // Add the modifiable binding method to the component model if we haven't already. |
| 65 | if (!methodName.isPresent()) { |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 66 | methodName = Optional.of(generatedComponentModel.getUniqueMethodName(request, binding)); |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 67 | generatedComponentModel.addModifiableBindingMethod( |
| 68 | modifiableBindingType, |
| cgdecker | 60a5dde | 2018-09-07 08:44:34 -0700 | [diff] [blame] | 69 | request, |
| dstrasburg | 6d2a7ad | 2018-09-05 08:05:51 -0700 | [diff] [blame] | 70 | methodBuilder(methodName.get()) |
| 71 | .addModifiers(bindingFinalized ? PRIVATE : PUBLIC) |
| 72 | .returns(TypeName.get(methodImplementation.returnType())) |
| 73 | .addCode(methodImplementation.body()) |
| 74 | .build(), |
| 75 | bindingFinalized); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected String methodName() { |
| 81 | checkState(methodName.isPresent(), "addMethod() must be called before methodName()."); |
| 82 | return methodName.get(); |
| 83 | } |
| 84 | } |