blob: b1d8aaae0983eec6050330a93910905cecaa86e0 [file] [log] [blame]
dpb02db2132018-01-08 07:20:23 -08001/*
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 dagger.internal.codegen.RequestKinds.requestType;
21
dpb02db2132018-01-08 07:20:23 -080022import com.squareup.javapoet.ClassName;
23import com.squareup.javapoet.CodeBlock;
dpb02db2132018-01-08 07:20:23 -080024import dagger.model.RequestKind;
dpb02db2132018-01-08 07:20:23 -080025import javax.lang.model.type.TypeMirror;
dpb02db2132018-01-08 07:20:23 -080026
27/** Defines a method body and return type for a given {@link BindingExpression}. */
28class BindingMethodImplementation {
dpb356a6842018-02-07 07:45:50 -080029 private final ContributionBinding binding;
30 private final RequestKind requestKind;
dpb02db2132018-01-08 07:20:23 -080031 private final BindingExpression bindingExpression;
32 private final ClassName componentName;
dpb02db2132018-01-08 07:20:23 -080033 private final DaggerTypes types;
dpb02db2132018-01-08 07:20:23 -080034
35 BindingMethodImplementation(
dpb356a6842018-02-07 07:45:50 -080036 ResolvedBindings resolvedBindings,
37 RequestKind requestKind,
dpb02db2132018-01-08 07:20:23 -080038 BindingExpression bindingExpression,
39 ClassName componentName,
dpb356a6842018-02-07 07:45:50 -080040 DaggerTypes types) {
41 this.binding = resolvedBindings.contributionBinding();
42 this.requestKind = checkNotNull(requestKind);
dpb02db2132018-01-08 07:20:23 -080043 this.bindingExpression = checkNotNull(bindingExpression);
44 this.componentName = checkNotNull(componentName);
45 this.types = checkNotNull(types);
dpb02db2132018-01-08 07:20:23 -080046 }
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 */
dpb466064a2018-01-26 08:15:43 -080054 CodeBlock body() {
dpb356a6842018-02-07 07:45:50 -080055 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();
dpb02db2132018-01-08 07:20:23 -080061 }
62
63 /** Returns the return type for the dependency request. */
64 final TypeMirror returnType() {
dpb02db2132018-01-08 07:20:23 -080065 if (requestKind.equals(RequestKind.INSTANCE)
66 && binding.contributedPrimitiveType().isPresent()) {
67 return binding.contributedPrimitiveType().get();
68 }
dpb356a6842018-02-07 07:45:50 -080069 return types.accessibleType(
70 requestType(requestKind, binding.contributedType(), types), componentName);
dpb02db2132018-01-08 07:20:23 -080071 }
72}