blob: 32f053cf3939a6acf89fd83fd067f71d3c57960c [file] [log] [blame]
dpb68c77d82017-08-15 15:23:35 -07001/*
2 * Copyright (C) 2016 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 dagger.internal.codegen.Accessibility.isRawTypeAccessible;
20import static dagger.internal.codegen.Accessibility.isTypeAccessibleFrom;
21import static dagger.internal.codegen.TypeNames.rawTypeName;
22
23import com.google.common.collect.FluentIterable;
24import com.google.common.collect.ImmutableList;
25import com.squareup.javapoet.ClassName;
26import com.squareup.javapoet.CodeBlock;
27import com.squareup.javapoet.TypeName;
28import java.util.HashMap;
29import java.util.Map;
30import javax.lang.model.type.TypeMirror;
31
32/** A factory of code expressions used to access any binding available to a component. */
33final class ComponentBindingExpressions {
34
35 // TODO(dpb): Can this use a flattened ImmutableMap, built from its parents? Maybe make
36 // BindingExpression.Factory create it.
37
38 /**
39 * A list of binding expression maps. The first element contains the bindings owned by this
40 * component; the second contains the bindings owned by its parent; and so on.
41 */
42 private final ImmutableList<Map<BindingKey, BindingExpression>> bindingExpressionsMaps;
43
44 private ComponentBindingExpressions(
45 ImmutableList<Map<BindingKey, BindingExpression>> bindingExpressionsMaps) {
46 this.bindingExpressionsMaps = bindingExpressionsMaps;
47 }
48
49 ComponentBindingExpressions() {
50 this(ImmutableList.of(newBindingExpressionMap()));
51 }
52
53 /**
54 * Returns an expression that evaluates to the value of a dependency request for a binding owned
55 * by this component or an ancestor.
56 *
57 * @param requestingClass the class that will contain the expression
58 * @throws IllegalStateException if there is no binding expression that satisfies the dependency
59 * request
60 */
61 CodeBlock getDependencyExpression(DependencyRequest request, ClassName requestingClass) {
62 return getBindingExpression(request.bindingKey())
63 .getDependencyExpression(request.kind(), requestingClass);
64 }
65
66 /**
67 * Returns an expression that evaluates to the value of a framework dependency for a binding owned
68 * in this component or an ancestor.
69 *
70 * @param requestingClass the class that will contain the expression
71 * @throws IllegalStateException if there is no binding expression that satisfies the dependency
72 * request
73 */
74 CodeBlock getDependencyExpression(
75 FrameworkDependency frameworkDependency, ClassName requestingClass) {
76 return getBindingExpression(frameworkDependency.bindingKey())
77 .getDependencyExpression(frameworkDependency.dependencyRequestKind(), requestingClass);
78 }
79
80 /**
81 * Returns an expression that evaluates to the value of a dependency request, for passing to a
82 * binding method, an {@code @Inject}-annotated constructor or member, or a proxy for one.
83 *
84 * <p>If the method is a generated static {@link InjectionMethods injection method}, each
85 * parameter will be {@link Object} if the dependency's raw type is inaccessible. If that is the
86 * case for this dependency, the returned expression will use a cast to evaluate to the raw type.
87 *
88 * @param requestingClass the class that will contain the expression
89 */
90 // TODO(b/64024402) Merge with getDependencyExpression(DependencyRequest, ClassName) if possible.
91 CodeBlock getDependencyArgumentExpression(
92 DependencyRequest dependencyRequest, ClassName requestingClass) {
93 CodeBlock.Builder argument = CodeBlock.builder();
94
95 TypeMirror dependencyType = dependencyRequest.key().type();
96 if (!isTypeAccessibleFrom(dependencyType, requestingClass.packageName())
97 && isRawTypeAccessible(dependencyType, requestingClass.packageName())) {
98 argument.add("($T) ", rawTypeName(TypeName.get(dependencyType)));
99 }
100
101 argument.add(getDependencyExpression(dependencyRequest, requestingClass));
102 return argument.build();
103 }
104
105 private BindingExpression getBindingExpression(BindingKey bindingKey) {
106 for (Map<BindingKey, BindingExpression> bindingExpressionsMap : bindingExpressionsMaps) {
107 BindingExpression expression = bindingExpressionsMap.get(bindingKey);
108 if (expression != null) {
109 return expression;
110 }
111 }
112 throw new IllegalStateException("no binding expression found for " + bindingKey);
113 }
114
115 /** Adds a binding expression for a single binding owned by this component. */
116 void addBindingExpression(BindingExpression bindingExpression) {
117 bindingExpressionsMaps.get(0).put(bindingExpression.bindingKey(), bindingExpression);
118 }
119
120 /**
121 * Returns a new object representing the bindings available from a child component of this one.
122 */
123 ComponentBindingExpressions forChildComponent() {
124 return new ComponentBindingExpressions(
125 FluentIterable.of(newBindingExpressionMap()).append(bindingExpressionsMaps).toList());
126 }
127
128 private static Map<BindingKey, BindingExpression> newBindingExpressionMap() {
129 return new HashMap<>();
130 }
131}