| dpb | 6486184 | 2017-12-12 08:17:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.google.common.base.Preconditions.checkNotNull; |
| dpb | 3379b95 | 2018-09-24 13:39:04 -0700 | [diff] [blame] | 20 | import static dagger.internal.codegen.BindingRequest.bindingRequest; |
| dpb | 6486184 | 2017-12-12 08:17:18 -0800 | [diff] [blame] | 21 | |
| 22 | import com.squareup.javapoet.ClassName; |
| 23 | import com.squareup.javapoet.CodeBlock; |
| dpb | 3db68f0 | 2018-01-04 09:15:52 -0800 | [diff] [blame] | 24 | import dagger.internal.codegen.FrameworkFieldInitializer.FrameworkInstanceCreationExpression; |
| ronshapiro | 120abc6 | 2017-12-15 09:57:09 -0800 | [diff] [blame] | 25 | import dagger.model.RequestKind; |
| dpb | 6486184 | 2017-12-12 08:17:18 -0800 | [diff] [blame] | 26 | import dagger.producers.Producer; |
| 27 | import java.util.Optional; |
| 28 | |
| dpb | 3db68f0 | 2018-01-04 09:15:52 -0800 | [diff] [blame] | 29 | /** An {@link Producer} creation expression for provision bindings. */ |
| 30 | final class ProducerFromProviderCreationExpression implements FrameworkInstanceCreationExpression { |
| 31 | private final ContributionBinding binding; |
| 32 | private final GeneratedComponentModel generatedComponentModel; |
| dpb | 6486184 | 2017-12-12 08:17:18 -0800 | [diff] [blame] | 33 | private final ComponentBindingExpressions componentBindingExpressions; |
| dpb | 6486184 | 2017-12-12 08:17:18 -0800 | [diff] [blame] | 34 | |
| dpb | 3db68f0 | 2018-01-04 09:15:52 -0800 | [diff] [blame] | 35 | ProducerFromProviderCreationExpression( |
| 36 | ContributionBinding binding, |
| dpb | 6486184 | 2017-12-12 08:17:18 -0800 | [diff] [blame] | 37 | GeneratedComponentModel generatedComponentModel, |
| 38 | ComponentBindingExpressions componentBindingExpressions) { |
| dpb | 3db68f0 | 2018-01-04 09:15:52 -0800 | [diff] [blame] | 39 | this.binding = checkNotNull(binding); |
| 40 | this.generatedComponentModel = checkNotNull(generatedComponentModel); |
| dpb | 6486184 | 2017-12-12 08:17:18 -0800 | [diff] [blame] | 41 | this.componentBindingExpressions = checkNotNull(componentBindingExpressions); |
| dpb | 6486184 | 2017-12-12 08:17:18 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | @Override |
| dpb | 3db68f0 | 2018-01-04 09:15:52 -0800 | [diff] [blame] | 45 | public CodeBlock creationExpression() { |
| dpb | 6486184 | 2017-12-12 08:17:18 -0800 | [diff] [blame] | 46 | return FrameworkType.PROVIDER.to( |
| ronshapiro | 120abc6 | 2017-12-15 09:57:09 -0800 | [diff] [blame] | 47 | RequestKind.PRODUCER, |
| dpb | 6486184 | 2017-12-12 08:17:18 -0800 | [diff] [blame] | 48 | componentBindingExpressions |
| 49 | .getDependencyExpression( |
| dpb | 3379b95 | 2018-09-24 13:39:04 -0700 | [diff] [blame] | 50 | bindingRequest(binding.key(), FrameworkType.PROVIDER), |
| dpb | 3db68f0 | 2018-01-04 09:15:52 -0800 | [diff] [blame] | 51 | generatedComponentModel.name()) |
| dpb | 6486184 | 2017-12-12 08:17:18 -0800 | [diff] [blame] | 52 | .codeBlock()); |
| 53 | } |
| 54 | |
| 55 | @Override |
| dpb | 3db68f0 | 2018-01-04 09:15:52 -0800 | [diff] [blame] | 56 | public Optional<ClassName> alternativeFrameworkClass() { |
| cgdecker | 9266549 | 2018-05-03 07:56:17 -0700 | [diff] [blame] | 57 | return Optional.of(TypeNames.PRODUCER); |
| dpb | 6486184 | 2017-12-12 08:17:18 -0800 | [diff] [blame] | 58 | } |
| ronshapiro | e674076 | 2018-05-01 08:37:26 -0700 | [diff] [blame] | 59 | |
| 60 | // TODO(ronshapiro): should this have a simple factory if the delegate expression is simple? |
| dpb | 6486184 | 2017-12-12 08:17:18 -0800 | [diff] [blame] | 61 | } |