| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package dagger.internal.codegen; |
| 18 | |
| 19 | import static dagger.internal.codegen.Accessibility.isRawTypeAccessible; |
| 20 | import static dagger.internal.codegen.Accessibility.isTypeAccessibleFrom; |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 21 | |
| 22 | import com.google.common.collect.FluentIterable; |
| 23 | import com.google.common.collect.ImmutableList; |
| 24 | import com.squareup.javapoet.ClassName; |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 25 | import java.util.HashMap; |
| 26 | import java.util.Map; |
| 27 | import javax.lang.model.type.TypeMirror; |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame^] | 28 | import javax.lang.model.util.Types; |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 29 | |
| ronshapiro | dc07ed5 | 2017-08-23 08:52:10 -0700 | [diff] [blame] | 30 | /** A central repository of code expressions used to access any binding available to a component. */ |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 31 | final class ComponentBindingExpressions { |
| 32 | |
| ronshapiro | dc07ed5 | 2017-08-23 08:52:10 -0700 | [diff] [blame] | 33 | // 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. |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 36 | |
| 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; |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame^] | 42 | private final Types types; |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 43 | |
| 44 | private ComponentBindingExpressions( |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame^] | 45 | ImmutableList<Map<BindingKey, BindingExpression>> bindingExpressionsMaps, Types types) { |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 46 | this.bindingExpressionsMaps = bindingExpressionsMaps; |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame^] | 47 | this.types = types; |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame^] | 50 | ComponentBindingExpressions(Types types) { |
| 51 | this(ImmutableList.of(newBindingExpressionMap()), types); |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 52 | } |
| 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 | */ |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame^] | 62 | 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); |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 77 | } |
| 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 | */ |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame^] | 87 | Expression getDependencyExpression( |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 88 | FrameworkDependency frameworkDependency, ClassName requestingClass) { |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame^] | 89 | return getDependencyExpression( |
| 90 | frameworkDependency.bindingKey(), |
| 91 | frameworkDependency.dependencyRequestKind(), |
| 92 | requestingClass); |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 93 | } |
| 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. |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame^] | 106 | Expression getDependencyArgumentExpression( |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 107 | DependencyRequest dependencyRequest, ClassName requestingClass) { |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 108 | |
| 109 | TypeMirror dependencyType = dependencyRequest.key().type(); |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame^] | 110 | Expression dependencyExpression = getDependencyExpression(dependencyRequest, requestingClass); |
| 111 | |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 112 | if (!isTypeAccessibleFrom(dependencyType, requestingClass.packageName()) |
| 113 | && isRawTypeAccessible(dependencyType, requestingClass.packageName())) { |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame^] | 114 | return dependencyExpression.castTo(types.erasure(dependencyType)); |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame^] | 117 | return dependencyExpression; |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 118 | } |
| 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) { |
| erichang | e6845ff | 2017-08-28 14:00:04 -0700 | [diff] [blame] | 132 | bindingExpressionsMaps |
| 133 | .get(0) |
| 134 | .put(bindingExpression.resolvedBindings().bindingKey(), bindingExpression); |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 135 | } |
| 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( |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame^] | 142 | FluentIterable.of(newBindingExpressionMap()).append(bindingExpressionsMaps).toList(), |
| 143 | types); |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | private static Map<BindingKey, BindingExpression> newBindingExpressionMap() { |
| 147 | return new HashMap<>(); |
| 148 | } |
| 149 | } |