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.writing; 18 19 import static com.google.common.base.Preconditions.checkNotNull; 20 import static dagger.internal.codegen.binding.SourceFiles.generatedClassNameForBinding; 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.ComponentImplementation.ShardImplementation; 28 import dagger.internal.codegen.writing.FrameworkFieldInitializer.FrameworkInstanceCreationExpression; 29 30 /** 31 * A {@link dagger.producers.Producer} creation expression for a {@link 32 * dagger.producers.Produces @Produces}-annotated module method. 33 */ 34 // TODO(dpb): Resolve with InjectionOrProvisionProviderCreationExpression. 35 final class ProducerCreationExpression implements FrameworkInstanceCreationExpression { 36 37 private final ShardImplementation shardImplementation; 38 private final ComponentRequestRepresentations componentRequestRepresentations; 39 private final ContributionBinding binding; 40 41 @AssistedInject ProducerCreationExpression( @ssisted ContributionBinding binding, ComponentImplementation componentImplementation, ComponentRequestRepresentations componentRequestRepresentations)42 ProducerCreationExpression( 43 @Assisted ContributionBinding binding, 44 ComponentImplementation componentImplementation, 45 ComponentRequestRepresentations componentRequestRepresentations) { 46 this.binding = checkNotNull(binding); 47 this.shardImplementation = componentImplementation.shardImplementation(binding); 48 this.componentRequestRepresentations = checkNotNull(componentRequestRepresentations); 49 } 50 51 @Override creationExpression()52 public CodeBlock creationExpression() { 53 return CodeBlock.of( 54 "$T.create($L)", 55 generatedClassNameForBinding(binding), 56 componentRequestRepresentations.getCreateMethodArgumentsCodeBlock( 57 binding, shardImplementation.name())); 58 } 59 60 @AssistedFactory 61 static interface Factory { create(ContributionBinding binding)62 ProducerCreationExpression create(ContributionBinding binding); 63 } 64 } 65