| ronshapiro | b449911 | 2017-08-30 11:34:31 -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.base.Preconditions.checkArgument; |
| 20 | import static com.google.common.collect.Iterables.getOnlyElement; |
| 21 | import static dagger.internal.codegen.Accessibility.isTypeAccessibleFrom; |
| dpb | 3379b95 | 2018-09-24 13:39:04 -0700 | [diff] [blame] | 22 | import static dagger.internal.codegen.BindingRequest.bindingRequest; |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 23 | import static dagger.internal.codegen.CodeBlocks.toParametersCodeBlock; |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 24 | import static dagger.internal.codegen.MapKeys.getMapKeyExpression; |
| ronshapiro | 0c4cddf | 2018-01-04 15:28:51 -0800 | [diff] [blame] | 25 | import static dagger.model.BindingKind.MULTIBOUND_MAP; |
| sunxin | 84ec3c6 | 2018-03-16 12:31:45 -0700 | [diff] [blame] | 26 | import static javax.lang.model.util.ElementFilter.methodsIn; |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 27 | |
| 28 | import com.google.common.collect.ImmutableMap; |
| 29 | import com.google.common.collect.Maps; |
| 30 | import com.squareup.javapoet.ClassName; |
| 31 | import com.squareup.javapoet.CodeBlock; |
| 32 | import dagger.internal.MapBuilder; |
| ronshapiro | 0c4cddf | 2018-01-04 15:28:51 -0800 | [diff] [blame] | 33 | import dagger.model.BindingKind; |
| ronshapiro | a5023ca | 2018-01-02 12:55:01 -0800 | [diff] [blame] | 34 | import dagger.model.DependencyRequest; |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 35 | import java.util.Collections; |
| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame] | 36 | import java.util.Optional; |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 37 | import javax.lang.model.type.DeclaredType; |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 38 | import javax.lang.model.type.TypeMirror; |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 39 | |
| 40 | /** A {@link BindingExpression} for multibound maps. */ |
| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame] | 41 | final class MapBindingExpression extends MultibindingExpression { |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 42 | /** Maximum number of key-value pairs that can be passed to ImmutableMap.of(K, V, K, V, ...). */ |
| 43 | private static final int MAX_IMMUTABLE_MAP_OF_KEY_VALUE_PAIRS = 5; |
| 44 | |
| 45 | private final ProvisionBinding binding; |
| 46 | private final ImmutableMap<DependencyRequest, ContributionBinding> dependencies; |
| 47 | private final ComponentBindingExpressions componentBindingExpressions; |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 48 | private final DaggerTypes types; |
| dpb | e7b8958 | 2018-02-19 08:42:19 -0800 | [diff] [blame] | 49 | private final DaggerElements elements; |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 50 | |
| 51 | MapBindingExpression( |
| dpb | 09fb2cb | 2018-01-29 08:39:33 -0800 | [diff] [blame] | 52 | ResolvedBindings resolvedBindings, |
| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame] | 53 | GeneratedComponentModel generatedComponentModel, |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 54 | BindingGraph graph, |
| 55 | ComponentBindingExpressions componentBindingExpressions, |
| dpb | a409c6f | 2017-10-04 09:38:54 -0700 | [diff] [blame] | 56 | DaggerTypes types, |
| dpb | e7b8958 | 2018-02-19 08:42:19 -0800 | [diff] [blame] | 57 | DaggerElements elements) { |
| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame] | 58 | super(resolvedBindings, generatedComponentModel); |
| dpb | 09fb2cb | 2018-01-29 08:39:33 -0800 | [diff] [blame] | 59 | this.binding = (ProvisionBinding) resolvedBindings.contributionBinding(); |
| 60 | BindingKind bindingKind = this.binding.kind(); |
| ronshapiro | 0c4cddf | 2018-01-04 15:28:51 -0800 | [diff] [blame] | 61 | checkArgument(bindingKind.equals(MULTIBOUND_MAP), bindingKind); |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 62 | this.componentBindingExpressions = componentBindingExpressions; |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 63 | this.types = types; |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 64 | this.elements = elements; |
| 65 | this.dependencies = |
| 66 | Maps.toMap( |
| 67 | binding.dependencies(), |
| ronshapiro | 0a277fd | 2017-12-22 08:30:49 -0800 | [diff] [blame] | 68 | dep -> graph.contributionBindings().get(dep.key()).contributionBinding()); |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame] | 72 | protected Expression buildDependencyExpression(ClassName requestingClass) { |
| 73 | Optional<CodeBlock> superMethodCall = superMethodCall(); |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 74 | // TODO(ronshapiro): We should also make an ImmutableMap version of MapFactory |
| 75 | boolean isImmutableMapAvailable = isImmutableMapAvailable(); |
| 76 | // TODO(ronshapiro, gak): Use Maps.immutableEnumMap() if it's available? |
| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame] | 77 | if (isImmutableMapAvailable |
| 78 | && dependencies.size() <= MAX_IMMUTABLE_MAP_OF_KEY_VALUE_PAIRS |
| 79 | && !superMethodCall.isPresent()) { |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 80 | return Expression.create( |
| 81 | immutableMapType(), |
| 82 | CodeBlock.builder() |
| 83 | .add("$T.", ImmutableMap.class) |
| 84 | .add(maybeTypeParameters(requestingClass)) |
| 85 | .add( |
| 86 | "of($L)", |
| 87 | dependencies |
| 88 | .keySet() |
| 89 | .stream() |
| 90 | .map(dependency -> keyAndValueExpression(dependency, requestingClass)) |
| 91 | .collect(toParametersCodeBlock())) |
| 92 | .build()); |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 93 | } |
| 94 | switch (dependencies.size()) { |
| 95 | case 0: |
| 96 | return collectionsStaticFactoryInvocation(requestingClass, CodeBlock.of("emptyMap()")); |
| 97 | case 1: |
| 98 | return collectionsStaticFactoryInvocation( |
| 99 | requestingClass, |
| 100 | CodeBlock.of( |
| 101 | "singletonMap($L)", |
| 102 | keyAndValueExpression(getOnlyElement(dependencies.keySet()), requestingClass))); |
| 103 | default: |
| 104 | CodeBlock.Builder instantiation = CodeBlock.builder(); |
| 105 | instantiation |
| 106 | .add("$T.", isImmutableMapAvailable ? ImmutableMap.class : MapBuilder.class) |
| 107 | .add(maybeTypeParameters(requestingClass)); |
| sunxin | 84ec3c6 | 2018-03-16 12:31:45 -0700 | [diff] [blame] | 108 | if (isImmutableMapBuilderWithExpectedSizeAvailable()) { |
| 109 | instantiation.add("builderWithExpectedSize($L)", dependencies.size()); |
| 110 | } else if (isImmutableMapAvailable) { |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 111 | instantiation.add("builder()"); |
| 112 | } else { |
| 113 | instantiation.add("newMapBuilder($L)", dependencies.size()); |
| 114 | } |
| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame] | 115 | for (DependencyRequest dependency : getNewContributions(dependencies.keySet())) { |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 116 | instantiation.add(".put($L)", keyAndValueExpression(dependency, requestingClass)); |
| 117 | } |
| dstrasburg | 09adeae | 2018-09-07 12:11:06 -0700 | [diff] [blame] | 118 | if (superMethodCall.isPresent()) { |
| 119 | instantiation.add(CodeBlock.of(".putAll($L)", superMethodCall.get())); |
| 120 | } |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 121 | return Expression.create( |
| 122 | isImmutableMapAvailable ? immutableMapType() : binding.key().type(), |
| 123 | instantiation.add(".build()").build()); |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 127 | private DeclaredType immutableMapType() { |
| 128 | MapType mapType = MapType.from(binding.key()); |
| 129 | return types.getDeclaredType( |
| dpb | e7b8958 | 2018-02-19 08:42:19 -0800 | [diff] [blame] | 130 | elements.getTypeElement(ImmutableMap.class), mapType.keyType(), mapType.valueType()); |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 133 | private CodeBlock keyAndValueExpression(DependencyRequest dependency, ClassName requestingClass) { |
| 134 | return CodeBlock.of( |
| 135 | "$L, $L", |
| ronshapiro | 3a08964 | 2018-07-17 15:39:40 -0700 | [diff] [blame] | 136 | getMapKeyExpression(dependencies.get(dependency), requestingClass, elements), |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame] | 137 | componentBindingExpressions |
| dpb | 3379b95 | 2018-09-24 13:39:04 -0700 | [diff] [blame] | 138 | .getDependencyExpression(bindingRequest(dependency), requestingClass) |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame] | 139 | .codeBlock()); |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 142 | private Expression collectionsStaticFactoryInvocation( |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 143 | ClassName requestingClass, CodeBlock methodInvocation) { |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame] | 144 | return Expression.create( |
| 145 | binding.key().type(), |
| 146 | CodeBlock.builder() |
| 147 | .add("$T.", Collections.class) |
| 148 | .add(maybeTypeParameters(requestingClass)) |
| 149 | .add(methodInvocation) |
| 150 | .build()); |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | private CodeBlock maybeTypeParameters(ClassName requestingClass) { |
| 154 | TypeMirror bindingKeyType = binding.key().type(); |
| 155 | MapType mapType = MapType.from(binding.key()); |
| 156 | return isTypeAccessibleFrom(bindingKeyType, requestingClass.packageName()) |
| 157 | ? CodeBlock.of("<$T, $T>", mapType.keyType(), mapType.valueType()) |
| 158 | : CodeBlock.of(""); |
| 159 | } |
| 160 | |
| sunxin | 84ec3c6 | 2018-03-16 12:31:45 -0700 | [diff] [blame] | 161 | private boolean isImmutableMapBuilderWithExpectedSizeAvailable() { |
| 162 | if (isImmutableMapAvailable()) { |
| 163 | return methodsIn(elements.getTypeElement(ImmutableMap.class).getEnclosedElements()) |
| 164 | .stream() |
| 165 | .anyMatch(method -> method.getSimpleName().contentEquals("builderWithExpectedSize")); |
| 166 | } |
| 167 | return false; |
| 168 | } |
| 169 | |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 170 | private boolean isImmutableMapAvailable() { |
| dpb | e7b8958 | 2018-02-19 08:42:19 -0800 | [diff] [blame] | 171 | return elements.getTypeElement(ImmutableMap.class) != null; |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 172 | } |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 173 | } |