blob: 114ddbd377881aa1fddaf19c0d7e6fe4ed8211f0 [file] [log] [blame]
dstrasburg6d2a7ad2018-09-05 08:05:51 -07001/*
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;
20import static com.google.common.base.Preconditions.checkState;
21import static com.squareup.javapoet.MethodSpec.methodBuilder;
22import static javax.lang.model.element.Modifier.PRIVATE;
23import static javax.lang.model.element.Modifier.PUBLIC;
24
25import com.squareup.javapoet.TypeName;
26import dagger.internal.codegen.ModifiableBindingMethods.ModifiableBindingMethod;
dstrasburg6d2a7ad2018-09-05 08:05:51 -070027import 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 */
34final class ModifiableConcreteMethodBindingExpression extends MethodBindingExpression {
35 private final ContributionBinding binding;
cgdecker60a5dde2018-09-07 08:44:34 -070036 private final BindingRequest request;
dstrasburg6d2a7ad2018-09-05 08:05:51 -070037 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,
cgdecker60a5dde2018-09-07 08:44:34 -070045 BindingRequest request,
dstrasburg6d2a7ad2018-09-05 08:05:51 -070046 ModifiableBindingType modifiableBindingType,
47 BindingMethodImplementation methodImplementation,
48 GeneratedComponentModel generatedComponentModel,
49 Optional<ModifiableBindingMethod> matchingModifiableBindingMethod,
50 boolean bindingFinalized) {
dstrasburg92031832018-10-17 08:48:40 -070051 super(methodImplementation, generatedComponentModel, matchingModifiableBindingMethod);
dstrasburg6d2a7ad2018-09-05 08:05:51 -070052 this.binding = resolvedBindings.contributionBinding();
cgdecker60a5dde2018-09-07 08:44:34 -070053 this.request = checkNotNull(request);
dstrasburg6d2a7ad2018-09-05 08:05:51 -070054 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()) {
cgdecker60a5dde2018-09-07 08:44:34 -070066 methodName = Optional.of(generatedComponentModel.getUniqueMethodName(request, binding));
dstrasburg6d2a7ad2018-09-05 08:05:51 -070067 generatedComponentModel.addModifiableBindingMethod(
68 modifiableBindingType,
cgdecker60a5dde2018-09-07 08:44:34 -070069 request,
dstrasburg6d2a7ad2018-09-05 08:05:51 -070070 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}