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.collect.Iterables.getOnlyElement; 20 import static dagger.internal.codegen.binding.BindingRequest.bindingRequest; 21 22 import com.squareup.javapoet.CodeBlock; 23 import dagger.assisted.Assisted; 24 import dagger.assisted.AssistedFactory; 25 import dagger.assisted.AssistedInject; 26 import dagger.internal.codegen.binding.ContributionBinding; 27 import dagger.internal.codegen.writing.FrameworkFieldInitializer.FrameworkInstanceCreationExpression; 28 29 /** 30 * A {@link FrameworkInstanceCreationExpression} for {@link 31 * dagger.internal.codegen.model.BindingKind#OPTIONAL optional bindings}. 32 */ 33 final class OptionalFactoryInstanceCreationExpression 34 implements FrameworkInstanceCreationExpression { 35 private final OptionalFactories optionalFactories; 36 private final ContributionBinding binding; 37 private final ComponentImplementation componentImplementation; 38 private final ComponentRequestRepresentations componentRequestRepresentations; 39 40 @AssistedInject OptionalFactoryInstanceCreationExpression( @ssisted ContributionBinding binding, OptionalFactories optionalFactories, ComponentImplementation componentImplementation, ComponentRequestRepresentations componentRequestRepresentations)41 OptionalFactoryInstanceCreationExpression( 42 @Assisted ContributionBinding binding, 43 OptionalFactories optionalFactories, 44 ComponentImplementation componentImplementation, 45 ComponentRequestRepresentations componentRequestRepresentations) { 46 this.optionalFactories = optionalFactories; 47 this.binding = binding; 48 this.componentImplementation = componentImplementation; 49 this.componentRequestRepresentations = componentRequestRepresentations; 50 } 51 52 @Override creationExpression()53 public CodeBlock creationExpression() { 54 return binding.dependencies().isEmpty() 55 ? optionalFactories.absentOptionalProvider(binding) 56 : optionalFactories.presentOptionalFactory( 57 binding, 58 componentRequestRepresentations 59 .getDependencyExpression( 60 bindingRequest( 61 getOnlyElement(binding.dependencies()).key(), binding.frameworkType()), 62 componentImplementation.shardImplementation(binding).name()) 63 .codeBlock()); 64 } 65 66 @AssistedFactory 67 static interface Factory { create(ContributionBinding binding)68 OptionalFactoryInstanceCreationExpression create(ContributionBinding binding); 69 } 70 } 71