blob: d4f6d85872c712b7457237a0ea8d36cc408e0c95 [file] [log] [blame]
dpb64861842017-12-12 08:17:18 -08001/*
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
17package dagger.internal.codegen;
18
19import static com.google.common.base.Preconditions.checkNotNull;
dpb3379b952018-09-24 13:39:04 -070020import static dagger.internal.codegen.BindingRequest.bindingRequest;
dpb64861842017-12-12 08:17:18 -080021
22import com.squareup.javapoet.ClassName;
23import com.squareup.javapoet.CodeBlock;
dpb3db68f02018-01-04 09:15:52 -080024import dagger.internal.codegen.FrameworkFieldInitializer.FrameworkInstanceCreationExpression;
ronshapiro120abc62017-12-15 09:57:09 -080025import dagger.model.RequestKind;
dpb64861842017-12-12 08:17:18 -080026import dagger.producers.Producer;
27import java.util.Optional;
28
dpb3db68f02018-01-04 09:15:52 -080029/** An {@link Producer} creation expression for provision bindings. */
30final class ProducerFromProviderCreationExpression implements FrameworkInstanceCreationExpression {
31 private final ContributionBinding binding;
32 private final GeneratedComponentModel generatedComponentModel;
dpb64861842017-12-12 08:17:18 -080033 private final ComponentBindingExpressions componentBindingExpressions;
dpb64861842017-12-12 08:17:18 -080034
dpb3db68f02018-01-04 09:15:52 -080035 ProducerFromProviderCreationExpression(
36 ContributionBinding binding,
dpb64861842017-12-12 08:17:18 -080037 GeneratedComponentModel generatedComponentModel,
38 ComponentBindingExpressions componentBindingExpressions) {
dpb3db68f02018-01-04 09:15:52 -080039 this.binding = checkNotNull(binding);
40 this.generatedComponentModel = checkNotNull(generatedComponentModel);
dpb64861842017-12-12 08:17:18 -080041 this.componentBindingExpressions = checkNotNull(componentBindingExpressions);
dpb64861842017-12-12 08:17:18 -080042 }
43
44 @Override
dpb3db68f02018-01-04 09:15:52 -080045 public CodeBlock creationExpression() {
dpb64861842017-12-12 08:17:18 -080046 return FrameworkType.PROVIDER.to(
ronshapiro120abc62017-12-15 09:57:09 -080047 RequestKind.PRODUCER,
dpb64861842017-12-12 08:17:18 -080048 componentBindingExpressions
49 .getDependencyExpression(
dpb3379b952018-09-24 13:39:04 -070050 bindingRequest(binding.key(), FrameworkType.PROVIDER),
dpb3db68f02018-01-04 09:15:52 -080051 generatedComponentModel.name())
dpb64861842017-12-12 08:17:18 -080052 .codeBlock());
53 }
54
55 @Override
dpb3db68f02018-01-04 09:15:52 -080056 public Optional<ClassName> alternativeFrameworkClass() {
cgdecker92665492018-05-03 07:56:17 -070057 return Optional.of(TypeNames.PRODUCER);
dpb64861842017-12-12 08:17:18 -080058 }
ronshapiroe6740762018-05-01 08:37:26 -070059
60 // TODO(ronshapiro): should this have a simple factory if the delegate expression is simple?
dpb64861842017-12-12 08:17:18 -080061}