| ronshapiro | dc07ed5 | 2017-08-23 08:52:10 -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 com.squareup.javapoet.ClassName; |
| 20 | import com.squareup.javapoet.CodeBlock; |
| 21 | |
| 22 | /** A binding expression for the instance of the component itself, i.e. {@code this}. */ |
| 23 | final class ComponentInstanceBindingExpression extends SimpleInvocationBindingExpression { |
| 24 | private final ClassName componentName; |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame] | 25 | private final ContributionBinding binding; |
| ronshapiro | dc07ed5 | 2017-08-23 08:52:10 -0700 | [diff] [blame] | 26 | |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame] | 27 | ComponentInstanceBindingExpression( |
| 28 | BindingExpression delegate, |
| 29 | ContributionBinding binding, |
| 30 | ClassName componentName, |
| dpb | a409c6f | 2017-10-04 09:38:54 -0700 | [diff] [blame] | 31 | DaggerTypes types) { |
| 32 | super(delegate, types); |
| ronshapiro | dc07ed5 | 2017-08-23 08:52:10 -0700 | [diff] [blame] | 33 | this.componentName = componentName; |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame] | 34 | this.binding = binding; |
| ronshapiro | dc07ed5 | 2017-08-23 08:52:10 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | @Override |
| bcorso | 8c3605d | 2017-12-13 13:36:49 -0800 | [diff] [blame] | 38 | Expression getInstanceDependencyExpression(ClassName requestingClass) { |
| ronshapiro | ba79486 | 2017-09-28 11:39:34 -0700 | [diff] [blame] | 39 | return Expression.create( |
| 40 | binding.key().type(), |
| 41 | componentName.equals(requestingClass) |
| 42 | ? CodeBlock.of("this") |
| 43 | : CodeBlock.of("$T.this", componentName)); |
| ronshapiro | dc07ed5 | 2017-08-23 08:52:10 -0700 | [diff] [blame] | 44 | } |
| 45 | } |