|
| 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.collect.Iterables.getOnlyElement; |
| 21 | +import static dagger.internal.codegen.Accessibility.isTypeAccessibleFrom; |
| 22 | +import static dagger.internal.codegen.Scope.reusableScope; |
| 23 | + |
| 24 | +import com.squareup.javapoet.ClassName; |
| 25 | +import javax.lang.model.type.TypeMirror; |
| 26 | +import javax.lang.model.util.Elements; |
| 27 | + |
| 28 | +/** A {@link BindingExpression} for {@code @Binds} methods. */ |
| 29 | +final class DelegateBindingExpression extends BindingExpression { |
| 30 | + private final ContributionBinding binding; |
| 31 | + private final ComponentBindingExpressions componentBindingExpressions; |
| 32 | + private final DaggerTypes types; |
| 33 | + private final Elements elements; |
| 34 | + private final BindsTypeChecker bindsTypeChecker; |
| 35 | + |
| 36 | + private DelegateBindingExpression( |
| 37 | + ResolvedBindings resolvedBindings, |
| 38 | + ComponentBindingExpressions componentBindingExpressions, |
| 39 | + DaggerTypes types, |
| 40 | + Elements elements) { |
| 41 | + super(resolvedBindings); |
| 42 | + this.binding = checkNotNull(resolvedBindings.contributionBinding()); |
| 43 | + this.componentBindingExpressions = checkNotNull(componentBindingExpressions); |
| 44 | + this.types = checkNotNull(types); |
| 45 | + this.elements = checkNotNull(elements); |
| 46 | + this.bindsTypeChecker = new BindsTypeChecker(types, elements); |
| 47 | + } |
| 48 | + |
| 49 | + static BindingExpression create( |
| 50 | + BindingGraph graph, |
| 51 | + BindingExpression bindingExpression, |
| 52 | + ComponentBindingExpressions componentBindingExpressions, |
| 53 | + DaggerTypes types, |
| 54 | + Elements elements) { |
| 55 | + ResolvedBindings resolvedBindings = bindingExpression.resolvedBindings(); |
| 56 | + ContributionBinding binding = resolvedBindings.contributionBinding(); |
| 57 | + Binding delegateBinding = |
| 58 | + graph |
| 59 | + .resolvedBindings() |
| 60 | + .get(getOnlyElement(binding.dependencies()).bindingKey()) |
| 61 | + .binding(); |
| 62 | + ScopeKind bindsScope = ScopeKind.get(binding, graph, elements); |
| 63 | + ScopeKind delegateScope = ScopeKind.get(delegateBinding, graph, elements); |
| 64 | + if (bindsScope.isSimilarOrWeakerScopeThan(delegateScope)) { |
| 65 | + return new DelegateBindingExpression( |
| 66 | + resolvedBindings, componentBindingExpressions, types, elements); |
| 67 | + } |
| 68 | + return bindingExpression; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + Expression getDependencyExpression( |
| 73 | + DependencyRequest.Kind requestKind, ClassName requestingClass) { |
| 74 | + Expression delegateExpression = |
| 75 | + componentBindingExpressions.getDependencyExpression( |
| 76 | + getOnlyElement(binding.dependencies()).bindingKey(), requestKind, requestingClass); |
| 77 | + |
| 78 | + TypeMirror contributedType = binding.contributedType(); |
| 79 | + switch (requestKind) { |
| 80 | + case INSTANCE: |
| 81 | + return instanceRequiresCast(delegateExpression, requestingClass) |
| 82 | + ? delegateExpression.castTo(contributedType) |
| 83 | + : delegateExpression; |
| 84 | + default: |
| 85 | + return castToRawTypeIfNecessary( |
| 86 | + delegateExpression, requestKind.type(contributedType, types)); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private boolean instanceRequiresCast(Expression delegateExpression, ClassName requestingClass) { |
| 91 | + // delegateExpression.type() could be Object if expression is satisfied with a raw |
| 92 | + // Provider's get() method. |
| 93 | + return !bindsTypeChecker.isAssignable( |
| 94 | + delegateExpression.type(), binding.contributedType(), binding.contributionType()) |
| 95 | + && isTypeAccessibleFrom(binding.contributedType(), requestingClass.packageName()); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * If {@code delegateExpression} can be assigned to {@code desiredType} safely, then {@code |
| 100 | + * delegateExpression} is returned unchanged. If the {@code delegateExpression} is already a raw |
| 101 | + * type, returns {@code delegateExpression} as well, as casting would have no effect. Otherwise, |
| 102 | + * returns a {@link Expression#castTo(TypeMirror) casted} version of {@code delegateExpression} |
| 103 | + * to the raw type of {@code desiredType}. |
| 104 | + */ |
| 105 | + // TODO(ronshapiro): this probably can be generalized for usage in InjectionMethods |
| 106 | + private Expression castToRawTypeIfNecessary( |
| 107 | + Expression delegateExpression, TypeMirror desiredType) { |
| 108 | + if (types.isAssignable(delegateExpression.type(), desiredType)) { |
| 109 | + return delegateExpression; |
| 110 | + } |
| 111 | + return delegateExpression.castTo(types.erasure(desiredType)); |
| 112 | + } |
| 113 | + |
| 114 | + private enum ScopeKind { |
| 115 | + UNSCOPED, |
| 116 | + RELEASABLE, |
| 117 | + SINGLE_CHECK, |
| 118 | + DOUBLE_CHECK, |
| 119 | + ; |
| 120 | + |
| 121 | + static ScopeKind get(Binding binding, BindingGraph graph, Elements elements) { |
| 122 | + if (!binding.scope().isPresent()) { |
| 123 | + return UNSCOPED; |
| 124 | + } |
| 125 | + |
| 126 | + Scope scope = binding.scope().get(); |
| 127 | + if (graph.scopesRequiringReleasableReferenceManagers().contains(scope)) { |
| 128 | + return RELEASABLE; |
| 129 | + } |
| 130 | + return scope.equals(reusableScope(elements)) ? SINGLE_CHECK : DOUBLE_CHECK; |
| 131 | + } |
| 132 | + |
| 133 | + boolean isSimilarOrWeakerScopeThan(ScopeKind other) { |
| 134 | + return ordinal() <= other.ordinal(); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments