| 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; |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 25 | |
| 26 | import com.google.common.collect.ImmutableMap; |
| 27 | import com.google.common.collect.Maps; |
| 28 | import com.squareup.javapoet.ClassName; |
| 29 | import com.squareup.javapoet.CodeBlock; |
| 30 | import dagger.internal.MapBuilder; |
| ronshapiro | 0c4cddf | 2018-01-04 15:28:51 -0800 | [diff] [blame] | 31 | import dagger.model.BindingKind; |
| ronshapiro | a5023ca | 2018-01-02 12:55:01 -0800 | [diff] [blame] | 32 | import dagger.model.DependencyRequest; |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 33 | import java.util.Collections; |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame^] | 34 | import javax.lang.model.type.DeclaredType; |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 35 | import javax.lang.model.type.TypeMirror; |
| 36 | import javax.lang.model.util.Elements; |
| 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; |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 47 | private final Elements elements; |
| 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, |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 54 | Elements 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)); |
| 102 | if (isImmutableMapAvailable) { |
| 103 | // TODO(ronshapiro): builderWithExpectedSize |
| 104 | instantiation.add("builder()"); |
| 105 | } else { |
| 106 | instantiation.add("newMapBuilder($L)", dependencies.size()); |
| 107 | } |
| 108 | for (DependencyRequest dependency : dependencies.keySet()) { |
| 109 | instantiation.add(".put($L)", keyAndValueExpression(dependency, requestingClass)); |
| 110 | } |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame^] | 111 | return Expression.create( |
| 112 | isImmutableMapAvailable ? immutableMapType() : binding.key().type(), |
| 113 | instantiation.add(".build()").build()); |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame^] | 117 | private DeclaredType immutableMapType() { |
| 118 | MapType mapType = MapType.from(binding.key()); |
| 119 | return types.getDeclaredType( |
| 120 | elements.getTypeElement(ImmutableMap.class.getName()), |
| 121 | mapType.keyType(), |
| 122 | mapType.valueType()); |
| 123 | } |
| 124 | |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 125 | private CodeBlock keyAndValueExpression(DependencyRequest dependency, ClassName requestingClass) { |
| 126 | return CodeBlock.of( |
| 127 | "$L, $L", |
| ronshapiro | ab02939 | 2017-09-27 10:40:25 -0700 | [diff] [blame] | 128 | getMapKeyExpression(dependencies.get(dependency), requestingClass), |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame] | 129 | componentBindingExpressions |
| 130 | .getDependencyExpression(dependency, requestingClass) |
| 131 | .codeBlock()); |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame^] | 134 | private Expression collectionsStaticFactoryInvocation( |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 135 | ClassName requestingClass, CodeBlock methodInvocation) { |
| dpb | 356a684 | 2018-02-07 07:45:50 -0800 | [diff] [blame^] | 136 | return Expression.create( |
| 137 | binding.key().type(), |
| 138 | CodeBlock.builder() |
| 139 | .add("$T.", Collections.class) |
| 140 | .add(maybeTypeParameters(requestingClass)) |
| 141 | .add(methodInvocation) |
| 142 | .build()); |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | private CodeBlock maybeTypeParameters(ClassName requestingClass) { |
| 146 | TypeMirror bindingKeyType = binding.key().type(); |
| 147 | MapType mapType = MapType.from(binding.key()); |
| 148 | return isTypeAccessibleFrom(bindingKeyType, requestingClass.packageName()) |
| 149 | ? CodeBlock.of("<$T, $T>", mapType.keyType(), mapType.valueType()) |
| 150 | : CodeBlock.of(""); |
| 151 | } |
| 152 | |
| 153 | private boolean isImmutableMapAvailable() { |
| 154 | return elements.getTypeElement(ImmutableMap.class.getCanonicalName()) != null; |
| 155 | } |
| ronshapiro | b449911 | 2017-08-30 11:34:31 -0700 | [diff] [blame] | 156 | } |