• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.PROTECTED;
24 
25 import com.squareup.javapoet.TypeName;
26 import dagger.internal.codegen.langmodel.DaggerTypes;
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 
36   private final BindingRequest request;
37   private final ModifiableBindingType modifiableBindingType;
38   private final ComponentImplementation componentImplementation;
39   private final boolean bindingCannotBeModified;
40   private Optional<String> methodName = Optional.empty();
41 
ModifiableConcreteMethodBindingExpression( BindingRequest request, ResolvedBindings resolvedBindings, MethodImplementationStrategy methodImplementationStrategy, BindingExpression wrappedBindingExpression, ModifiableBindingType modifiableBindingType, ComponentImplementation componentImplementation, boolean bindingCannotBeModified, DaggerTypes types)42   ModifiableConcreteMethodBindingExpression(
43       BindingRequest request,
44       ResolvedBindings resolvedBindings,
45       MethodImplementationStrategy methodImplementationStrategy,
46       BindingExpression wrappedBindingExpression,
47       ModifiableBindingType modifiableBindingType,
48       ComponentImplementation componentImplementation,
49       boolean bindingCannotBeModified,
50       DaggerTypes types) {
51     super(
52         request,
53         resolvedBindings,
54         methodImplementationStrategy,
55         wrappedBindingExpression,
56         componentImplementation,
57         types);
58     this.request = checkNotNull(request);
59     this.modifiableBindingType = checkNotNull(modifiableBindingType);
60     this.componentImplementation = checkNotNull(componentImplementation);
61     this.bindingCannotBeModified = bindingCannotBeModified;
62   }
63 
64   @Override
addMethod()65   protected void addMethod() {
66     if (methodName.isPresent()) {
67       return;
68     }
69 
70     if (supertypeModifiableBindingMethod().isPresent()) {
71       methodName = supertypeModifiableBindingMethod().map(method -> method.methodSpec().name);
72       return;
73     }
74 
75     // Add the modifiable binding method to the component if we haven't already.
76     methodName = Optional.of(componentImplementation.getUniqueMethodName(request));
77     componentImplementation.addModifiableBindingMethod(
78         modifiableBindingType,
79         request,
80         returnType(),
81         methodBuilder(methodName.get())
82             .addModifiers(bindingCannotBeModified ? PRIVATE : PROTECTED)
83             .returns(TypeName.get(returnType()))
84             .addCode(methodBody())
85             .build(),
86         bindingCannotBeModified);
87   }
88 
89   @Override
methodName()90   protected String methodName() {
91     checkState(methodName.isPresent(), "addMethod() must be called before methodName().");
92     return methodName.get();
93   }
94 
95   @Override
isModifiableImplementationMethod()96   protected boolean isModifiableImplementationMethod() {
97     return true;
98   }
99 }
100