blob: f1c2e6a23f3ec7a2c8d224c2d288648f2f05abef [file] [log] [blame]
ronshapirodc07ed52017-08-23 08:52:10 -07001/*
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
17package dagger.internal.codegen;
18
19import com.squareup.javapoet.ClassName;
20import com.squareup.javapoet.CodeBlock;
21
22/** A binding expression for the instance of the component itself, i.e. {@code this}. */
23final class ComponentInstanceBindingExpression extends SimpleInvocationBindingExpression {
24 private final ClassName componentName;
ronshapiroba794862017-09-28 11:39:34 -070025 private final ContributionBinding binding;
ronshapirodc07ed52017-08-23 08:52:10 -070026
ronshapiroba794862017-09-28 11:39:34 -070027 ComponentInstanceBindingExpression(
28 BindingExpression delegate,
29 ContributionBinding binding,
30 ClassName componentName,
dpba409c6f2017-10-04 09:38:54 -070031 DaggerTypes types) {
32 super(delegate, types);
ronshapirodc07ed52017-08-23 08:52:10 -070033 this.componentName = componentName;
ronshapiroba794862017-09-28 11:39:34 -070034 this.binding = binding;
ronshapirodc07ed52017-08-23 08:52:10 -070035 }
36
37 @Override
bcorso8c3605d2017-12-13 13:36:49 -080038 Expression getInstanceDependencyExpression(ClassName requestingClass) {
ronshapiroba794862017-09-28 11:39:34 -070039 return Expression.create(
40 binding.key().type(),
41 componentName.equals(requestingClass)
42 ? CodeBlock.of("this")
43 : CodeBlock.of("$T.this", componentName));
ronshapirodc07ed52017-08-23 08:52:10 -070044 }
45}