blob: 91c131773a28796847a8762f9b0e94a9db171bfc [file] [log] [blame]
bcorsocc739fe2018-04-23 09:16:12 -07001/*
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
17package dagger.internal.codegen;
18
19import static com.squareup.javapoet.MethodSpec.constructorBuilder;
dpb3379b952018-09-24 13:39:04 -070020import static dagger.internal.codegen.BindingRequest.bindingRequest;
bcorsocc739fe2018-04-23 09:16:12 -070021import static dagger.model.RequestKind.INSTANCE;
22import static javax.lang.model.element.Modifier.FINAL;
23import static javax.lang.model.element.Modifier.PRIVATE;
24
25import com.squareup.javapoet.ClassName;
26import com.squareup.javapoet.CodeBlock;
27import com.squareup.javapoet.TypeName;
28import com.squareup.javapoet.TypeSpec;
29import dagger.model.Key;
30import javax.inject.Provider;
31import 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 */
37final class InnerSwitchingProviders extends SwitchingProviders {
38 private final ComponentBindingExpressions componentBindingExpressions;
39 private final DaggerTypes types;
40
41 InnerSwitchingProviders(
dpb159d81b2018-11-01 14:06:17 -070042 ComponentImplementation componentImplementation,
bcorsocc739fe2018-04-23 09:16:12 -070043 ComponentBindingExpressions componentBindingExpressions,
44 DaggerTypes types) {
dpb159d81b2018-11-01 14:06:17 -070045 super(componentImplementation, types);
bcorsocc739fe2018-04-23 09:16:12 -070046 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
bcorsobe9493d2018-10-04 13:52:31 -070091 public Expression getProviderExpression(ClassName switchingProviderClass, int switchId) {
bcorsocc739fe2018-04-23 09:16:12 -070092 TypeMirror instanceType = types.accessibleType(binding.contributedType(), requestingClass);
93 return Expression.create(
94 types.wrapType(instanceType, Provider.class),
bcorsobe9493d2018-10-04 13:52:31 -070095 CodeBlock.of("new $T<>($L)", switchingProviderClass, switchId));
bcorsocc739fe2018-04-23 09:16:12 -070096 }
97
98 @Override
bcorsobe9493d2018-10-04 13:52:31 -070099 public Expression getReturnExpression(ClassName switchingProviderClass) {
bcorsocc739fe2018-04-23 09:16:12 -0700100 return componentBindingExpressions.getDependencyExpression(
bcorsobe9493d2018-10-04 13:52:31 -0700101 bindingRequest(binding.key(), INSTANCE), switchingProviderClass);
bcorsocc739fe2018-04-23 09:16:12 -0700102 }
103 }
104}