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