blob: 7bb9312f7f2f7b56555d753410cae4f89561bbe9 [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;
dpb68c77d82017-08-15 15:23:35 -070021
22import com.google.common.collect.FluentIterable;
23import com.google.common.collect.ImmutableList;
24import com.squareup.javapoet.ClassName;
dpb68c77d82017-08-15 15:23:35 -070025import java.util.HashMap;
26import java.util.Map;
27import javax.lang.model.type.TypeMirror;
ronshapiroba794862017-09-28 11:39:34 -070028import javax.lang.model.util.Types;
dpb68c77d82017-08-15 15:23:35 -070029
ronshapirodc07ed52017-08-23 08:52:10 -070030/** A central repository of code expressions used to access any binding available to a component. */
dpb68c77d82017-08-15 15:23:35 -070031final class ComponentBindingExpressions {
32
ronshapirodc07ed52017-08-23 08:52:10 -070033 // TODO(dpb,ronshapiro): refactor this and ComponentRequirementFields into a
34 // HierarchicalComponentMap<K, V>, or perhaps this use a flattened ImmutableMap, built from its
35 // parents? If so, maybe make BindingExpression.Factory create it.
dpb68c77d82017-08-15 15:23:35 -070036
37 /**
38 * A list of binding expression maps. The first element contains the bindings owned by this
39 * component; the second contains the bindings owned by its parent; and so on.
40 */
41 private final ImmutableList<Map<BindingKey, BindingExpression>> bindingExpressionsMaps;
ronshapiroba794862017-09-28 11:39:34 -070042 private final Types types;
dpb68c77d82017-08-15 15:23:35 -070043
44 private ComponentBindingExpressions(
ronshapiroba794862017-09-28 11:39:34 -070045 ImmutableList<Map<BindingKey, BindingExpression>> bindingExpressionsMaps, Types types) {
dpb68c77d82017-08-15 15:23:35 -070046 this.bindingExpressionsMaps = bindingExpressionsMaps;
ronshapiroba794862017-09-28 11:39:34 -070047 this.types = types;
dpb68c77d82017-08-15 15:23:35 -070048 }
49
ronshapiroba794862017-09-28 11:39:34 -070050 ComponentBindingExpressions(Types types) {
51 this(ImmutableList.of(newBindingExpressionMap()), types);
dpb68c77d82017-08-15 15:23:35 -070052 }
53
54 /**
55 * Returns an expression that evaluates to the value of a dependency request for a binding owned
56 * by this component or an ancestor.
57 *
58 * @param requestingClass the class that will contain the expression
59 * @throws IllegalStateException if there is no binding expression that satisfies the dependency
60 * request
61 */
ronshapiroba794862017-09-28 11:39:34 -070062 Expression getDependencyExpression(
63 BindingKey bindingKey, DependencyRequest.Kind requestKind, ClassName requestingClass) {
64 return getBindingExpression(bindingKey).getDependencyExpression(requestKind, requestingClass);
65 }
66
67 /**
68 * Returns an expression that evaluates to the value of a dependency request for a binding owned
69 * by this component or an ancestor.
70 *
71 * @param requestingClass the class that will contain the expression
72 * @throws IllegalStateException if there is no binding expression that satisfies the dependency
73 * request
74 */
75 Expression getDependencyExpression(DependencyRequest request, ClassName requestingClass) {
76 return getDependencyExpression(request.bindingKey(), request.kind(), requestingClass);
dpb68c77d82017-08-15 15:23:35 -070077 }
78
79 /**
80 * Returns an expression that evaluates to the value of a framework dependency for a binding owned
81 * in this component or an ancestor.
82 *
83 * @param requestingClass the class that will contain the expression
84 * @throws IllegalStateException if there is no binding expression that satisfies the dependency
85 * request
86 */
ronshapiroba794862017-09-28 11:39:34 -070087 Expression getDependencyExpression(
dpb68c77d82017-08-15 15:23:35 -070088 FrameworkDependency frameworkDependency, ClassName requestingClass) {
ronshapiroba794862017-09-28 11:39:34 -070089 return getDependencyExpression(
90 frameworkDependency.bindingKey(),
91 frameworkDependency.dependencyRequestKind(),
92 requestingClass);
dpb68c77d82017-08-15 15:23:35 -070093 }
94
95 /**
96 * Returns an expression that evaluates to the value of a dependency request, for passing to a
97 * binding method, an {@code @Inject}-annotated constructor or member, or a proxy for one.
98 *
99 * <p>If the method is a generated static {@link InjectionMethods injection method}, each
100 * parameter will be {@link Object} if the dependency's raw type is inaccessible. If that is the
101 * case for this dependency, the returned expression will use a cast to evaluate to the raw type.
102 *
103 * @param requestingClass the class that will contain the expression
104 */
105 // TODO(b/64024402) Merge with getDependencyExpression(DependencyRequest, ClassName) if possible.
ronshapiroba794862017-09-28 11:39:34 -0700106 Expression getDependencyArgumentExpression(
dpb68c77d82017-08-15 15:23:35 -0700107 DependencyRequest dependencyRequest, ClassName requestingClass) {
dpb68c77d82017-08-15 15:23:35 -0700108
109 TypeMirror dependencyType = dependencyRequest.key().type();
ronshapiroba794862017-09-28 11:39:34 -0700110 Expression dependencyExpression = getDependencyExpression(dependencyRequest, requestingClass);
111
dpb68c77d82017-08-15 15:23:35 -0700112 if (!isTypeAccessibleFrom(dependencyType, requestingClass.packageName())
113 && isRawTypeAccessible(dependencyType, requestingClass.packageName())) {
ronshapiroba794862017-09-28 11:39:34 -0700114 return dependencyExpression.castTo(types.erasure(dependencyType));
dpb68c77d82017-08-15 15:23:35 -0700115 }
116
ronshapiroba794862017-09-28 11:39:34 -0700117 return dependencyExpression;
dpb68c77d82017-08-15 15:23:35 -0700118 }
119
120 private BindingExpression getBindingExpression(BindingKey bindingKey) {
121 for (Map<BindingKey, BindingExpression> bindingExpressionsMap : bindingExpressionsMaps) {
122 BindingExpression expression = bindingExpressionsMap.get(bindingKey);
123 if (expression != null) {
124 return expression;
125 }
126 }
127 throw new IllegalStateException("no binding expression found for " + bindingKey);
128 }
129
130 /** Adds a binding expression for a single binding owned by this component. */
131 void addBindingExpression(BindingExpression bindingExpression) {
erichange6845ff2017-08-28 14:00:04 -0700132 bindingExpressionsMaps
133 .get(0)
134 .put(bindingExpression.resolvedBindings().bindingKey(), bindingExpression);
dpb68c77d82017-08-15 15:23:35 -0700135 }
136
137 /**
138 * Returns a new object representing the bindings available from a child component of this one.
139 */
140 ComponentBindingExpressions forChildComponent() {
141 return new ComponentBindingExpressions(
ronshapiroba794862017-09-28 11:39:34 -0700142 FluentIterable.of(newBindingExpressionMap()).append(bindingExpressionsMaps).toList(),
143 types);
dpb68c77d82017-08-15 15:23:35 -0700144 }
145
146 private static Map<BindingKey, BindingExpression> newBindingExpressionMap() {
147 return new HashMap<>();
148 }
149}