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