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.writing;
18 
19 import static com.google.common.base.Preconditions.checkNotNull;
20 import static dagger.internal.codegen.binding.BindingRequest.bindingRequest;
21 
22 import com.squareup.javapoet.ClassName;
23 import dagger.internal.codegen.binding.BindingRequest;
24 import dagger.internal.codegen.binding.ComponentDescriptor.ComponentMethodDescriptor;
25 import dagger.internal.codegen.binding.FrameworkType;
26 import dagger.internal.codegen.javapoet.Expression;
27 import dagger.internal.codegen.langmodel.DaggerTypes;
28 import dagger.model.Key;
29 import dagger.model.RequestKind;
30 
31 /** A binding expression that depends on a framework instance. */
32 final class DerivedFromFrameworkInstanceBindingExpression extends BindingExpression {
33 
34   private final BindingRequest frameworkRequest;
35   private final RequestKind requestKind;
36   private final FrameworkType frameworkType;
37   private final ComponentBindingExpressions componentBindingExpressions;
38   private final DaggerTypes types;
39 
DerivedFromFrameworkInstanceBindingExpression( Key key, FrameworkType frameworkType, RequestKind requestKind, ComponentBindingExpressions componentBindingExpressions, DaggerTypes types)40   DerivedFromFrameworkInstanceBindingExpression(
41       Key key,
42       FrameworkType frameworkType,
43       RequestKind requestKind,
44       ComponentBindingExpressions componentBindingExpressions,
45       DaggerTypes types) {
46     this.frameworkRequest = bindingRequest(key, frameworkType);
47     this.requestKind = checkNotNull(requestKind);
48     this.frameworkType = checkNotNull(frameworkType);
49     this.componentBindingExpressions = checkNotNull(componentBindingExpressions);
50     this.types = checkNotNull(types);
51   }
52 
53   @Override
getDependencyExpression(ClassName requestingClass)54   Expression getDependencyExpression(ClassName requestingClass) {
55     return frameworkType.to(
56         requestKind,
57         componentBindingExpressions.getDependencyExpression(frameworkRequest, requestingClass),
58         types);
59   }
60 
61   @Override
getDependencyExpressionForComponentMethod( ComponentMethodDescriptor componentMethod, ComponentImplementation component)62   Expression getDependencyExpressionForComponentMethod(
63       ComponentMethodDescriptor componentMethod, ComponentImplementation component) {
64     Expression frameworkInstance =
65         componentBindingExpressions.getDependencyExpressionForComponentMethod(
66             frameworkRequest, componentMethod, component);
67     return frameworkType.to(requestKind, frameworkInstance, types);
68   }
69 }
70