| bcorso | cc739fe | 2018-04-23 09:16:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.squareup.javapoet.MethodSpec.constructorBuilder; |
| dpb | 3379b95 | 2018-09-24 13:39:04 -0700 | [diff] [blame^] | 20 | import static dagger.internal.codegen.BindingRequest.bindingRequest; |
| bcorso | cc739fe | 2018-04-23 09:16:12 -0700 | [diff] [blame] | 21 | import static dagger.model.RequestKind.INSTANCE; |
| 22 | import static javax.lang.model.element.Modifier.FINAL; |
| 23 | import static javax.lang.model.element.Modifier.PRIVATE; |
| 24 | |
| 25 | import com.squareup.javapoet.ClassName; |
| 26 | import com.squareup.javapoet.CodeBlock; |
| 27 | import com.squareup.javapoet.TypeName; |
| 28 | import com.squareup.javapoet.TypeSpec; |
| 29 | import dagger.model.Key; |
| 30 | import javax.inject.Provider; |
| 31 | import javax.lang.model.type.TypeMirror; |
| 32 | |
| 33 | /** |
| 34 | * Generates {@linkplain BindingExpression binding expressions} for a binding that is represented by |
| 35 | * an inner {@code SwitchingProvider} class. |
| 36 | */ |
| 37 | final class InnerSwitchingProviders extends SwitchingProviders { |
| 38 | private final ComponentBindingExpressions componentBindingExpressions; |
| 39 | private final DaggerTypes types; |
| 40 | |
| 41 | InnerSwitchingProviders( |
| 42 | GeneratedComponentModel generatedComponentModel, |
| 43 | ComponentBindingExpressions componentBindingExpressions, |
| 44 | DaggerTypes types) { |
| 45 | super(generatedComponentModel, types); |
| 46 | this.componentBindingExpressions = componentBindingExpressions; |
| 47 | this.types = types; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns the binding expression for a binding that satisfies a {@link Provider} requests with a |
| 52 | * inner {@code SwitchingProvider} class. |
| 53 | */ |
| 54 | BindingExpression newBindingExpression(ContributionBinding binding) { |
| 55 | return new BindingExpression() { |
| 56 | @Override |
| 57 | Expression getDependencyExpression(ClassName requestingClass) { |
| 58 | return getProviderExpression(new SwitchCase(binding, requestingClass)); |
| 59 | } |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | protected TypeSpec createSwitchingProviderType(TypeSpec.Builder builder) { |
| 65 | return builder |
| 66 | .addModifiers(PRIVATE, FINAL) |
| 67 | .addField(TypeName.INT, "id", PRIVATE, FINAL) |
| 68 | .addMethod( |
| 69 | constructorBuilder() |
| 70 | .addParameter(TypeName.INT, "id") |
| 71 | .addStatement("this.id = id") |
| 72 | .build()) |
| 73 | .build(); |
| 74 | } |
| 75 | |
| 76 | private final class SwitchCase implements SwitchingProviders.SwitchCase { |
| 77 | private final ContributionBinding binding; |
| 78 | private final ClassName requestingClass; |
| 79 | |
| 80 | SwitchCase(ContributionBinding binding, ClassName requestingClass) { |
| 81 | this.binding = binding; |
| 82 | this.requestingClass = requestingClass; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Key key() { |
| 87 | return binding.key(); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public Expression getProviderExpression(ClassName switchType, int switchId) { |
| 92 | TypeMirror instanceType = types.accessibleType(binding.contributedType(), requestingClass); |
| 93 | return Expression.create( |
| 94 | types.wrapType(instanceType, Provider.class), |
| 95 | CodeBlock.of("new $T<>($L)", switchType, switchId)); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public Expression getReturnExpression() { |
| 100 | return componentBindingExpressions.getDependencyExpression( |
| dpb | 3379b95 | 2018-09-24 13:39:04 -0700 | [diff] [blame^] | 101 | bindingRequest(binding.key(), INSTANCE), requestingClass); |
| bcorso | cc739fe | 2018-04-23 09:16:12 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |