| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 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.collect.Iterables.getOnlyElement; |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 20 | import static dagger.internal.codegen.Accessibility.isTypeAccessibleFrom; |
| dpb | 3379b95 | 2018-09-24 13:39:04 -0700 | [diff] [blame] | 21 | import static dagger.internal.codegen.BindingRequest.bindingRequest; |
| ronshapiro | cb725da | 2017-07-04 15:14:48 -0700 | [diff] [blame] | 22 | import static dagger.internal.codegen.CodeBlocks.toParametersCodeBlock; |
| sunxin | 84ec3c6 | 2018-03-16 12:31:45 -0700 | [diff] [blame] | 23 | import static javax.lang.model.util.ElementFilter.methodsIn; |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 24 | |
| ronshapiro | cb725da | 2017-07-04 15:14:48 -0700 | [diff] [blame] | 25 | import com.google.common.collect.ImmutableSet; |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 26 | import com.squareup.javapoet.ClassName; |
| 27 | import com.squareup.javapoet.CodeBlock; |
| 28 | import dagger.internal.SetBuilder; |
| ronshapiro | a5023ca | 2018-01-02 12:55:01 -0800 | [diff] [blame] | 29 | import dagger.model.DependencyRequest; |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 30 | import java.util.Collections; |
| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame] | 31 | import java.util.Optional; |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 32 | import javax.lang.model.type.DeclaredType; |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 33 | import javax.lang.model.type.TypeMirror; |
| 34 | |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 35 | /** A binding expression for multibound sets. */ |
| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame] | 36 | final class SetBindingExpression extends MultibindingExpression { |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 37 | private final ProvisionBinding binding; |
| erichang | 9dd6aed | 2017-06-15 15:54:00 -0700 | [diff] [blame] | 38 | private final BindingGraph graph; |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 39 | private final ComponentBindingExpressions componentBindingExpressions; |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 40 | private final DaggerTypes types; |
| dpb | e7b8958 | 2018-02-19 08:42:19 -0800 | [diff] [blame] | 41 | private final DaggerElements elements; |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 42 | |
| ronshapiro | 186d17a | 2017-08-11 08:18:07 -0700 | [diff] [blame] | 43 | SetBindingExpression( |
| dpb | 09fb2cb | 2018-01-29 08:39:33 -0800 | [diff] [blame] | 44 | ResolvedBindings resolvedBindings, |
| dpb | 159d81b | 2018-11-01 14:06:17 -0700 | [diff] [blame] | 45 | ComponentImplementation componentImplementation, |
| erichang | 9dd6aed | 2017-06-15 15:54:00 -0700 | [diff] [blame] | 46 | BindingGraph graph, |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 47 | ComponentBindingExpressions componentBindingExpressions, |
| dpb | a409c6f | 2017-10-04 09:38:54 -0700 | [diff] [blame] | 48 | DaggerTypes types, |
| dpb | e7b8958 | 2018-02-19 08:42:19 -0800 | [diff] [blame] | 49 | DaggerElements elements) { |
| dpb | 159d81b | 2018-11-01 14:06:17 -0700 | [diff] [blame] | 50 | super(resolvedBindings, componentImplementation); |
| dpb | 09fb2cb | 2018-01-29 08:39:33 -0800 | [diff] [blame] | 51 | this.binding = (ProvisionBinding) resolvedBindings.contributionBinding(); |
| erichang | 9dd6aed | 2017-06-15 15:54:00 -0700 | [diff] [blame] | 52 | this.graph = graph; |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 53 | this.componentBindingExpressions = componentBindingExpressions; |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 54 | this.types = types; |
| ronshapiro | cb725da | 2017-07-04 15:14:48 -0700 | [diff] [blame] | 55 | this.elements = elements; |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame] | 59 | protected Expression buildDependencyExpression(ClassName requestingClass) { |
| 60 | Optional<CodeBlock> superMethodCall = superMethodCall(); |
| ronshapiro | cb725da | 2017-07-04 15:14:48 -0700 | [diff] [blame] | 61 | // TODO(ronshapiro): We should also make an ImmutableSet version of SetFactory |
| 62 | boolean isImmutableSetAvailable = isImmutableSetAvailable(); |
| 63 | // TODO(ronshapiro, gak): Use Sets.immutableEnumSet() if it's available? |
| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame] | 64 | if (isImmutableSetAvailable |
| 65 | && binding.dependencies().stream().allMatch(this::isSingleValue) |
| 66 | && !superMethodCall.isPresent()) { |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 67 | return Expression.create( |
| 68 | immutableSetType(), |
| 69 | CodeBlock.builder() |
| 70 | .add("$T.", ImmutableSet.class) |
| 71 | .add(maybeTypeParameter(requestingClass)) |
| 72 | .add( |
| 73 | "of($L)", |
| 74 | binding |
| 75 | .dependencies() |
| 76 | .stream() |
| 77 | .map(dependency -> getContributionExpression(dependency, requestingClass)) |
| 78 | .collect(toParametersCodeBlock())) |
| 79 | .build()); |
| ronshapiro | cb725da | 2017-07-04 15:14:48 -0700 | [diff] [blame] | 80 | } |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 81 | switch (binding.dependencies().size()) { |
| 82 | case 0: |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 83 | return collectionsStaticFactoryInvocation(requestingClass, CodeBlock.of("emptySet()")); |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 84 | case 1: |
| 85 | { |
| 86 | DependencyRequest dependency = getOnlyElement(binding.dependencies()); |
| dpb | a6ff6c1 | 2017-08-14 08:47:36 -0700 | [diff] [blame] | 87 | CodeBlock contributionExpression = getContributionExpression(dependency, requestingClass); |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 88 | if (isSingleValue(dependency)) { |
| 89 | return collectionsStaticFactoryInvocation( |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 90 | requestingClass, CodeBlock.of("singleton($L)", contributionExpression)); |
| ronshapiro | cb725da | 2017-07-04 15:14:48 -0700 | [diff] [blame] | 91 | } else if (isImmutableSetAvailable) { |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 92 | return Expression.create( |
| 93 | immutableSetType(), |
| 94 | CodeBlock.builder() |
| 95 | .add("$T.", ImmutableSet.class) |
| 96 | .add(maybeTypeParameter(requestingClass)) |
| 97 | .add("copyOf($L)", contributionExpression) |
| 98 | .build()); |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | // fall through |
| 102 | default: |
| 103 | CodeBlock.Builder instantiation = CodeBlock.builder(); |
| 104 | instantiation |
| ronshapiro | cb725da | 2017-07-04 15:14:48 -0700 | [diff] [blame] | 105 | .add("$T.", isImmutableSetAvailable ? ImmutableSet.class : SetBuilder.class) |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 106 | .add(maybeTypeParameter(requestingClass)); |
| sunxin | 84ec3c6 | 2018-03-16 12:31:45 -0700 | [diff] [blame] | 107 | if (isImmutableSetBuilderWithExpectedSizeAvailable()) { |
| 108 | instantiation.add("builderWithExpectedSize($L)", binding.dependencies().size()); |
| 109 | } else if (isImmutableSetAvailable) { |
| ronshapiro | cb725da | 2017-07-04 15:14:48 -0700 | [diff] [blame] | 110 | instantiation.add("builder()"); |
| 111 | } else { |
| 112 | instantiation.add("newSetBuilder($L)", binding.dependencies().size()); |
| 113 | } |
| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame] | 114 | for (DependencyRequest dependency : getNewContributions(binding.dependencies())) { |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 115 | String builderMethod = isSingleValue(dependency) ? "add" : "addAll"; |
| 116 | instantiation.add( |
| dpb | a6ff6c1 | 2017-08-14 08:47:36 -0700 | [diff] [blame] | 117 | ".$L($L)", builderMethod, getContributionExpression(dependency, requestingClass)); |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 118 | } |
| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame] | 119 | if (superMethodCall.isPresent()) { |
| 120 | instantiation.add(CodeBlock.of(".addAll($L)", superMethodCall.get())); |
| 121 | } |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 122 | instantiation.add(".build()"); |
| 123 | return Expression.create( |
| 124 | isImmutableSetAvailable ? immutableSetType() : binding.key().type(), |
| 125 | instantiation.build()); |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 129 | private DeclaredType immutableSetType() { |
| 130 | return types.getDeclaredType( |
| dpb | e7b8958 | 2018-02-19 08:42:19 -0800 | [diff] [blame] | 131 | elements.getTypeElement(ImmutableSet.class), SetType.from(binding.key()).elementType()); |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| dpb | a6ff6c1 | 2017-08-14 08:47:36 -0700 | [diff] [blame] | 134 | private CodeBlock getContributionExpression( |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 135 | DependencyRequest dependency, ClassName requestingClass) { |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame] | 136 | return componentBindingExpressions |
| dpb | 3379b95 | 2018-09-24 13:39:04 -0700 | [diff] [blame] | 137 | .getDependencyExpression(bindingRequest(dependency), requestingClass) |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame] | 138 | .codeBlock(); |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 141 | private Expression collectionsStaticFactoryInvocation( |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 142 | ClassName requestingClass, CodeBlock methodInvocation) { |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 143 | return Expression.create( |
| 144 | binding.key().type(), |
| 145 | CodeBlock.builder() |
| 146 | .add("$T.", Collections.class) |
| 147 | .add(maybeTypeParameter(requestingClass)) |
| 148 | .add(methodInvocation) |
| 149 | .build()); |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| dpb | 68c77d8 | 2017-08-15 15:23:35 -0700 | [diff] [blame] | 152 | private CodeBlock maybeTypeParameter(ClassName requestingClass) { |
| 153 | TypeMirror elementType = SetType.from(binding.key()).elementType(); |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 154 | return isTypeAccessibleFrom(elementType, requestingClass.packageName()) |
| ronshapiro | cb725da | 2017-07-04 15:14:48 -0700 | [diff] [blame] | 155 | ? CodeBlock.of("<$T>", elementType) |
| 156 | : CodeBlock.of(""); |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | private boolean isSingleValue(DependencyRequest dependency) { |
| erichang | 9dd6aed | 2017-06-15 15:54:00 -0700 | [diff] [blame] | 160 | return graph |
| ronshapiro | 0a277fd | 2017-12-22 08:30:49 -0800 | [diff] [blame] | 161 | .contributionBindings() |
| 162 | .get(dependency.key()) |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 163 | .contributionBinding() |
| 164 | .contributionType() |
| 165 | .equals(ContributionType.SET); |
| 166 | } |
| ronshapiro | cb725da | 2017-07-04 15:14:48 -0700 | [diff] [blame] | 167 | |
| sunxin | 84ec3c6 | 2018-03-16 12:31:45 -0700 | [diff] [blame] | 168 | private boolean isImmutableSetBuilderWithExpectedSizeAvailable() { |
| 169 | if (isImmutableSetAvailable()) { |
| 170 | return methodsIn(elements.getTypeElement(ImmutableSet.class).getEnclosedElements()) |
| 171 | .stream() |
| 172 | .anyMatch(method -> method.getSimpleName().contentEquals("builderWithExpectedSize")); |
| 173 | } |
| 174 | return false; |
| 175 | } |
| 176 | |
| ronshapiro | cb725da | 2017-07-04 15:14:48 -0700 | [diff] [blame] | 177 | private boolean isImmutableSetAvailable() { |
| dpb | e7b8958 | 2018-02-19 08:42:19 -0800 | [diff] [blame] | 178 | return elements.getTypeElement(ImmutableSet.class) != null; |
| ronshapiro | cb725da | 2017-07-04 15:14:48 -0700 | [diff] [blame] | 179 | } |
| ronshapiro | 7bd173e | 2017-06-14 11:00:36 -0700 | [diff] [blame] | 180 | } |